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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
ojhaashok
Regular Visitor

Power Query running difference

How can I calculare the running difference in "Power Query". In fact I need to do in Dataflow of fabric so asking Power Query.

 

I have column values Asset, AsOfDate, Name and Value data. I added Row ID just so my example is clear. I need to calculare Depreciare column value in Power Query. I don't know how I can do that.

The story is in 1/1/2024 someone got new laptop of $2000, and the value of laptop goes down $50 every future months. At one point value will be zero but below is small example to find a solution.

 

Row IDAssetAsOfDateNameValueDepreciate
1Laptop1/1/2024Price20000
1Laptop1/1/20242/1/2024501950
1Laptop1/1/20243/1/2024501900
1Laptop1/1/20244/1/2024501850
1Laptop1/1/20245/1/2024501800
2Desk1/1/2024Price5000
2Desk1/1/20242/1/202410490
2Desk1/1/20243/1/202410480
2Desk1/1/20244/1/202410470
2Desk1/1/20245/1/202410460
3TV1/1/2024Price15000
3TV1/1/20242/1/2024301470
3TV1/1/20243/1/2024301440
3TV1/1/20244/1/2024301410
3TV1/1/20245/1/2024301380

 

Thank you,

AK

2 ACCEPTED SOLUTIONS
ronrsnfld
Super User
Super User

In Power Query, you can use List.Generate to create a list of the values, then add that as a column. You would do this as an aggregation within  the Table.Group function.

You would also need to manually zero the first entry to get your desired result.

 

You can group by Asset eliminating the need for the row column.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kksKMkvUNJRMtQ31DcyMDIBMgOKMpNTgbSRgYGBUqwOdkVGCKYpblXGRKkyIUqVKYYql9TibOxONzXApQLJ3Ya41BgTocaECDWmGGpCwrC71xDmYHQFSM41xq7CmKAKE4IqTFFVxAIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Asset = _t, AsOfDate = _t, Name = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Asset", type text}, {"AsOfDate", type date}, {"Name", type text}, {"Value", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Asset"}, {
        
        {"Deprec", (t)=> let 
            #"Deprec List" =
                 List.Generate(
                    ()=>[d=t[Value]{0}, idx=0],
                    each [idx] < Table.RowCount(t),
                    each [d=[d]-t[Value]{[idx]+1}, idx=[idx]+1],
                    each [d]),
            #"Zero First" = List.ReplaceRange(#"Deprec List",0,1,{0}),
            #"Add as Column" = Table.FromColumns(
                Table.ToColumns(t)
                & {#"Zero First"},
                type table [Asset=nullable text, AsOfDate=nullable date, Name=nullable text, Value=nullable number, Depreciation=nullable number])
    
        in 
            #"Add as Column",
        
            type table [Asset=nullable text, AsOfDate=nullable date, Name=nullable text, Value=nullable number, Depreciation=nullable number]
    }}),
    #"Expanded Deprec" = Table.ExpandTableColumn(#"Grouped Rows", "Deprec", {"AsOfDate", "Name", "Value", "Depreciation"})
in
    #"Expanded Deprec"

  

View solution in original post

slorin
Super User
Super User

Hi, @ojhaashok 

 

another solution just with Table.NestedJoin

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kksKMkvUNJRMtQ31DcyMDIBMgOKMpNTgbSRgYGBUqwOdkVGCKYpblXGRKkyIUqVKYYql9TibOxONzXApQLJ3Ya41BgTocaECDWmGGpCwrC71xDmYHQFSM41xq7CmKAKE4IqTFFVxAIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Asset = _t, AsOfDate = _t, Name = _t, Value = _t]),
ChangedType = Table.TransformColumnTypes(Source,{{"Asset", type text}, {"AsOfDate", type date}, {"Name", type text}, {"Value", Int64.Type}}),

Month = Table.AddColumn(ChangedType, "Month", each try Date.From([Name]) otherwise null),
Join = Table.NestedJoin(Month, {"Asset", "AsOfDate"}, #"Month", {"Asset", "AsOfDate"}, "Join", JoinKind.LeftOuter),
Depreciate = Table.AddColumn(Join, "Depreciate",
(x) => x[Join]{[Name ="Price"]}[Value] - List.Sum(Table.SelectRows(x[Join], each [Month]<=x[Month])[Value]))
in
Depreciate

Stéphane 

View solution in original post

4 REPLIES 4
slorin
Super User
Super User

Hi, @ojhaashok 

 

another solution just with Table.NestedJoin

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kksKMkvUNJRMtQ31DcyMDIBMgOKMpNTgbSRgYGBUqwOdkVGCKYpblXGRKkyIUqVKYYql9TibOxONzXApQLJ3Ya41BgTocaECDWmGGpCwrC71xDmYHQFSM41xq7CmKAKE4IqTFFVxAIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Asset = _t, AsOfDate = _t, Name = _t, Value = _t]),
ChangedType = Table.TransformColumnTypes(Source,{{"Asset", type text}, {"AsOfDate", type date}, {"Name", type text}, {"Value", Int64.Type}}),

Month = Table.AddColumn(ChangedType, "Month", each try Date.From([Name]) otherwise null),
Join = Table.NestedJoin(Month, {"Asset", "AsOfDate"}, #"Month", {"Asset", "AsOfDate"}, "Join", JoinKind.LeftOuter),
Depreciate = Table.AddColumn(Join, "Depreciate",
(x) => x[Join]{[Name ="Price"]}[Value] - List.Sum(Table.SelectRows(x[Join], each [Month]<=x[Month])[Value]))
in
Depreciate

Stéphane 

dufoq3
Super User
Super User

Hi @ojhaashok, anothere solution (similar to @ronrsnfld, but with List.Accumulate)

 

dufoq3_0-1718268531950.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kksKMkvUNJRMtQ31DcyMDIBMgOKMpNTgbSRgYGBUqwOdkVGCKYpblXGRKkyIUqVKYYql9TibOxONzXApQLJ3Ya41BgTocaECDWmGGpCwrC71xDmYHQFSM41xq7CmKAKE4IqTFFVxAIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Asset = _t, AsOfDate = _t, Name = _t, Value = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Asset", type text}, {"AsOfDate", type date}, {"Name", type text}, {"Value", Int64.Type}}),

    fn_Depreciate = 
        (myTable as table)=>
        let
            // Tbl = GroupedRows{[Asset="Laptop"]}[All],
            Tbl = myTable,
            ValueBuffer = List.Buffer(Tbl[Value]),
            AccumulatedDepreciate = List.Accumulate(
                { 0..List.Count(ValueBuffer)-1 },
                {},
                (s,c)=> s & { if c = 0 then 0 else if c = 1 then ValueBuffer{0} - ValueBuffer{c} else s{c-1} - ValueBuffer{c} } ),
            ToTable = Table.FromColumns(Table.ToColumns(Tbl) & {AccumulatedDepreciate}, Value.Type(Tbl & #table(type table[Depreciate = number],{})))
        in
            ToTable,

    GroupedRows = Table.Group(ChangedType, {"Asset"}, {{"All", fn_Depreciate, type table}}),
    CombinedAll = Table.Combine(GroupedRows[All])
    in 
        CombinedAll

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Anonymous
Not applicable

Hi @ojhaashok ,

@ronrsnfld Thanks for your concern about this case!
Another solution:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jc+rCoBAEIXhd9ksOJedNzAaDGKRDSIGMSjq++MUUdl1mXT+8JXT9w5d4ephO9dNA0ssCchrNvs8TroEAC4U/5CelLxks/RmKZEkzWo6lvQdgZx6fcGcY6PzRieRY822S3/A+0QKvS7wv2KT8iYlXxUu", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Row ID" = _t, Asset = _t, AsOfDate = _t, Name = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Row ID", Int64.Type}, {"Asset", type text}, {"AsOfDate", type date}, {"Name", type text}, {"Value", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Row ID"}, {{"Count", each Table.AddIndexColumn(_, "Index", 1, 1, Int64.Type)}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Asset", "AsOfDate", "Name", "Value", "Index"}, {"Asset", "AsOfDate", "Name", "Value", "Index"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Count", "Custom", each 
    let 
        currentID = [Row ID],
        currentIndex = [Index]
    in
        Table.SelectRows(#"Expanded Count", each ([Row ID] = currentID and [Name] = "Price")){0}[Value] -
        (if [Name] = "Price" then 
        [Value]
        else 
        List.Sum(
            Table.SelectRows(
                #"Expanded Count",
                each [Row ID] = currentID and [Index] <= currentIndex and [Index] <> 1
            )[Value]
        )))
in
    #"Added Custom"

And the final output is as below:

vjunyantmsft_0-1718242271615.png


Best Regards,
Dino Tao
If this post helps, then please consider Accept both of the answers as the solution to help the other members find it more quickly.

ronrsnfld
Super User
Super User

In Power Query, you can use List.Generate to create a list of the values, then add that as a column. You would do this as an aggregation within  the Table.Group function.

You would also need to manually zero the first entry to get your desired result.

 

You can group by Asset eliminating the need for the row column.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kksKMkvUNJRMtQ31DcyMDIBMgOKMpNTgbSRgYGBUqwOdkVGCKYpblXGRKkyIUqVKYYql9TibOxONzXApQLJ3Ya41BgTocaECDWmGGpCwrC71xDmYHQFSM41xq7CmKAKE4IqTFFVxAIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Asset = _t, AsOfDate = _t, Name = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Asset", type text}, {"AsOfDate", type date}, {"Name", type text}, {"Value", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Asset"}, {
        
        {"Deprec", (t)=> let 
            #"Deprec List" =
                 List.Generate(
                    ()=>[d=t[Value]{0}, idx=0],
                    each [idx] < Table.RowCount(t),
                    each [d=[d]-t[Value]{[idx]+1}, idx=[idx]+1],
                    each [d]),
            #"Zero First" = List.ReplaceRange(#"Deprec List",0,1,{0}),
            #"Add as Column" = Table.FromColumns(
                Table.ToColumns(t)
                & {#"Zero First"},
                type table [Asset=nullable text, AsOfDate=nullable date, Name=nullable text, Value=nullable number, Depreciation=nullable number])
    
        in 
            #"Add as Column",
        
            type table [Asset=nullable text, AsOfDate=nullable date, Name=nullable text, Value=nullable number, Depreciation=nullable number]
    }}),
    #"Expanded Deprec" = Table.ExpandTableColumn(#"Grouped Rows", "Deprec", {"AsOfDate", "Name", "Value", "Depreciation"})
in
    #"Expanded Deprec"

  

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.