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

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
umairarshad
Helper II
Helper II

Running Difference between rows

hello all,

For the following data, I want to have running difference between consective rows.

Please help me out in this regard.

like

83-5780

120-83

16989-120

629-16989

and so on.

umairarshad_0-1721358576897.png

 

Regards,

 

1 ACCEPTED SOLUTION
BA_Pete
Super User
Super User

Hi @umairarshad ,

 

Try this example code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjW3MFCK1YlWsjAGU4ZGEK6hmaWFJZhlZgShjQzMjCAqTcCUsamFUmwsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Running Hours" = _t]),
    chgTypes = Table.TransformColumnTypes(Source,{{"Running Hours", Int64.Type}}),

// Relevant steps from here ===>
    addIndex0 = Table.AddIndexColumn(chgTypes, "Index", 0, 1, Int64.Type),
    addRunningDiff = Table.AddColumn(addIndex0, "RunningDiff", each try [Running Hours] - addIndex0[Running Hours]{[Index] - 1} otherwise null)
in
    addRunningDiff

 

Summary:

First, you need to sort your table so the numbers are in the order you need them to be for subtraction.

addIndex0 = Add an index column starting from zero. This allows us to reference this as a proxy for row number.

addRunningDiff = Deduct prior row value using previous step and [Index] value minus 1 from each row value.

 

Example output:

BA_Pete_0-1721405103628.png

 

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

3 REPLIES 3
umairarshad
Helper II
Helper II

Great. It worked. many thanks

Ahmedx
Super User
Super User

pls try this

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTI1tzBQitWBcCyM4UxDI4SwoZmlhSWY5wTkmRkh2EYGZkZwjoUJnGlsaqEUGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Hours = _t, #"Running Hours" = _t]),
    Types = Table.TransformColumnTypes(Source,{{"Running Hours", Int64.Type}}),
    f= (x)=> [
      nametblcolumn = Table.ColumnNames(x),
    a= Table.ToColumns(x),
    b = a&{{null}&List.RemoveLastN(a{1},1)},
    c = Table.FromColumns(b, nametblcolumn&{"NewColumn"}),
    d= Table.RemoveColumns( Table.ReplaceValue(c,each [Running Hours],each [Running Hours]-[NewColumn],Replacer.ReplaceValue,{"Running Hours", "NewColumn"}),{"NewColumn"})][d],

    final = f(Types)
in
    final
BA_Pete
Super User
Super User

Hi @umairarshad ,

 

Try this example code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjW3MFCK1YlWsjAGU4ZGEK6hmaWFJZhlZgShjQzMjCAqTcCUsamFUmwsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Running Hours" = _t]),
    chgTypes = Table.TransformColumnTypes(Source,{{"Running Hours", Int64.Type}}),

// Relevant steps from here ===>
    addIndex0 = Table.AddIndexColumn(chgTypes, "Index", 0, 1, Int64.Type),
    addRunningDiff = Table.AddColumn(addIndex0, "RunningDiff", each try [Running Hours] - addIndex0[Running Hours]{[Index] - 1} otherwise null)
in
    addRunningDiff

 

Summary:

First, you need to sort your table so the numbers are in the order you need them to be for subtraction.

addIndex0 = Add an index column starting from zero. This allows us to reference this as a proxy for row number.

addRunningDiff = Deduct prior row value using previous step and [Index] value minus 1 from each row value.

 

Example output:

BA_Pete_0-1721405103628.png

 

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

February Power BI Update Carousel

Power BI Monthly Update - February 2026

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

Top Solution Authors