I have a column with id, it has repeated items and columns with values. Data are like week 1, week 1, week 2, week 2, etc. I need to create a table where for week 3 I have an average value between 2 whole previous weeks (week 1, week 1 week 2, week 2), for week 4 I have an average value between week 2, week 2, week 3, week 3 from the original table etc. How can I do it in Power Query.
0
Oh ouch. First off, I’d fix your design. I would do it, but I don’t understand completely what it means. (could you explain?)
ID column has two bits of information in it, and it should only have one. So split the column on the semi-colon delimiter so you get two separate columns.
Then unpivot the Value1 and Value2 columns so you end up with just
(Vehicle, WeekNumber, Value)
Then sums and averages etc are really simple.
4
You can go to Data(Power Query)->Blank Query, and paste below code in the advance editor.
let
Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],
groupedbyId = Table.Group(Source, {"id"}, {{"values", (g)=> g[value 1] & g[value 2]}}),
splitId = Table.SplitColumn(groupedbyId, "id", Splitter.SplitTextByDelimiter(";"), {"Name", "Week"}),
extractWeekNum = Table.TransformColumns(splitId, {{"Week", (x) =>Text.AfterDelimiter(x, " ", {0, RelativePosition.FromEnd})}}),
changeToNumber = Table.TransformColumnTypes(extractWeekNum, {{"Week", type number}}),
filterRows = Table.AddColumn(changeToNumber, "PreviousTwoWeeks", (row) => Table.SelectRows(changeToNumber, (r)=> r[Name] =row[Name] and r[Week] >= row[Week]-2 and r[Week] <= row[Week]-1)),
listPrevious2WeeksValues = Table.AddColumn(filterRows, "Previous2WeeksValues", (row)=>List.Combine(row[PreviousTwoWeeks][values])),
average = Table.AddColumn(listPrevious2WeeksValues, "Previous2WeeksAverage", (row)=> List.Average(row[Previous2WeeksValues])),
displayValues = Table.AddColumn(average, "Display Values", (row)=> Text.Combine(List.Transform(row[Previous2WeeksValues], (x)=>Number.ToText(x)), ", ")),
removeUnused = Table.RemoveColumns(displayValues, {"values", "PreviousTwoWeeks", "Previous2WeeksValues"})
in
removeUnused
The main logic is in Table.SelectRows()
, it filters out data from previous two weeks.
Results look like this:
This should do it (same answer as I provided to your identical question in the Fabric community)
Note that Week 2 is included here as the average of the two preceding weeks (Week 0 and Week 1). I don’t understand why it was excluded in your example, but if you need to exclude it also for some reason, it is only a minor code change.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY85DoAgEEWvQqgtWF3iUQiFGio6G+PtZQsyA8Vv3s8szxh6HfdOHuc8YXSiPGSmdmp5ZDJEDbgOEYiLwNY00/PcQS7LXT7gWwrkKv3S71dlf/7nfL1rxFgVaIp4danGqNCjVdANFXx0A9qh4tdrCuiHiiJoPw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, #"value 1" = _t, #"value 2" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"id", type text}, {"value 1", Int64.Type}, {"value 2", Int64.Type}}),
//Group by id and return a list of all the values
#"Grouped Rows" = Table.Group(#"Changed Type", {"id"}, {
{"Values", each [value 1] & [value 2],type {Int64.Type}}
}),
//Split of the type (car or byke) to group on this
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "id Type", each List.First(Text.Split([id],";")), type text),
//Create a list of the rolling two week average, and add it as a colum
#"Grouped Rows1" = Table.Group(#"Added Custom", {"id Type"}, {
{"2 wk avg", (t)=>
let
RA = List.Generate(
()=>[ra=null, idx=1],
each [idx] <= Table.RowCount(t),
each[ra=if [idx]<2 then null
else List.Average((t{[idx]-1}[Values] & t{[idx]-2}[Values])), idx=[idx]+1],
each [ra])
in Table.FromColumns(
Table.ToColumns(t)
& {RA}, {"id","a","b","value"}
), type table [id=nullable text, a=any, b=any,value=nullable number]}}),
#"Removed Columns" = Table.RemoveColumns(#"Grouped Rows1",{"id Type"}),
#"Expanded 2 wk avg" = Table.ExpandTableColumn(#"Removed Columns", "2 wk avg", {"id", "value"}),
#"Filtered Rows" = Table.SelectRows(#"Expanded 2 wk avg", each ([value] <> null))
in
#"Filtered Rows"