Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Hello:
I am writing to ask if it is possible to create a calculated column which calculates the time difference ( Duration ) between the next rowset ( timestamp ) ? Please see example below. Thank you for your time.
error num | error text | category num | category text | timestamp | time no millis | Index | short name | Line Name | IndexAll | Duration (Minutes) |
3030 | S010 MACHINE IS RUNNING | 7 | MACHINE RUNNING | 3/3/2021 16:20 | 3/3/2021 16:20 | 368 | CX482 S010 | CX482 BASELINE ONE | 368 | 7 |
3000 | S010 MACHINE IS IDLE | 2 | MACHINE IDLE | 3/3/2021 16:27 | 3/3/2021 16:27 | 369 | CX482 S010 | CX482 BASELINE ONE | 369 | 3 |
3030 | S010 MACHINE IS RUNNING | 7 | MACHINE RUNNING | 3/3/2021 16:30 | 3/3/2021 16:30 | 370 | CX482 S010 | CX482 BASELINE ONE | 370 | 13 |
3000 | S010 MACHINE IS IDLE | 2 | MACHINE IDLE | 3/3/2021 16:43 | 3/3/2021 16:43 | 371 | CX482 S010 | CX482 BASELINE ONE | 371 | 0 |
3030 | S010 MACHINE IS RUNNING | 7 | MACHINE RUNNING | 3/3/2021 16:43 | 3/3/2021 16:43 | 372 | CX482 S010 | CX482 BASELINE ONE | 372 | 40 |
3000 | S010 MACHINE IS IDLE | 2 | MACHINE IDLE | 3/3/2021 17:23 | 3/3/2021 17:23 | 373 | CX482 S010 | CX482 BASELINE ONE | 373 | 9 |
3030 | S010 MACHINE IS RUNNING | 7 | MACHINE RUNNING | 3/3/2021 17:32 | 3/3/2021 17:32 | 374 | CX482 S010 | CX482 BASELINE ONE | 374 | 18 |
549 | +S011 ERROR FIXTURE 1 CYLINDER 17.3 NOT REVERSE | 1 | MACHINE ERRORS | 3/3/2021 17:50 | 3/3/2021 17:50 | 375 | CX482 S010 | CX482 BASELINE ONE | 375 |
Solved! Go to Solution.
Hello @Anonymous
if you need a solution in Power Query then you can add a custom column with this formula. You have to replace the "PreviousStep" with the name of your previous step. Be aware that i use your index column for referencing the "next" step. Here the formula
try Duration.TotalMinutes(Table.SelectRows(PreviousStep, (sel)=> sel[Index] = [Index]+1)[timestamp]{0} - [timestamp]) otherwise null
here a complete code example to understand it better
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rZLRagIxEEV/ZdjXik5msju6b7qmNWCzkGixyP7/b5jYbbUSIbA+JJCTmcnhkvO5YmSsZlVAhfC57nbWGbAB/NE56z7ijcT1e3GjvOAFISlQTUuYBc0y7t1JLwnS9L/DZh3MPk3rnRnrhlkSwZyI3e5TFd1ZjOjfi5IFzapQYTUqTM6CH7P4AYJlIrFuchaas0BUoYJ6VRbPRKhQhKZmIS1xFggXKvBrspCWKQtEF4roq0it04d+i6UKjPe9h3d7Ohy9AQXdd2zYGh+HzxlcfwBvvowPqV/d+V37woNNjVkgdaFeXQ3DBQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"rror num" = _t, #"error text" = _t, #"category num" = _t, #"category text" = _t, timestamp = _t, #"time no millis" = _t, Index = _t, #"short name" = _t, #"Line Name" = _t, IndexAll = _t]),
PreviousStep = Table.TransformColumnTypes(Source,{{"rror num", Int64.Type}, {"error text", type text}, {"category num", Int64.Type}, {"category text", type text}, {"timestamp", type datetime}, {"time no millis", type datetime}, {"Index", Int64.Type}, {"short name", type text}, {"Line Name", type text}, {"IndexAll", Int64.Type}}),
#"Added Custom" = Table.AddColumn(PreviousStep, "Custom", each try Duration.TotalMinutes(Table.SelectRows(PreviousStep, (sel)=> sel[Index] = [Index]+1)[timestamp]{0} - [timestamp]) otherwise null)
in
#"Added Custom"
output
Copy paste this code to the advanced editor in a new blank query to see how the solution works.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy
Hello @Anonymous
if you need a solution in Power Query then you can add a custom column with this formula. You have to replace the "PreviousStep" with the name of your previous step. Be aware that i use your index column for referencing the "next" step. Here the formula
try Duration.TotalMinutes(Table.SelectRows(PreviousStep, (sel)=> sel[Index] = [Index]+1)[timestamp]{0} - [timestamp]) otherwise null
here a complete code example to understand it better
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rZLRagIxEEV/ZdjXik5msju6b7qmNWCzkGixyP7/b5jYbbUSIbA+JJCTmcnhkvO5YmSsZlVAhfC57nbWGbAB/NE56z7ijcT1e3GjvOAFISlQTUuYBc0y7t1JLwnS9L/DZh3MPk3rnRnrhlkSwZyI3e5TFd1ZjOjfi5IFzapQYTUqTM6CH7P4AYJlIrFuchaas0BUoYJ6VRbPRKhQhKZmIS1xFggXKvBrspCWKQtEF4roq0it04d+i6UKjPe9h3d7Ohy9AQXdd2zYGh+HzxlcfwBvvowPqV/d+V37woNNjVkgdaFeXQ3DBQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"rror num" = _t, #"error text" = _t, #"category num" = _t, #"category text" = _t, timestamp = _t, #"time no millis" = _t, Index = _t, #"short name" = _t, #"Line Name" = _t, IndexAll = _t]),
PreviousStep = Table.TransformColumnTypes(Source,{{"rror num", Int64.Type}, {"error text", type text}, {"category num", Int64.Type}, {"category text", type text}, {"timestamp", type datetime}, {"time no millis", type datetime}, {"Index", Int64.Type}, {"short name", type text}, {"Line Name", type text}, {"IndexAll", Int64.Type}}),
#"Added Custom" = Table.AddColumn(PreviousStep, "Custom", each try Duration.TotalMinutes(Table.SelectRows(PreviousStep, (sel)=> sel[Index] = [Index]+1)[timestamp]{0} - [timestamp]) otherwise null)
in
#"Added Custom"
output
Copy paste this code to the advanced editor in a new blank query to see how the solution works.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy
Jimmy: Thank you very much for the help
Hey
Try using this DAX in your calculate column:
DifferenceColumn = var nextIndex = SampleTable[Index] + 1
var nextdate = CALCULATE(VALUES(SampleTable[timestamp]),FILTER(All(SampleTable),SampleTable[Index]=nextIndex))
return DATEDIFF(SampleTable[timestamp],nextdate,MINUTE)
It gives you the diiference column as expected:
Refer this thread for more ways to do it:
https://community.powerbi.com/t5/Desktop/Difference-between-two-rows/m-p/291588
Thanks
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.