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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

Calculate differences by category in power query

Hi!

 

I have a dataset similar to the one below, where each row describes a salary change for a given employee. If the end date is 9999.12.31, it is the current amount. 

I need an additional column that contains the increase amount (if there was an increase, so the employee appears more than once in the table). Eg. for Mike Peterson it's $255 000 in the first row, and it's 0 where there's been no increase or if it's the first time the given name appears.

Can you advise how to do that in power query?

Thanks a lot!

 

IDNameEffective DateEnd DateAmount
89765Mike Peterson2022.07.019999.12.31$1 106 000
89765Mike Peterson2022.01.012022.06.30$851 000
89765Mike Peterson2020.08.012021.12.31$774 000
59705755John Smith2021.12.139999.12.31$550 000
59706448Jack London2022.07.019999.12.31$350 000
59704850Sample Peter2021.02.159999.12.31$610 000
466490Amber Johnson2022.07.019999.12.31$519 000
466490Amber Johnson2022.01.012022.06.30$462 000
466490Amber Johnson2021.01.012021.12.31$440 000
98765Barbara Smith2020.04.142020.12.31$300 000
98765Barbara Smith2021.01.019999.12.31$330 000
1 ACCEPTED SOLUTION
Vijay_A_Verma
Super User
Super User

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZFNi8IwFEX/SiguS3gveS8fS10ODgy4FBdRC0ptO1T/P5PQ6TSjQtvdXZzTd3P3+8J5a7goi89rXYmv6lH1966NWYFSEqwEjMHHT6KSOoUVlgimBABRHMo5Aw6GIRipIRkc4yIeJLg/HqcLrKWJZ2+BLSfFR3dpxa65Pi4ZgvqlATP85w2RS3w41WLbtee5J9DPAnKcmu1C8337bTGeAPEEfjEYzAxkDPnEr5tj1YtUY3YFRr9Y8HYEMmqZAHNBtgJRVsG7YcVN6I+hD/kKcUWSSGOYHhGW8dP/n0bQI3/4AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Name = _t, #"Effective Date" = _t, #"End Date" = _t, Amount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Name", type text}, {"Effective Date", type date}, {"End Date", type date}, {"Amount", Currency.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "OIndex", 0, 1, Int64.Type),
    #"Sorted Rows" = Table.Buffer(Table.Sort(#"Added Index",{{"ID", Order.Ascending}, {"End Date", Order.Ascending}})),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"ID"}, {{"AllRows", each Table.AddIndexColumn(_,"Index",0,1)}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each let
        DummyTbl = [AllRows]
        in
        Table.AddColumn(DummyTbl, "Salary Change", each if [Index]=0 then null else [Amount]-DummyTbl[Amount]{[Index]-1})),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"AllRows"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Name", "Effective Date", "End Date", "Amount", "OIndex", "Salary Change"}, {"Name", "Effective Date", "End Date", "Amount", "OIndex", "Salary Change"}),
    #"Sorted Rows1" = Table.Sort(#"Expanded Custom",{{"OIndex", Order.Ascending}}),
    #"Removed Columns1" = Table.RemoveColumns(#"Sorted Rows1",{"OIndex"})
in
    #"Removed Columns1"

 👍 It's been a pleasure to help you | Help Hours: 11 AM to 9 PM (UTC+05:30)

How to get your questions answered quickly -- How to provide sample data

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Perfect, thanks a lot!

Vijay_A_Verma
Super User
Super User

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZFNi8IwFEX/SiguS3gveS8fS10ODgy4FBdRC0ptO1T/P5PQ6TSjQtvdXZzTd3P3+8J5a7goi89rXYmv6lH1966NWYFSEqwEjMHHT6KSOoUVlgimBABRHMo5Aw6GIRipIRkc4yIeJLg/HqcLrKWJZ2+BLSfFR3dpxa65Pi4ZgvqlATP85w2RS3w41WLbtee5J9DPAnKcmu1C8337bTGeAPEEfjEYzAxkDPnEr5tj1YtUY3YFRr9Y8HYEMmqZAHNBtgJRVsG7YcVN6I+hD/kKcUWSSGOYHhGW8dP/n0bQI3/4AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Name = _t, #"Effective Date" = _t, #"End Date" = _t, Amount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Name", type text}, {"Effective Date", type date}, {"End Date", type date}, {"Amount", Currency.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "OIndex", 0, 1, Int64.Type),
    #"Sorted Rows" = Table.Buffer(Table.Sort(#"Added Index",{{"ID", Order.Ascending}, {"End Date", Order.Ascending}})),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"ID"}, {{"AllRows", each Table.AddIndexColumn(_,"Index",0,1)}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each let
        DummyTbl = [AllRows]
        in
        Table.AddColumn(DummyTbl, "Salary Change", each if [Index]=0 then null else [Amount]-DummyTbl[Amount]{[Index]-1})),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"AllRows"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Name", "Effective Date", "End Date", "Amount", "OIndex", "Salary Change"}, {"Name", "Effective Date", "End Date", "Amount", "OIndex", "Salary Change"}),
    #"Sorted Rows1" = Table.Sort(#"Expanded Custom",{{"OIndex", Order.Ascending}}),
    #"Removed Columns1" = Table.RemoveColumns(#"Sorted Rows1",{"OIndex"})
in
    #"Removed Columns1"

 👍 It's been a pleasure to help you | Help Hours: 11 AM to 9 PM (UTC+05:30)

How to get your questions answered quickly -- How to provide sample data

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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