Forum Discussion
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.
- Anonymous4 years ago
Hi rashidanwar ,
You could understand by looking at his every step.
1.This is what the dataset looked like initially.
2.Here he creates a function that will be used later.
3.In this step, he groups the original table by call_id.
4.In this step, he adds a custom column that uses the previously created function.
5.Expand the fxProcessDelta column.
6.Remove the Temp column.
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.
3 Replies
- Vijay_A_Verma
Most Valuable Professional
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"- rashidanwar
Advocate II
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.
- AnonymousNot applicable
Hi rashidanwar ,
You could understand by looking at his every step.
1.This is what the dataset looked like initially.
2.Here he creates a function that will be used later.
3.In this step, he groups the original table by call_id.
4.In this step, he adds a custom column that uses the previously created function.
5.Expand the fxProcessDelta column.
6.Remove the Temp column.
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.