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
Anonymous
Not applicable

Calculate time difference across dates and users

Hey guys, I'm hoping you can assist two desperate bachelor thesis authors!

We have a dataset with sales orders, a date when order was packed and a worker who packed specific sales order. We are interested to find out how much time each packer spends from finishing one order till finish of the next one. This is how the data is organized (Despatch date, packer name):
"28/7/2023 13:11:00" "Georg",
"29/7/2023 12:11:30" "Britney",
"27/7/2023 08:12:15" Georg",
"28/7/2023 10:12:45" "Britney"

Can you provide a formula that calculates the time difference between finishing one order till finishing another for each packer?

1 ACCEPTED SOLUTION
Anonymous
Not applicable

First sort by name, and then by date. Add two index columns, one starting at zero and one starting at one, then left outer join the table to itself on index equals index one. Then change your text date time to an actual date time value, and then just do the row math, specifying that if Name <> Name.1 then 0 else DateTime-DateTime.2

 

--Nate

 

--Nate

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Hi @Anonymous ,

 

You can try my solution in Power Query.

vstephenmsft_1-1691477431157.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrLQN9c3MjAyVjA0tjI0tDIwUNJRck/NL0pXitUBSlvCpY1A0sYgaaeizJK81EqIAnOYAgMLK5AaU1T9COMNQNImpsj6YwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Despatch date" = _t, #"packer name" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Despatch date", type text}, {"packer name", type text}}),
    #"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"Despatch date", type datetime}}, "en-GB"),
    #"Sorted Rows" = Table.Sort(#"Changed Type with Locale",{{"Despatch date", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"packer name"}, {{"All Rows", each _, type table [Despatch date=nullable datetime, packer name=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([All Rows],"Index",0,1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"All Rows"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Despatch date", "Index"}, {"Despatch date", "Index"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded Custom", "Previous Despatch date", each List.Range(
   #"Expanded Custom"[Despatch date],
   [Index]-1,
   1)),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom1", {"Previous Despatch date", each Text.Combine(List.Transform(_, Text.From)), type text}),
    #"Replaced Errors" = Table.ReplaceErrorValues(#"Extracted Values", {{"Previous Despatch date", ""}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Replaced Errors",{{"Previous Despatch date", type datetime}}),
    #"Added Custom2" = Table.AddColumn(#"Changed Type1", "Time Difference", each Duration.ToText([Despatch date]-[Previous Despatch date]))
in
    #"Added Custom2"

You can also download my attachment for more details.

 

                                                                                                                                                         

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.           

Anonymous
Not applicable

Thank you for your reply:)

Anonymous
Not applicable

First sort by name, and then by date. Add two index columns, one starting at zero and one starting at one, then left outer join the table to itself on index equals index one. Then change your text date time to an actual date time value, and then just do the row math, specifying that if Name <> Name.1 then 0 else DateTime-DateTime.2

 

--Nate

 

--Nate

Anonymous
Not applicable

I appreciate your response!

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