Forum Discussion
Anonymous
8 years agoNot applicable
How to do a running Sum by group in Power Query?
Hi Everyone, I am trying to do a running sum by group in Power Query (m language). Thank you. All solutions I found was to use DAX which I cannot use for my data at this time. Here is what my ...
- 8 years ago
You can use this query (assuming you want to group on "BU"):
let Source = Table1, TableType = Value.Type(Table.AddColumn(Source, "Running Sum", each null, type number)), #"Grouped Rows" = Table.Group(Source, {"BU"}, {{"AllData", fnAddRunningSum, TableType}}), #"Expanded AllData" = Table.ExpandTableColumn(#"Grouped Rows", "AllData", {"Location", "Month", "Cost", "Running Sum"}, {"Location", "Month", "Cost", "Running Sum"}) in #"Expanded AllData"With function fnAddRunningSum:
(MyTable as table) as table =>
let
Source = Table.Buffer(MyTable),
TableType = Value.Type(Table.AddColumn(Source, "Running Sum", each null, type number)),
Cumulative = List.Skip(List.Accumulate(Source[Cost],{0},(cumulative,cost) => cumulative & {List.Last(cumulative) + cost})),
AddedRunningSum = Table.FromColumns(Table.ToColumns(Source)&{Cumulative},TableType)
in
AddedRunningSum
78chris
8 years agoNew Member
Hello
To MarcelBeug
This is very good.
Is it possible to give the Field to use (Cost here) as a parameter of the function ?
Thanks a lot
78Chris
- 78chris7 years agoNew Member
Hello
I found a solution and give it
(MyTable as table, MyColumn as text) => let Source = Table.Buffer(MyTable), TableType = Value.Type(Table.AddColumn(Source, "Cumul", each null, type number)), Cumulative = List.Skip(List.Accumulate(Table.Column(Source,MyColumn),{0},(cumulative,MyColumn) => cumulative & {List.Last(cumulative) + MyColumn})), Cumul = Table.FromColumns(Table.ToColumns(Source)&{Cumulative},TableType) in AddedRunningSum- TheOctopusIAm6 years agoFrequent Visitor
This is great, though how should I call this function now that it has two arguments?
78chris- TheOctopusIAm6 years agoFrequent Visitor
78chris
Thanks Chris, you posted this code to allow me to select the specific column :(MyTable as table, MyColumn as text) => let Source = Table.Buffer(MyTable), TableType = Value.Type(Table.AddColumn(Source, "Cumul", each null, type number)), Cumulative = List.Skip(List.Accumulate(Table.Column(Source,MyColumn),{0},(cumulative,MyColumn) => cumulative & {List.Last(cumulative) + MyColumn})), Cumul = Table.FromColumns(Table.ToColumns(Source)&{Cumulative},TableType) in AddedRunningSumBelow is the original code, which calls this function. What changes need to be made to this to call the function, now that it has two arguments?
let Source = Table1, TableType = Value.Type(Table.AddColumn(Source, "Running Sum", each null, type number)), #"Grouped Rows" = Table.Group(Source, {"BU"}, {{"AllData", fnAddRunningSum, TableType}}), #"Expanded AllData" = Table.ExpandTableColumn(#"Grouped Rows", "AllData", {"Location", "Month", "Cost", "Running Sum"}, {"Location", "Month", "Cost", "Running Sum"}) in #"Expanded AllData"