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! Request now

Reply
JohannesBier
Helper I
Helper I

Difference between two line entries with the same ID

I would like to add a custom column that calculates the difference between two rows. I have a column ValueMax and would like to have a ValueColumn that calculates the difference to the previous ValueMax. Is this possible in PowerQuery?

 

Thank you!

 

Best regards

Johannes

2 ACCEPTED SOLUTIONS
AlienSx
Super User
Super User

let
    s = your_table,
    rows = List.Buffer(Table.ToRecords(s)),
    add_col = List.Generate(
        () => [i = 0, val = rows{0} & [ValueColumn = 0]],
        (x) => rows{x[i]}? <> null,
        (x) => [i = x[i] + 1, val = rows{i} & [ValueColumn = rows{i}[ValueMax] - x[val][ValueMax]]],
        (x) => x[val]
    ),
    z = Table.FromRecords(add_col)
in
    z

View solution in original post

Anonymous
Not applicable

Hi @JohannesBier 

You can refer to the following solution.

Sample data

vxinruzhumsft_0-1701140887174.png

First step

Add index column group by the id and then expand it., you can refer to the following code or the video.

Insert a new step

vxinruzhumsft_1-1701141056463.png

= Table.Group(#"Changed Type"(the last step name), {"ID"}, {{"Data", each Table.AddIndexColumn(_,"Index",1,1),type table}})

Create Row Number for Each Group in Power BI using Power Query - RADACAD

Second Step:Add a custom column to calculate the difference

=[ValueMax]-List.Min(Table.SelectRows(#"Expanded Data"(last step name),(x)=>x[ID]=[ID] and x[Index]=[Index]-1)[ValueMax])

Output

vxinruzhumsft_2-1701141251267.png

 

And you can put the following code to advanved editor in power query to refer to 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTI0UIrVgTCNEExjBNMEwjQCMk0RTCRRMwTTHMG0ADJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, ValueMax = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"ValueMax", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"Data", each Table.AddIndexColumn(_,"Index",1,1),type table}}),
    #"Expanded Data" = Table.ExpandTableColumn(#"Grouped Rows", "Data", {"ValueMax", "Index"}, {"ValueMax", "Index"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Data", "Difference", each [ValueMax]-List.Min(Table.SelectRows(#"Expanded Data",(x)=>x[ID]=[ID] and x[Index]=[Index]-1)[ValueMax]))
in
    #"Added Custom"

Best Regards!

Yolo Zhu

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

4 REPLIES 4
JohannesBier
Helper I
Helper I

Can you explain it here?

 

JohannesBier_0-1700661892464.png

 

Anonymous
Not applicable

Hi @JohannesBier 

You can refer to the following solution.

Sample data

vxinruzhumsft_0-1701140887174.png

First step

Add index column group by the id and then expand it., you can refer to the following code or the video.

Insert a new step

vxinruzhumsft_1-1701141056463.png

= Table.Group(#"Changed Type"(the last step name), {"ID"}, {{"Data", each Table.AddIndexColumn(_,"Index",1,1),type table}})

Create Row Number for Each Group in Power BI using Power Query - RADACAD

Second Step:Add a custom column to calculate the difference

=[ValueMax]-List.Min(Table.SelectRows(#"Expanded Data"(last step name),(x)=>x[ID]=[ID] and x[Index]=[Index]-1)[ValueMax])

Output

vxinruzhumsft_2-1701141251267.png

 

And you can put the following code to advanved editor in power query to refer to 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTI0UIrVgTCNEExjBNMEwjQCMk0RTCRRMwTTHMG0ADJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, ValueMax = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"ValueMax", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"Data", each Table.AddIndexColumn(_,"Index",1,1),type table}}),
    #"Expanded Data" = Table.ExpandTableColumn(#"Grouped Rows", "Data", {"ValueMax", "Index"}, {"ValueMax", "Index"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Data", "Difference", each [ValueMax]-List.Min(Table.SelectRows(#"Expanded Data",(x)=>x[ID]=[ID] and x[Index]=[Index]-1)[ValueMax]))
in
    #"Added Custom"

Best Regards!

Yolo Zhu

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

 

@JohannesBier you don't need to add custom column. My code modifies your initial table in to the one with new column as per your instructions. Please watch this video to know how to incorporate this code. 

AlienSx
Super User
Super User

let
    s = your_table,
    rows = List.Buffer(Table.ToRecords(s)),
    add_col = List.Generate(
        () => [i = 0, val = rows{0} & [ValueColumn = 0]],
        (x) => rows{x[i]}? <> null,
        (x) => [i = x[i] + 1, val = rows{i} & [ValueColumn = rows{i}[ValueMax] - x[val][ValueMax]]],
        (x) => x[val]
    ),
    z = Table.FromRecords(add_col)
in
    z

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

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!

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.