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
rashidanwar
Advocate II
Advocate II

Running change/delta per category

Hi 

Hope that every one is doing good.

I have a data table that looks like as follow

call_id             type               time
12                   A                    Date, HH:mm:ss
12                   B                    Date, HH:mm:ss
12                   C                    Date, HH:mm:ss
13                   A                    Date, HH:mm:ss
13                   B                    Date, HH:mm:ss
13                   C                    Date, HH:mm:ss
14                   D                    Date, HH:mm:ss

I actually want to add a 4th column that would be the running delta or you can say it running change. It will take the value of the next record in the time field and will deduct the value of the previous record in the time field. But this would be per call id, that is actually the category. Please let me know any solution that can be implemented in Power Query or Dax in Power BI.

I would appreciate for any help in this regard.

Thank you so much.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @rashidanwar ,

 

You could understand by looking at his every step.

1.This is what the dataset looked like initially.

vstephenmsft_1-1649401630215.png

 

2.Here he creates a function that will be used later.

7.png

 

3.In this step, he groups the original table by call_id.

8.png

 

4.In this step, he adds a custom column that uses the previously created function.

9.png

 

5.Expand the fxProcessDelta column.

10.png

 

6.Remove the Temp column.

11.png

 

Best Regards,

Stephen Tao

 

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

3 REPLIES 3
Vijay_A_Verma
Super User
Super User

Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc5LDoAgDATQqxjWJkwHAe3Ozy0I97+GjZIQcdHVvMy0FCd0s9vtEDyTJ8hJoCJqSZ0bOAYQVTZF7uBsIL8A8jSsLwj/CUCxqaCDcSIpDeQOxglav7I1LBZeX2AP2BvRQL0B", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [call_id = _t, #"type" = _t, time = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"call_id", Int64.Type}, {"type", type text}, {"time", type datetime}}),
    // Function Start
    fxProcessDelta=(Tbl)=>
        let
            #"Added Index" = Table.AddIndexColumn(Tbl, "Index", 0, 1, Int64.Type),
            #"Added Custom" = Table.AddColumn(#"Added Index", "Delta", each if [Index]=0 then null else [time]-#"Added Index"[time]{[Index]-1})
        in
            #"Added Custom",
    // Function End
    #"Grouped Rows" = Table.Group(#"Changed Type", {"call_id"}, {{"Temp", each _, type table [call_id=nullable number, type=nullable text, time=nullable datetime]}}),
    #"Invoked Custom Function" = Table.AddColumn(#"Grouped Rows", "fxProcessDelta", each fxProcessDelta([Temp])),
    #"Expanded fxProcessDelta" = Table.ExpandTableColumn(#"Invoked Custom Function", "fxProcessDelta", {"type", "time", "Delta"}, {"type", "time", "Delta"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded fxProcessDelta",{"Temp"})
in
    #"Removed Columns"

 

Thank you so much @Vijay_A_Verma for your time and support.  Can we have a more clean solution without having too much code? All I know that beauty of power query is to do the data transformation with out much coding. 

Anonymous
Not applicable

Hi @rashidanwar ,

 

You could understand by looking at his every step.

1.This is what the dataset looked like initially.

vstephenmsft_1-1649401630215.png

 

2.Here he creates a function that will be used later.

7.png

 

3.In this step, he groups the original table by call_id.

8.png

 

4.In this step, he adds a custom column that uses the previously created function.

9.png

 

5.Expand the fxProcessDelta column.

10.png

 

6.Remove the Temp column.

11.png

 

Best Regards,

Stephen Tao

 

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

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.

Top Solution Authors