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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
mtpirhala
Regular Visitor

Calculate Month over Month Labor Cost

Good morning, 

 

Thanks for the information you provide. I am new to Power BI and have gotten so much information from this forum already. 

 

I have a table that I am trying to adjust and want to use Power Query. In the example table (see below), I have the original sales price for a project and I want to get a running total of the labor/material costs by month. I have seen tuturials on adding running totals month over month, but I would need to subtract month over month. 

 

DateSales PriceLabor MaterialsTotal Cost (C1+D1)Net
4/1/20211,00010050150850
5/1/20211,000200100300550
6/1/20211,00010025125425
7/1/20211,00015015165260
Total1,000550190740260

 

The Sales Price stays the same as it is for one project, while the labor and materials continues to reduce the profit month over month. Similar to a ledger. The Net is essentially just B2-D2 for the first row (Net = 850) then the subsequent rows subtract from each other (E3=E2-D3), so on and so forth. 

 

Is it possible to add another column and formula in Power Query? 

 

Thanks so much for any help you can provide. 

 

Myriah 

1 ACCEPTED SOLUTION
v-yanjiang-msft
Community Support
Community Support

Hi @mtpirhala ,

According to your description, here's my solution.

Add a custom column.

vkalyjmsft_0-1667543151742.png

[Sales Price]-List.Sum(Table.SelectRows(#"Changed Type",(x)=>x[Date]<=[Date])[Total Cost])

Get the correct result.

vkalyjmsft_1-1667543199175.png

Here's the whole M syntax, you can copy-paste in a blank query to see the details.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtE31DcyMDJU0lEy1DEwMADRYNIUzASSsTrRSqaYqoyQ1BobQJSZ4TLMyBTEBJIgVeZYVEFtAxFmQFWxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #"Sales Price" = _t, #"Labor " = _t, Materials = _t, #"Total Cost" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Sales Price", Int64.Type}, {"Labor ", Int64.Type}, {"Materials", Int64.Type}, {"Total Cost", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Net", each [Sales Price]-List.Sum(Table.SelectRows(#"Changed Type",(x)=>x[Date]<=[Date])[Total Cost]))
in
    #"Added Custom"

I also attach my sample below for your reference.

 

Best Regards,
Community Support Team _ kalyj

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-yanjiang-msft
Community Support
Community Support

Hi @mtpirhala ,

According to your description, here's my solution.

Add a custom column.

vkalyjmsft_0-1667543151742.png

[Sales Price]-List.Sum(Table.SelectRows(#"Changed Type",(x)=>x[Date]<=[Date])[Total Cost])

Get the correct result.

vkalyjmsft_1-1667543199175.png

Here's the whole M syntax, you can copy-paste in a blank query to see the details.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtE31DcyMDJU0lEy1DEwMADRYNIUzASSsTrRSqaYqoyQ1BobQJSZ4TLMyBTEBJIgVeZYVEFtAxFmQFWxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #"Sales Price" = _t, #"Labor " = _t, Materials = _t, #"Total Cost" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Sales Price", Int64.Type}, {"Labor ", Int64.Type}, {"Materials", Int64.Type}, {"Total Cost", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Net", each [Sales Price]-List.Sum(Table.SelectRows(#"Changed Type",(x)=>x[Date]<=[Date])[Total Cost]))
in
    #"Added Custom"

I also attach my sample below for your reference.

 

Best Regards,
Community Support Team _ kalyj

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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

Top Solution Authors