Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
ompowerbi
Helper I
Helper I

Running Total for each Group / ID

Hi, I am trying to create a running total in my table that only does the running total for each respective Group / ID. An example of my table is below. 

IDNumberDate
Apple2001/01/2023
Apple-3006/01/2023
Apple-4011/01/2023
Apple2016/01/2023
Banana1002/12/2022
Banana1007/12/2022
Banana-4012/12/2022
Banana1017/12/2022
Banana-5022/12/2022
Orange-8009/02/2022
Orange1021/02/2022
Orange1005/03/2022
Orange2017/03/2022
Orange1029/03/2022

 


The result would ideally look like this:

IDNumberDateRunning total per group
Apple2001/01/202320
Apple-3006/01/2023-10
Apple-4011/01/2023-50
Apple2016/01/2023-30
Banana1002/12/202210
Banana1007/12/202220
Banana-4012/12/2022-20
Banana1017/12/2022-10
Banana-5022/12/2022-60
Orange-8009/02/2022-80
Orange1021/02/2022-70
Orange1005/03/2022-60
Orange2017/03/2022-40
Orange1029/03/2022-30



I have been using these formulas in power query:
Sum by group:

= Table.AddColumn(#"Added Custom", "Custom.1", each let _Group = [ID] in
List.Sum(
Table.SelectRows(#"Added Custom", each [ID] = _Group)[Number]))


Running Total:
= Table.AddColumn(#"Added Index", "Custom", each List.Sum(List.FirstN(#"Added Index"[Number],[Index])))

I have been trying to combine these two but I cannot seem to get them to work together, does anyone have any suggestions. (I am fairly new to power query and M code). 

Thank you 

1 ACCEPTED SOLUTION
Vijay_A_Verma
Super User
Super User

Use this. Replace ??? appropriately

let
    Source = ???
    BuffNumberList = Source[Number],
    BuffIDList = Source[ID],
    GenListOfRunningTotal = List.Generate(()=>[x=BuffNumberList{0},y=0], each [y]<List.Count(BuffNumberList), each [y=[y]+1, x=if BuffIDList{y} = BuffIDList{[y]} then [x]+BuffNumberList{y} else BuffNumberList{y}], each [x]),
    Result = Table.FromColumns(Table.ToColumns(Source) & {GenListOfRunningTotal},Table.ColumnNames(Source)&{"Running total per group"})
    in
    Result

View solution in original post

3 REPLIES 3
Vijay_A_Verma
Super User
Super User

Use this. Replace ??? appropriately

let
    Source = ???
    BuffNumberList = Source[Number],
    BuffIDList = Source[ID],
    GenListOfRunningTotal = List.Generate(()=>[x=BuffNumberList{0},y=0], each [y]<List.Count(BuffNumberList), each [y=[y]+1, x=if BuffIDList{y} = BuffIDList{[y]} then [x]+BuffNumberList{y} else BuffNumberList{y}], each [x]),
    Result = Table.FromColumns(Table.ToColumns(Source) & {GenListOfRunningTotal},Table.ColumnNames(Source)&{"Running total per group"})
    in
    Result

Thank you for the response, this works great. If you don't mind, I had come up with something sort of similar as a blank query. 

(values as list, ID as list) as list=>
let
RT =
List.Generate(
()=>[RT = values{0},counter = 1],
each [counter]-1 < List.Count(values),
each (if ID{[counter]-1}=ID{[counter]}
then [RT = [RT]+values{[counter]},counter=[counter]+1]
else [RT = values{[counter]},counter=[counter]+1]),
each [RT])
in
RT

However, on this line "each [counter]-1 < List.Count(values)," I get the error message: 'An error occurred in the ‘’ query. Expression.Error: There weren't enough elements in the enumeration to complete the operation.
Details: [List]', and another line is added to the table just saying error for each column.

 

If I change the line to: " each [counter] < List.Count(values)," then the last value in the RT (running total) column is null. I am not sure of a way around this, do you have any suggestions please?

Thank you for the response, this works great. If you don't mind, I had come up with something sort of similar as a blank query. 

(values as list, ID as list) as list=>
let
RT =
List.Generate(
()=>[RT = values{0},counter = 1],
each [counter]-1 < List.Count(values),
each (if ID{[counter]-1}=ID{[counter]}
then [RT = [RT]+values{[counter]},counter=[counter]+1]
else [RT = values{[counter]},counter=[counter]+1]),
each [RT])
in
RT

However, on this line "each [counter]-1 < List.Count(values)," I get the error message: 'An error occurred in the ‘’ query. Expression.Error: There weren't enough elements in the enumeration to complete the operation.
Details: [List]', and another line is added to the table just saying error for each column.

 

If I change the line to: " each [counter] < List.Count(values)," then the last value in the RT (running total) column is null. I am not sure of a way around this, do you have any suggestions please?

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.