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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Patrick95
Frequent Visitor

Calculate time between dates on 2 different rows

Hi All,


I'm looking for a way to calculate the time between 2 dates on 2 different rows:

The data is from picking orders in a warehouse. Each picking process has an unique Document No. and is continued via Status codes. The purpose is to measure the average picking time per day/userID/document No.  


In my example below I want to calculate the time between the Starting date and the End date that are located in differend rows.

 

Patrick95_1-1660659166021.png

 

Hope you guys can help!

 

Greetings,

 

Patrick



2 ACCEPTED SOLUTIONS

I ve created this table with kind of same structure.

 

SebSchoon1_0-1660743975784.png

 

Then Group By ID

 

SebSchoon1_1-1660744170195.png

 

Then add a column with the "add personalized column"

 

Paste this inside

 

= Table.AddColumn(#"Lignes groupées", "Personnalisé", each Table.FillDown([Nombre],{"End date"}))

 

explanation

SebSchoon1_2-1660744385715.png

 

Delete column [Nombre]

expand [Personnalisé]

SebSchoon1_3-1660744414839.png

 

SebSchoon1_4-1660744534532.png

 

Then add logic 🙂

 

Then append tables.

 

You can also try to keep file name by type of selected source (ex: folder)

View solution in original post

v-yalanwu-msft
Community Support
Community Support

Hi, @Patrick95 ;

You could try it.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIyMDLStdA1NFAwNLYytLAyMAAKKsXqQGTR2DDFhgqGhlZGliDFIFknJClTBUNTK2NTJHOcUPUCFZhZGYAtio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DocumentNo = _t, Start = _t, End = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DocumentNo", type text}, {"Start", type datetime}, {"End", type datetime}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"DocumentNo"}, {{"Count", (x)=> Table.AddColumn(x, "Picking Time", each Duration.ToRecord(List.Max(x[End]) - List.Min(x[Start])), Int64.Type)}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Start", "End", "Picking Time"}, {"Start", "End", "Picking Time"}),
    #"Expanded Picking Time" = Table.ExpandRecordColumn(#"Expanded Count", "Picking Time", {"Days", "Hours", "Minutes", "Seconds"}, {"Days", "Hours", "Minutes", "Seconds"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Picking Time",{{"Hours", type text}, {"Minutes", type text}, {"Seconds", type text}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type1","0","00",Replacer.ReplaceValue,{"Hours", "Minutes", "Seconds"}),
    #"Inserted Merged Column" = Table.AddColumn(#"Replaced Value", "duration", each Text.Combine({Text.From([Days], "zh-CN"), Text.From([Hours], "zh-CN"), Text.From([Minutes], "zh-CN"), Text.From([Seconds], "zh-CN")}, ":"), type text),
    #"Removed Columns" = Table.RemoveColumns(#"Inserted Merged Column",{"Hours", "Minutes", "Seconds"})
in
    #"Removed Columns"

 

The final show:

AilsaTao_0-1660895240838.png

If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community


Best Regards,
Community Support Team _ Yalan Wu
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

17 REPLIES 17
v-yalanwu-msft
Community Support
Community Support

Hi, @Patrick95 ;

Is your problem solved?  If so, kindly mark the proper reply as a solution to help others having the similar issue and close the case. If not, let me know and I'll try to help you further.


Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

v-yalanwu-msft
Community Support
Community Support

Hi, @Patrick95 ;

You could try it.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIyMDLStdA1NFAwNLYytLAyMAAKKsXqQGTR2DDFhgqGhlZGliDFIFknJClTBUNTK2NTJHOcUPUCFZhZGYAtio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DocumentNo = _t, Start = _t, End = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DocumentNo", type text}, {"Start", type datetime}, {"End", type datetime}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"DocumentNo"}, {{"Count", (x)=> Table.AddColumn(x, "Picking Time", each Duration.ToRecord(List.Max(x[End]) - List.Min(x[Start])), Int64.Type)}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Start", "End", "Picking Time"}, {"Start", "End", "Picking Time"}),
    #"Expanded Picking Time" = Table.ExpandRecordColumn(#"Expanded Count", "Picking Time", {"Days", "Hours", "Minutes", "Seconds"}, {"Days", "Hours", "Minutes", "Seconds"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Picking Time",{{"Hours", type text}, {"Minutes", type text}, {"Seconds", type text}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type1","0","00",Replacer.ReplaceValue,{"Hours", "Minutes", "Seconds"}),
    #"Inserted Merged Column" = Table.AddColumn(#"Replaced Value", "duration", each Text.Combine({Text.From([Days], "zh-CN"), Text.From([Hours], "zh-CN"), Text.From([Minutes], "zh-CN"), Text.From([Seconds], "zh-CN")}, ":"), type text),
    #"Removed Columns" = Table.RemoveColumns(#"Inserted Merged Column",{"Hours", "Minutes", "Seconds"})
in
    #"Removed Columns"

 

The final show:

AilsaTao_0-1660895240838.png

If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community


Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

With some adjustments on my part I succeeded! thanks!

SebSchoon1
Post Patron
Post Patron

Hi, 

 

Maybe you could Group the data by Order ID , 

 

Then in the column which contains all the created tables, you fill down the end date column

 

Then create conditional column, 

 

if start date is not null and end date is not null Then do "End date" minus "Start date"  else = null

 

then expand ?

 

🙂

Has that worked for you? if Yes could you click on the solution button?

 

it would be my first one ^^

Hi, 

 

No unfortunatley not, I have replied on your post earlier. 

 

Gre, 

 

Patrick

Hi Seb,

 

Unfortunately this didn't help, but thanks for your answer.

 

Patrick95_0-1660737722862.png

 

Greetings,

 

Patrick

Hello,

 

For your Grouping do like this.

 

Group By ID (only) then by table  

You'll see all the id's in left column and column containing tables on the right column

 

In these tables, you can write formulas to drill down (or up) specific columns

 

then follow the logic i told you, then expand, it should work

 

maybe tomorrow i'll have the time to show with an example

 

 

 

Ok I follow you, I've got the ID's in the left column now and the containing tables in the right.

How do I "write formulas to drill down (or up) specific columns"? 

 

Would be great if you can show me 🙂 It's driving me nuts xD

Patrick95_0-1660742169809.png

 

 

Hello,

 

has it worked? as desired output?

I ve created this table with kind of same structure.

 

SebSchoon1_0-1660743975784.png

 

Then Group By ID

 

SebSchoon1_1-1660744170195.png

 

Then add a column with the "add personalized column"

 

Paste this inside

 

= Table.AddColumn(#"Lignes groupées", "Personnalisé", each Table.FillDown([Nombre],{"End date"}))

 

explanation

SebSchoon1_2-1660744385715.png

 

Delete column [Nombre]

expand [Personnalisé]

SebSchoon1_3-1660744414839.png

 

SebSchoon1_4-1660744534532.png

 

Then add logic 🙂

 

Then append tables.

 

You can also try to keep file name by type of selected source (ex: folder)

To calculate Days and time between End date and Start date there are multiple possibilities

 

From the menu. Select first [End date] then [Start date]

 

Then go here 

SebSchoon1_5-1660745410952.png

OR add personnalized column

 

and type

SebSchoon1_6-1660745470347.png

Then expand record

 

to get (after replacing (null) errors

 

SebSchoon1_7-1660745541181.png

 

 

 

 

 

good !! i'm on it !

 

i'm trying to show you something with which you should only change according to your columns names

Hi Seb, everything OK? 

Now the next thing I have to do is calculate the hours between the two dates and EXCLUDE the weekends (Sat and Sunday). Do you have any idea how to achieve this? Like to hear from you again. Greetings.

jbwtp
Memorable Member
Memorable Member

Hi @Patrick95,

 

I think this conceptually what you are after. Could you pleae have a look?

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTLUN9Q3MgIylGJ1ICJobGNDfXOQithYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [OrderNo = _t, Start = _t, End = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"OrderNo", type text}, {"Start", type date}, {"End", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"OrderNo"}, {{"Count", (x)=> Table.AddColumn(x, "Picking Time", each Duration.TotalDays(List.Max(x[End]) - List.Min(x[Start])), Int64.Type)}}),
    #"Expanded Count" = Table.Combine(#"Grouped Rows"[Count])
in
    #"Expanded Count"

 

Thanks,

John

Hi John,

 

Thanks for your quick reply!

 

Seems like it will give the same result (211) unfortunately.

Patrick95_0-1660720340586.png

Patrick95_1-1660720461071.png

 

Could you please take another look? 

 

Thanks!

 

Patrick

 

 

 

Hi @Patrick95,

 

Do you mind sharing your query code?

Unless you copied everything including the Source step from my code, I can't see a reson for getting the 211 in the Picking Time column. If you can provide a code, I will write exactly how it needs to look like, so you could copy/paste it and move forward.

 

Kind regards,

John

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.