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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
EiliseH
Regular Visitor

PowerQuery Cumulative/Running Total with less than condition on Date

Hello,

I'm hoping someone here can help.

I have a data set which records how much is spent per week on each project by each role in each stage of work.

In PowerQuery (not DAX), I want to add a cumulative expenditure column for each project-role-stage.

Form searching, I think the first step is to to group by project, role and stage, but I'm not sure where to go from there?

I have pasted some sample data below, with my desired result in the ExpenditureToDate column.

In Excel I have used the below formula to acheive my desired results:

=SUMIFS([ExpenditureInWeek],[Project],[@Project],[Role],[@Role],[Stage],[@Stage],[WeekEnding],"<="&[@WeekEnding])

 

ProjectRoleStageWeekEndingExpenditureInWeekCumulativeExpenditure
AFaçade SpecialistStage 407/07/202430003000
AFaçade SpecialistStage 414/07/202430006000
AFaçade SpecialistStage 421/07/202430009000
AFaçade SpecialistStage 428/07/2024300012000
AFaçade SpecialistStage 528/07/202420002000
AFaçade SpecialistStage 504/08/202430005000
AFaçade SpecialistStage 511/08/202410006000
AArchitectStage 414/07/2024200200
AArchitectStage 421/07/2024100300
BFaçade SpecialistStage 418/08/202450005000
BFaçade SpecialistStage 425/08/202410006000
BFaçade SpecialistStage 401/09/20246006600
BFaçade SpecialistStage 408/09/20244007000
1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

lbendlin_0-1726182720894.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCijKz0pNLlHSUQrKz0kFUsEliekgOjw1Nds1LyUzLx3Ica0oSAWyS0qLUj3zQDJKsTrRSo5AGbfEw8sTU1IVggtSkzMTczKLS2BmKJgAWQbm+kBkZGAE4hgbGBgQq9HQhEyNRobkarQgQ6MpukYjEjQaAP1oQY6NhoZIGg2RNToWJWdklkAiFEdgGhFUjhKEhlDlTkTEmQWSs0wNiNdoZIrNP0RoNAC61BKm0YwEfRZI+kxA+mIB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"ExpenditureInWeek", Currency.Type}, {"WeekEnding", type date}},"en-GB"),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "CumulativeExpenditure", (k)=> List.Sum(Table.SelectRows(#"Changed Type", each [Project]=k[Project] and [Role]=k[Role] and [Stage]=k[Stage] and [WeekEnding]<=k[WeekEnding])[ExpenditureInWeek]),Currency.Type)
in
    #"Added Custom"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

 

View solution in original post

2 REPLIES 2
EiliseH
Regular Visitor

Thank you so much, @lbendlin, this works great!

lbendlin
Super User
Super User

lbendlin_0-1726182720894.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCijKz0pNLlHSUQrKz0kFUsEliekgOjw1Nds1LyUzLx3Ica0oSAWyS0qLUj3zQDJKsTrRSo5AGbfEw8sTU1IVggtSkzMTczKLS2BmKJgAWQbm+kBkZGAE4hgbGBgQq9HQhEyNRobkarQgQ6MpukYjEjQaAP1oQY6NhoZIGg2RNToWJWdklkAiFEdgGhFUjhKEhlDlTkTEmQWSs0wNiNdoZIrNP0RoNAC61BKm0YwEfRZI+kxA+mIB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"ExpenditureInWeek", Currency.Type}, {"WeekEnding", type date}},"en-GB"),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "CumulativeExpenditure", (k)=> List.Sum(Table.SelectRows(#"Changed Type", each [Project]=k[Project] and [Role]=k[Role] and [Stage]=k[Stage] and [WeekEnding]<=k[WeekEnding])[ExpenditureInWeek]),Currency.Type)
in
    #"Added Custom"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

 

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 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.