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
lottieritchie
Helper I
Helper I

Create report with running totals

I need to create a report with running totals where certain values exist in the source data table.

 

The source table has a column "city" and there are multiple values in this city column. There is another column which lists a value for "week purchased".   I need my report to only calculate the running count total for "week purchased" where "city" equals say Manchester. I want it to ignore all the other values where the city is something different. Help please!

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @lottieritchie ,

 

you need a slightly modified version from here: https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/td-p/290123...

 

Assuming a table like this (mySource):

CityWeek Purchased
MCH10
LDN5
MSK12
PAR8
MCH5
LDN4
MSK3

 

This is the PQ code in your main query:

 

let
    Source = mySource,
    #"Grouped Rows" = Table.Group(Source, {"City"}, {{"AllData", each fnAddRunningSum (_, "Week Purchased", {"MCH"})}}),
    #"Expanded AllData" = Table.ExpandTableColumn(#"Grouped Rows", "AllData", {"Week Purchased", "Cumul"}, {"Week Purchased", "Cumul"})
in
    #"Expanded AllData"

 

 

This is the modified function (fnAddRunningSum):

 

(MyTable as table, MyColumn as text, myFilter as list) =>
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},(seed,add) => seed & {List.Last(seed) + add})),
    Cumul = if List.Contains(myFilter, MyTable[City]{0}) then Table.FromColumns(Table.ToColumns(Source)&{Cumulative},TableType) else MyTable
in
    Cumul

 

 

Please note that you need to pass the "cilty" filter as a list (in {}) even if there is only one city to filter - this version gives you the flexibility to use more than one city.

 

The result looks like this:

 

 

 

 

 

 

Regards,

JB

 

View solution in original post

1 REPLY 1
Anonymous
Not applicable

Hi @lottieritchie ,

 

you need a slightly modified version from here: https://community.powerbi.com/t5/Desktop/How-to-do-a-running-Sum-by-group-in-Power-Query/td-p/290123...

 

Assuming a table like this (mySource):

CityWeek Purchased
MCH10
LDN5
MSK12
PAR8
MCH5
LDN4
MSK3

 

This is the PQ code in your main query:

 

let
    Source = mySource,
    #"Grouped Rows" = Table.Group(Source, {"City"}, {{"AllData", each fnAddRunningSum (_, "Week Purchased", {"MCH"})}}),
    #"Expanded AllData" = Table.ExpandTableColumn(#"Grouped Rows", "AllData", {"Week Purchased", "Cumul"}, {"Week Purchased", "Cumul"})
in
    #"Expanded AllData"

 

 

This is the modified function (fnAddRunningSum):

 

(MyTable as table, MyColumn as text, myFilter as list) =>
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},(seed,add) => seed & {List.Last(seed) + add})),
    Cumul = if List.Contains(myFilter, MyTable[City]{0}) then Table.FromColumns(Table.ToColumns(Source)&{Cumulative},TableType) else MyTable
in
    Cumul

 

 

Please note that you need to pass the "cilty" filter as a list (in {}) even if there is only one city to filter - this version gives you the flexibility to use more than one city.

 

The result looks like this:

 

 

 

 

 

 

Regards,

JB

 

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!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.

Top Solution Authors