Power BI calcualte the variance between two rows of table data - dax

first time caller long time listener. I'm trying to work out the variance for each row of a table of data for my local junior tiathlon club. I need to work out the difference of time in a particular event since the previous date to know if they have improved or not. e.g. if a student only runs for two days and these are recorded as rows, how can i add a column to calculate if they are faster or slower? The data is below and the final column illustrates what i need. You'll see that the variance resets to 0 for each category. I'm trying to do this in Power BI DEsktop in the tables as an added column so i can use the data against a matrix table report.
Put simply, how can i measure the time in the current row against the time in the previous row>
Thanks in advance, Moe

Try
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
// might need to replace replace Excel.CurrentWorkbook with Excel.WorkBook for Power BI
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Event", type text}, {"Category", type any}, {"Date", type date}, {"Time", type time}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Name", "Event"}, {
{"data", each
let a=Table.AddIndexColumn(Table.Sort(_,{{"Date", Order.Ascending}, {"Time", Order.Ascending}}), "index", 0, 1, Int64.Type) ,
b=Table.AddColumn(a,"diff", each try Duration.ToText(a{[index]}[Time] - a{[index]-1}[Time]) otherwise 0)
in b, type table }}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"Category", "Date", "Time", "diff"}, {"Category", "Date", "Time", "diff"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded data",{{"Date", type date}, {"Time", type time}})
in #"Changed Type1"

Related

PowerQuery / multiply many columns with a single column / shift up results without blank(null)

First of all, sorry for my broken English. If something might feel offence, that is my fault. Sorry (and Thanks) in advance.
As title said, I want to get a way how to multiply many rows with one single row in one step: regardless how many columns and whatever column name is, & ignore blank cells then shift up filled results in POWERQUERY.
want to get a final table in yellow, with merging Ratio table and Total table.
want to know how to multiply Ratio table (many columns) and Total table (a single column) in one step.
From Grey table, want to get a way how to remove null value and shift up filled results.
prefer "Transform" than "Add column".
if you need an example file, can get it via this link : https://www.dropbox.com/s/fs2cymeak1f2w57/powerquery%20practice.xlsx?dl=0
Im 99% certain Im doing homework for someone, but anyway
Unpivot Ratio. Merge in Total, multiply with custom column. Group on Attribute and add index. Pivot
let Source = Ratio,
// I am too lazy to do my own homework and should be ashamed
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"M"}, "Attribute", "Value"),
#"Filtered Rows1" = Table.SelectRows(#"Unpivoted Other Columns", each ([Value] <> 0)),
#"Merged Queries" = Table.NestedJoin(#"Filtered Rows1", {"M"}, Total, {"Month"}, "Total", JoinKind.LeftOuter),
#"Expanded Total" = Table.ExpandTableColumn(#"Merged Queries", "Total", {"Total"}, {"Total"}),
#"Added Custom" = Table.AddColumn(#"Expanded Total", "Custom", each if [Total]=null then 0 else [Total]*[Value]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Value", "Total","M"}),
#"Grouped Rows" = Table.Group(#"Removed Columns", {"Attribute"}, {{"data", each Table.AddIndexColumn(_, "Month", 1, 1, Int64.Type), type table }}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", { "Custom", "Month"}, { "Custom", "Month"}),
#"Pivoted Column" = Table.Pivot(#"Expanded data", List.Distinct(#"Expanded data"[Attribute]), "Attribute", "Custom")
in #"Pivoted Column"

Power Query: Duplicate Rows Based on Value

I have a column that contains the Total Stock of an item. I'd like to expand this out into 1 row per item (i.e. the item has 6 in stock and therefore appears as 6 line items).
Is this possible with power query?
The M-Code below will expand this input table
to this
let
Source = Excel.CurrentWorkbook(){[Name="tblData"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ColA", type text}, {"Stock", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Col", each List.Repeat({[ColA]},[Stock])),
#"Expanded Col" = Table.ExpandListColumn(#"Added Custom", "Col")
in
#"Expanded Col"

Power Query transform a table with date ranges into daily

I am using Power Query to solve the following problem:
need to create a daily split of data based on a range of dates.
I have a table with ad campaign results. Each line is a campaign with Index as ID. It has a start date, end date (or a duration) and some numeric fields, in this case AdRequests and Spend.
For each line, it's a total for the number days in Duration column.
What I need to do is un-summarise this into daily data. Populate a daily data out of it with values of spend etc being a daily average. N.B. number of campaigns varies from project to project, it needs to be dynamic
i.e. this is my input table
and here is what i want it to be. This is example for 1st campaign only, but the table needs to have all of them, and that varies from project to project.
I have to deliver this tomorrow, and so far my search hasn't yielded anything remotely good.
thank you for any ideas, in advance
Here's how I did it.
Add a custom column with the formula = {1..[Duration]}
Expand the custom column
Change start date to number type
Add a custom column with the formula = [Start Date] + [Custom] -1
Change start date back to a date type column
Delete unnecessary columns
Rename columns
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"Start Date", type datetime}, {"Ad Requests", Int64.Type}, {"Spend", Int64.Type}, {"Duration", Int64.Type}, {"Daily Adrequests", Int64.Type}, {"Daily Spend", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each {1..[Duration]}),
#"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded Custom",{{"Start Date", Int64.Type}}),
#"Added Custom1" = Table.AddColumn(#"Changed Type1", "Custom.1", each [Start Date] + [Custom] -1),
#"Changed Type2" = Table.TransformColumnTypes(#"Added Custom1",{{"Custom.1", type date}}),
#"Reordered Columns" = Table.ReorderColumns(#"Changed Type2",{"Index", "Custom.1", "Start Date", "Ad Requests", "Spend", "Duration", "Daily Adrequests", "Daily Spend", "Custom"}),
#"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"Start Date", "Ad Requests", "Spend", "Duration"})
in
#"Removed Columns"
Before/After

Power Query M - Custom Column for Rolling 28 Days Sales

I'm looking for some Power Query help. I have a huge set of sales data for 40k products over one year. For each product on each day I need to add a 28 day sales column.
I essentially want to do a sumifs like the below but in M.
=SUMIFS([SALES],[Product Code],[This Product Code],[Date],<=[This Date],[Date],>=[This Date]-28))
Try this then, it should work but would likely do so at a crawl
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Sales", Int64.Type}, {"Product Code", type text}, {"Date", type date}}),
TotalAmountAdded = Table.AddColumn(Source, "Total Amount", (i) => List.Sum(Table.SelectRows(Source, each ([Product Code] = i[Product Code] and [Date]<=i[Date] and [Date]>=Date.AddDays(i[Date],-28)))[Sales]), type number )
in TotalAmountAdded
Add a custom column with date logic (based on your sample sumif formula), filter the new column to get the relevant rows, then group by product code and sum Sales. Assuming source data is in Table1 with three columns (Sales,Product Code, Date) the code would be
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Sales", Int64.Type}, {"Product Code", type text}, {"Date", type date}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "AddMe", each if [Date]<=DateTime.Date(DateTime.LocalNow()) and [Date]>=Date.AddDays(DateTime.Date(DateTime.LocalNow()),-28) then 1 else 0),
#"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([AddMe] = 1)),
#"Grouped Rows" = Table.Group(#"Filtered Rows", {"Product Code"}, {{"ProductSales", each List.Sum([Sales]), type number}})
in #"Grouped Rows"

Iterating over each cell in a column in Power Query

I created a table called Table3 with two columns named URL which is empty and Value which contains a list of websites. The following query retrieves data from the websites stored in Table 3.
let
Parameter = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
URL= Parameter{1}[Value],
Source = Web.Page(Web.Contents(URL)),
Data0 = Source{0}[Data],
#"Changed Type" = Table.TransformColumnTypes(Data0,{{"Date", type date}, {"Open", type number}, {"High", type number}, {"Low", type number}, {"Close", type number}, {"Volume", type number}, {"Market Cap", type number}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Market Cap", "Open", "High", "Low"}),
#"Sorted Rows" = Table.Sort(#"Removed Columns",{{"Date", Order.Ascending}})
in
#"Sorted Rows"
The second line runs the query for the first website in the Value column.
Is it possible to introduce a loop that would run the query for all the websites?
If not is it possible to run the query for all the websites in sequence by manually pasting the above code and changing the number in the brackets for each website?
If it is possible I assume it would load the contents in the same sheet, is there any way to load the content in different sheets for each iteration?
Thanks for reading my question.
Loops aren't really a thing in Power Query, but you can still do what you're after. I don't know what URLs you're pulling from, so let me give you an example using publicly available ones.
Let's suppose my Table3 is the following:
URL
--------
https://finance.yahoo.com/quote/AAPL?p=AAPL
https://finance.yahoo.com/quote/AAPL?p=GOOG
I can load this into the query editor and create a custom column that reads the webpage for each URL.
= Web.Page(Web.Contents([URL])){0}[Data]
(The table I want is the first one (hence the {0} row index) and is in the [Data] column.)
Now I have a table like this where the bottom table is a preview of the cell I have selected.
Click the arrows icon to expand the tables.
From here you can filter Column1 to pick which values you are interested in (let's say Ask, Bid, Open, and Volume) and then pivot that column (Transform > Pivot Column). Choose Column2 as the values column and select "Don't Aggregate" under Advanced options.
The result should be the table you see above the pivot dialogue box.
Here's the full M code for the query that shows up in the Advanced Editor
let
Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"URL", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Web.Page(Web.Contents([URL])){0}[Data]),
#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Column1", "Column2"}, {"Column1", "Column2"}),
#"Filtered Rows" = Table.SelectRows(#"Expanded Custom", each ([Column1] = "Ask" or [Column1] = "Bid" or [Column1] = "Open" or [Column1] = "Volume")),
#"Pivoted Column" = Table.Pivot(#"Filtered Rows", List.Distinct(#"Filtered Rows"[Column1]), "Column1", "Column2")
in
#"Pivoted Column"

Resources