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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

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
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors