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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
Anonymous
Not applicable

Subtract two rows based on condition

Hello, 

 

I am trying to subtract two consecutive rows from each other (Power) based on a condition from another column (Day No.) to yield a new value (Power_2) as shown below. However, once a new Day No. is reached, the first value of that day will remain the same and be copied into the Power_2 column. 

Capture.PNG

 

Thanks

1 ACCEPTED SOLUTION
Mariusz
Community Champion
Community Champion

Hi @Anonymous 

Hope this works for you.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("RcqxDcAwDAPBXVirsKjYyxjaf40EQoDvrrh7lQrlUsfPhIY1NNfc4YZnWB+9YELDgo+6Xw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Day = _t, Power = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Day", Int64.Type}, {"Power", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Power_2", each let _day = [Day], _power = [Power] 
        in [Power] - Replacer.ReplaceValue( List.Max( Table.SelectRows( #"Changed Type", each [Day] = _day and  [Power] < _power )[Power]), null, 0 ), Int64.Type )
in
    #"Added Custom"

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
Mariusz Repczynski

 

View solution in original post

1 REPLY 1
Mariusz
Community Champion
Community Champion

Hi @Anonymous 

Hope this works for you.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("RcqxDcAwDAPBXVirsKjYyxjaf40EQoDvrrh7lQrlUsfPhIY1NNfc4YZnWB+9YELDgo+6Xw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Day = _t, Power = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Day", Int64.Type}, {"Power", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Power_2", each let _day = [Day], _power = [Power] 
        in [Power] - Replacer.ReplaceValue( List.Max( Table.SelectRows( #"Changed Type", each [Day] = _day and  [Power] < _power )[Power]), null, 0 ), Int64.Type )
in
    #"Added Custom"

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
Mariusz Repczynski

 

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

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

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

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