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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
caseski
Helper I
Helper I

Difference between rows by group

Hello I have a column that records the progressive incremental value associated with an attribute. I would like to calculate the difference between the value of two rows to find the total value for each day and of each attribute.
The simplified example of the table is as follows.

 

The 5th column [kw] is the difference between two consecutive rows of the same attribute.

 

The target is to calculate the kw consumed every day by each equipment

 

IDEquipmentDateHourStopkwEndkw
1412/1/24 10.50125789 
2414/1/24 12.521288823093
3511/1/24 15.185241 
4412/1/24 15.181328543972
5613/1/24 15.1821521 
6614/1/24 15.18243642843
7615/1/24 15.18284574093
8416/1/24 15.181384525598
9617/1/24 15.18302141757
10718/1/24 15.1822542 
11719/1/24 15.1823452910
12620/1/24 15.18312451031
1 ACCEPTED SOLUTION

you mean row 4?

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XdDBDcUgDAPQVb44Vy02CaSzoO6/xi8BDs2BA9HDBnpPSEeSd4EXLsoP+dTse212p+foiZvIIjyVTsyMTsq71THCInrCxowCBxIzFkChqTgZ56uPvoRQzpC6RQihlDoz2hYahIk2F7YvUuNFXjIfc++Q9iUlE7MG44dGFyz0UGWGAJvcgZTdA64i5lAEiqbn+QM=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Equipment = _t, DateHourStop = _t, kwEnd = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Equipment", Int64.Type}, {"DateHourStop", type text}, {"kwEnd", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Equipment"}, {{"Rows", each Table.AddColumn(_, "kw", (k)=> try k[kwEnd]-Table.Last(Table.SelectRows(_, each [DateHourStop]<k[DateHourStop]))[kwEnd] otherwise null,Int64.Type), type table [ID=nullable number, Equipment=nullable number, DateHourStop=nullable text, kwEnd=nullable number, kw=nullable number]}}),
    #"Expanded Rows" = Table.ExpandTableColumn(#"Grouped Rows", "Rows", {"ID", "DateHourStop", "kwEnd", "kw"}, {"ID", "DateHourStop", "kwEnd", "kw"})
in
    #"Expanded Rows"

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

3 REPLIES 3
lbendlin
Super User
Super User

Your sample data is inconsistent for equipment 4 (rows 2 and 4).  Please check.

Yes sorry (the table is more complicated, this is just a sample) the date of rows 2 is 14/01. Thanks

you mean row 4?

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XdDBDcUgDAPQVb44Vy02CaSzoO6/xi8BDs2BA9HDBnpPSEeSd4EXLsoP+dTse212p+foiZvIIjyVTsyMTsq71THCInrCxowCBxIzFkChqTgZ56uPvoRQzpC6RQihlDoz2hYahIk2F7YvUuNFXjIfc++Q9iUlE7MG44dGFyz0UGWGAJvcgZTdA64i5lAEiqbn+QM=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Equipment = _t, DateHourStop = _t, kwEnd = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Equipment", Int64.Type}, {"DateHourStop", type text}, {"kwEnd", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Equipment"}, {{"Rows", each Table.AddColumn(_, "kw", (k)=> try k[kwEnd]-Table.Last(Table.SelectRows(_, each [DateHourStop]<k[DateHourStop]))[kwEnd] otherwise null,Int64.Type), type table [ID=nullable number, Equipment=nullable number, DateHourStop=nullable text, kwEnd=nullable number, kw=nullable number]}}),
    #"Expanded Rows" = Table.ExpandTableColumn(#"Grouped Rows", "Rows", {"ID", "DateHourStop", "kwEnd", "kw"}, {"ID", "DateHourStop", "kwEnd", "kw"})
in
    #"Expanded Rows"

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
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.