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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Calculating the difference in time where time is in the same column

I am trying to calculate the time difference between steps of a process. This process isn't always 100% clean. A step 1 can generate 4 step 2s for each step 2 they should get a step 3 and 4. I want to know per SerialNumber the difference of time between step 1 and step 2, then I want to average that. NOTE there could be cases where step 2 hasn't generated. 

I feel like I might have to transpose this into a horizontal table, but I really don't want to do that. 

jzwhear_0-1629377879148.png

 

12 REPLIES 12
Anonymous
Not applicable

Ok, new approach:  Let's say your last step is named LastStep. I would add a new step named "SelectList". In the formula bar, type:

 

= LastStep[TimestampID]

Then add a new step named AddDurations, and in the formula bar, type:

 

= Table.AddColumn(LastStep, "Durations", each if StepID > 1 then [TimestampID] - List.Max(List.Select(SelectList, each _ < [TimestampID])) else null)


--Nate

 

 

 

 

Anonymous
Not applicable

I would add an index column that starts from 1, then add:

 

= let PriorTime = List.Max(Table.SelectRows(PriorStep, each Table.Range(_, 0, [Index]))[Timestamp]) in Table.AddColumn(PriorStep, "Durations", each if StepID > 1 then [Timestamp] - PriorTime else null)

 

--Nate

Keep in mind that there are different serial numbers.

Anonymous
Not applicable

That being so, after adding the function, you can group by SerialNumber, and use the "Average" aggregation for the Durations column. 

--Nate

The serial numbers have different phase 1 timestamps. You can only compute the difference between phase 2 and phase 1 for an individual serial number.

lbendlin
Super User
Super User

Please provide sample data in usable format (not as a picture - maybe insert into a table?) and show the expected outcome.

Anonymous
Not applicable

SerialNumber, StepID, ClassRecut, SerialNumber, UserID, TimestampID,

AAD1222121M0206 AAD1222121M02168/9/2021 11:05:49 PM79
JAC3121721M2346 JAC3121721M23518/9/2021 9:04:43 PM68
AAD1222121M0209  68/9/2021 11:06:31 PM82
AAD1222121M0208  68/9/2021 11:06:19 PM81
AAD1222121M0207  68/9/2021 11:06:06 PM80
AAD1222121M0205CC 68/9/2021 11:05:21 PM78
AAD1222121M0204CC 68/9/2021 11:04:47 PM77
AAD1222121M0203CC 68/9/2021 11:04:23 PM76
AAD1222121M0202CC 68/9/2021 11:04:00 PM75
AAD1222121M0201  68/9/2021 11:03:39 PM74
JAC3121721M2373CC 18/18/2021 2:39:59 PM93
JAC3121721M2372CC 18/18/2021 2:39:18 PM92
JAC3121721M2372S 18/18/2021 2:39:05 PM91
JAC3121721M2371  18/18/2021 2:38:07 PM90
JAC3121721M2354CC 18/16/2021 4:38:50 PM89
JAC3121721M2353CC 18/10/2021 2:02:29 PM88
JAC3121721M2352U 18/10/2021 2:02:27 PM87
JAC3121721M2352K 18/10/2021 2:02:25 PM86
JAC3121721M2352S 18/10/2021 2:02:16 PM85
JAC3121721M2352CC 18/10/2021 2:02:06 PM84
JAC3121721M2351  18/10/2021 2:01:45 PM83
JAC3121721M2362CC 18/9/2021 9:08:37 PM73
JAC3121721M2361  18/9/2021 9:08:29 PM72
JAC3121721M2349  18/9/2021 9:05:09 PM71
JAC3121721M2348  18/9/2021 9:05:02 PM70
JAC3121721M2347  18/9/2021 9:04:54 PM69
JAC3121721M2345U 18/9/2021 9:03:47 PM67
JAC3121721M2344S 18/9/2021 9:03:40 PM66
JAC3121721M2345K 18/9/2021 9:03:30 PM65
JAC3121721M2345S 18/9/2021 9:03:27 PM64
JAC3121721M2345CC 18/9/2021 9:03:20 PM63
JAC3121721M2344U 18/9/2021 9:03:17 PM62
JAC3121721M2344K 18/9/2021 9:03:16 PM61
JAC3121721M2344CC 18/9/2021 9:03:09 PM60
JAC3121721M2343U 18/9/2021 9:03:00 PM59
JAC3121721M2343K 18/9/2021 9:02:44 PM58
JAC3121721M2343S 18/9/2021 9:02:35 PM57
JAC3121721M2343CC 18/9/2021 9:02:30 PM56
JAC3121721M2342U 18/9/2021 9:02:23 PM55
JAC3121721M2342K 18/9/2021 9:02:14 PM54
JAC3121721M2342S 18/9/2021 9:02:06 PM53
JAC3121721M2342CC 18/9/2021 9:01:56 PM52
JAC3121721M2341  18/9/2021 9:01:41 PM51
CAW3218321M1842U 18/9/2021 4:20:45 PM50
CAW3218321M1842K 18/9/2021 4:19:45 PM49
CAW3218321M1842S 18/9/2021 4:19:38 PM48
CAW3218321M1842CC 18/9/2021 4:19:28 PM47
CAW3218321M1841  18/9/2021 4:19:08 PM46

 

I need to turn this into this, 

 

jzwhear_0-1629458725754.png

I can do the aggerate things easily, but the time calculations where the time is in the same row and it can branch based on the class is very complicated for me. 

 

Thank you for providing the sample data.

 

You cannot have two columns with the same name.  What's the difference between columns 1 and 4?

What's the purpose of the last column? Looks like an index (which would be fantastic)

Anonymous
Not applicable

I made a mistake in seperating the column names it should be, 

 

SerialNumber, StepID, Class, RecutSerialNumber, UserID, TimestampID,

 

Sorry!

Here is a query that shows average duration between step 1 and all steps 2 of each serialnumber. The sorting step is not required, and should be removed to improve performance.

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hZY9bsMwDIWvYnguUJEiJVlbkE4tAhQIig5B73+NKjGZOBapDB5i5xN/9Piky2U+HD4AEQHhFDDMb3Nqz9Sepw8gH8r78o7t5wRQA1dapu9Te52X+e/tMn8ejrH9PzcEI23WevrA7QVs11pqoEpxXSqV21JdWossNVmZpBphxQvaeBnjIIUUsPE8xkMSPNj4teTj0eO5omSfneJpyLfmZeGzzccXPErzc7J5fMGHIDzbPIzaF2tUGVEvo7xPf5UOlHUBbHBl4Zdo8/iChyI8+vzZxwMLDja+rb7DSw2yeUvocd5vvvBp5enKszS/GEPITveCxg9YUbVfbP5a/c8Al/RL9vGvAS7NK8nHzz4OOnns46Pi75NrSE+NavJwqKTZG8pLdviH6ZUadW4dvI++pXXjsiFb2lnmnuYalDZUSzvHNGgU2hAt7QyzN3smMXvn3OBOcg863t0uGYojGZizT8u4JENwGvvLpaPSht6U9mPrrCRDbUq7cmm4BjfkooX7bQMN7siFhoXroCVHLoZLbXFVW3L0Eoep6/nCjl7iIHWsJGpjw+CU9vasObSMODtqM+z1CdfUHbn17rql9WBmR269uW5p0MIdufXeuqXVGtlR29DboLLijtxG3taMVW5EvMrtePiNCKU9JyjDtrXLTLjbMgefNttG7SZ4p2nxabNtNzrKZYKKT9ttu+GoeLZxt203Oijd1Pb3Dw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [SerialNumber = _t, StepID = _t, Class = _t, RecutSerialNumber = _t, UserID = _t, Timestamp = _t, V = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"V", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"V", Order.Ascending}}),
    #"Filtered Rows" = Table.SelectRows(#"Sorted Rows", each ([StepID] = "2")),
    #"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows",{"SerialNumber", "StepID", "Timestamp"}),
    #"Added Custom" = Table.AddColumn(#"Removed Other Columns", "FirstRow", each Table.SelectRows(Source,(k)=> k[SerialNumber]=[SerialNumber] and k[StepID]="1")[Timestamp]{0}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Timestamp", type datetime}, {"FirstRow", type datetime}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type1", "Duration", each Duration.Seconds([Timestamp]-[FirstRow])),
    #"Grouped Rows" = Table.Group(#"Added Custom1", {"SerialNumber"}, {{"Average Duration", each List.Average([Duration]), type number}})
in
    #"Grouped Rows"

 

What's the purpose of the last column? Looks like an index (which would be fantastic)

Anonymous
Not applicable

It is, I can also add an extra index if needed. I might have sorted the columns by something else before pasting, and I cutoff some extra data to keep it a little simpler. 

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

Top Solution Authors