Professional Documents
Culture Documents
Register
·
Sign in
·
Help · Go To
Power BI Overview Products Pricing Solutions Partners Resources Community
Anonymous
Not applicable
Hi Everyone,
All solutions I found was to use DAX which I cannot use for my data at this time.
Here is what my data looks like, I would like a running sum of the cost in a new column.
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 1/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
Solved!
Go to Solution.
Labels:
Need Help
1
Reply
1 ACCEPTED SOLUTION
MarcelBeug
Community Champion
In response to Anonymous
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 2/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
10-25-2017
01:34 PM
You can use this query (assuming you want to group on "BU"):
let
Source = Table1,
#"Expanded AllData" = Table.ExpandTableColumn(#"Grouped Rows", "AllData", {"Location", "Month", "Cost", "Running Sum"}, {"Location",
in
#"Expanded AllData"
let
in
AddedRunningSum
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 3/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
Power
Message 3 of 46 (47,305 BI
Views) Overview Products Pricing Solutions Partners Resources Community
12
Reply
Jcarofi
Frequent Visitor
09-08-2021
01:16 PM
Please and if I want the accumulated taking into account the BU and Location columns
0 Reply
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 4/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
jborro
Power
Solution SageBI Overview Products Pricing Solutions Partners Resources Community
In response to Jcarofi
09-08-2021
01:31 PM
These lines:
#"Expanded AllData" = Table.ExpandTableColumn(#"Grouped Rows", "AllData", {"Location", "Month", "Cost", "Running Sum"}, {"Locat
ion", "Month", "Cost", "Running Sum"})
#"Expanded AllData" = Table.ExpandTableColumn(#"Grouped Rows", "AllData", {"Month", "Cost", "Running Sum"}, {"Month", "Cost",
"Running Sum"})
0 Reply
Jcarofi
Frequent Visitor
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 5/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
In response to jborro
Power BI Overview Products Pricing Solutions Partners Resources Community
09-08-2021
02:51 PM
0 Reply
jborro
Solution Sage
In response to Jcarofi
09-08-2021
03:21 PM
Ok.
This is a modified fnAddRunningSum - nothing really important, just made the values column name a variable to
untie the internal data structure to the main table from the computation logic in the function:
let
Source = Table.Buffer(MyTable),
AddedRunningSum = Table.FromColumns(Table.ToColumns(Source)&{Cumulative},TableType)
in
AddedRunningSum
This is the main table and the call to the function above using your sample data:
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 7/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
let
Power BI
SourceOverview Products Pricing Solutions Partners Resources Community
= Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKlTSUUoEYkMDpVgdBBeVZ4xPEsJLxsoz
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Est", type text}, {"CICL", type text}, {"ve", type num
ber}}),
#"Grouped Rows" = Table.Group(Source, {"Est", "CICL"}, {{"AllData", each fnAddRunningSum (_, "ve"), TableTyp
e}}),
#"Expanded AllData" = Table.ExpandTableColumn(#"Grouped Rows", "AllData", {"ve", "Running Sum"}, {"ve", "Runn
ing Sum"})
in #"Expanded AllData"
Please note that the function call has changed slightly as we added the second parameter (value column name) to
the function which calculates running totals.
EstCICLveRunning Sum
q a 10 10
q a 0 10
q a 30 40
q a 0 40
q a 0 40
c a 0 0
c a 40 40
c a 0 40
c a 50 90
c a 0 90
d a 30 30
d a 60 90
d a 0 90
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 8/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
d a 0 90
d a 0 90
Power
q
BI
b
Overview
0 0
Products Pricing Solutions Partners Resources Community
q b 40 40
q b 20 60
q b 0 60
c b 40 40
c b 0 40
c b 70 110
c b 0 110
Kind regards,
JB
0 Reply
Jcarofi
Frequent Visitor
In response to jborro
09-08-2021
04:43 PM
I want to thank you for responding to my request, but the code has not worked for me, I don't know what I'm doing w
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 9/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
Power
BI Overview Products Pricing Solutions Partners Resources Community
let
Est_Table = Origen{[Item="Est",Kind="Table"]}[Data],
#"Grouped Rows" = Table.Group(Est_Table, {"Estanque", "Ciclo"}, {{"AllData", each fnAddRunningSum (_, "Raleo"), Tab
#"Expanded AllData" = Table.ExpandTableColumn(#"Grouped Rows", "AllData", {"Raleo", "Running Sum"}, {"Raleo", "Runn
in #"Expanded AllData"
Running Funcion
let
Source = Table.Buffer(Est),
in
AddedRunningSum
(ve) is = "Raleo"
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 10/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
0 Reply
jborro
Solution Sage
In response to Jcarofi
09-08-2021
06:28 PM
I do no think that there is something wrong with the code. It looks Ok.
The error that you get is the "stack overflow", which potentially means that the calculation result does not fit into the
variable type.
Could you please try to filter your table (after #"Tipo cambiado" step) to take only 10 top lines to prove that the
concept works?
Thanks,
JB
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 11/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
JB
0 Reply
Jcarofi
Frequent Visitor
In response to jborro
09-08-2021
06:43 PM
If you have an email where I can send you the files, I thank you
0 Reply
jborro
Solution Sage
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 12/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
Solution Sage
Power BI toOverview
In response Jcarofi Products Pricing Solutions Partners Resources Community
09-12-2021
11:37 AM
Hi @Jcarofi,
if this is still not resolved, you can save the sample files to Google Drive, Dropbox or alike and send me a link via
private message. I can have a look later today.
Thanks,
JB
0 Reply
Jcarofi
Frequent Visitor
In response to jborro
1 1
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 13/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
09-08-2021
06:41 PM
Power BI Overview Products Pricing Solutions Partners Resources Community
As the function is written in the result, you start creating rows by mixing the (ponds) with the (cycles) creating
0 Reply
Jcarofi
Frequent Visitor
In response to Jcarofi
09-08-2021
02:52 PM
0 Reply
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 14/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
Helper III
05-19-2021
06:56 PM
Unbelievable.
Power Bi has invented a billion new functions and formulas but needs 20 lines of code across a query and embedded function to
calculate the running total?
Unbelievable.
1
Reply
Anonymous
Not applicable
05-26-2020
12:12 PM
Hi @MarcelBeug
Would be great if you could help me understand how the below statement is working in the code that you provided. I am having a hard
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 15/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
Thanks in advance!
0 Reply
78chris
New Member
03-31-2018
03:47 AM
Hello
To MarcelBeug
Thi i d
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 16/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
This is very good.
Power BI Overview Products Pricing Solutions Partners Resources Community
Is it possible to give the Field to use (Cost here) as a parameter of the function ?
Thanks a lot
78Chris
1
Reply
78chris
New Member
In response to 78chris
10-18-2018
02:15 AM
Hello
let
Source = Table.Buffer(MyTable),
in
AddedRunningSum
2
Reply
TheOctopusIAm
Frequent Visitor
In response to 78chris
03-20-2020
11:55 AM
This is great, though how should I call this function now that it has two arguments?
@78chris
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 18/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
0 Reply
TheOctopusIAm
Frequent Visitor
In response to TheOctopusIAm
03-20-2020
01:09 PM
@78chris
Thanks Chris, you posted this code to allow me to select the specific column :
let
Source = Table.Buffer(MyTable),
Cumul = Table.FromColumns(Table.ToColumns(Source)&{Cumulative},TableType)
i
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 19/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
in
Power BI
AddedRunningSum
Overview Products Pricing Solutions Partners Resources Community
Below 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,
in
#"Expanded AllData"
0 Reply
cyongt_bdf
Advocate II
In response to TheOctopusIAm
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 20/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
03-21-2020
03:25 AM
Power BI Overview Products Pricing Solutions Partners Resources Community
Hi @TheOctopusIAm ,
You may try this code where "ColumnHeader" is your column header name:
let
Source = Table1,
in
#"Expanded AllData"
2
Reply
TheOctopusIAm
Frequent Visitor
In response to cyongt_bdf
03-21-2020
09:26 PM
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 21/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
Expression.Error: There is an unknown identifier. Did you use the [field] shorthand for a _[field] outside of an
'each' expression?
I've had a search around, but can't find any similar scenarios that generate this same error.
Any thoughts?
0 Reply
cyongt_bdf
Advocate II
In response to TheOctopusIAm
03-22-2020
12:19 AM
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 22/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
Hi @TheOctopusIAm ,
Power BI Overview Products Pricing Solutions Partners Resources Community
let
Source = Table1,
in
#"Expanded AllData"
3
Reply
SegerC
Regular Visitor
In response to cyongt_bdf
03-31-2020
02:11 PM
Hi @ bdf!
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 23/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
Hi @cyongt_bdf!
Power BI Overview Products Pricing Solutions Partners Resources Community
I am having trouble getting this to work. I have to apologize, I'm new to M and power queries custom functions.
When I copied the original table submitted by OP, made it into a source and tried to apply the formulas on it
chaning "ColumnHeader" to "Costs". However, I received the following error:
Expression.Error: The import AddedRunningSum matches no exports. Did you miss a module reference?
How do I solve this? Additionally, is the name of the function "query" important, i.e. does it need to
"fnAddRunningSum" or "AddedRunningSum"?
0 Reply
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 24/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
Helpful resources
Announcements
Learn More
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 25/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
Recommendations
Power BI Overview Products Pricing Solutions Partners Resources Community
07-13-2021
Running total by group with a reset
02:02 AM
08-26-2021
SUM IFS in Power Query using a date and article nu...
03:55 AM
08-20-2021
More efficient running total column in Power Query...
10:09 AM
09-14-2021
Power Query running slow through google search
02:16 AM
12-07-2021
Sum of previous Periods Grouped
02:55 AM
Featured Topics
amitchandak 309
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 26/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
lbendlin 69
johnt75
58
PaulDBrown 50
Jihwan_Kim 41
See all ≫
amitchandak
344
truptis
161
lbendlin
114
parry2k
96
PaulDBrown
68
See all ≫
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 27/29
3/27/22, 11:30 AM Solved: How to do a running Sum by group in Power Query? - Microsoft Power BI Community
Webinars
Developers
Sign in
Blog
Sign up
Newsletter
https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/m-p/290123 29/29