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

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.

Reply
mpalha04
Helper III
Helper III

Calculate Time Difference with Odd Working Hours

Hello,

 

I am trying to calculate the turn around time for a ticketing system with working hours between 8AM-1AM Monday-Friday. Orders can be placed by customers anytime, so, if an order is placed outside of working hours, the time difference will calculate beginning the next working day at 8AM.

 

Another challenge is that MessageSentTimeLocal can be outside of working hours as well, but the turn around time should be calculated up until the end of the working day at 1AM. 

Some sample data below:

Capture.JPG

 

For orders like Order#35812, the TAT should be about 5.5 hours since MessageTimeLocal should technically be treated as 1AM as it is outside of working hours. 

 

If the MessageSentTimeLocal is after a couple of days from order date, the TAT should still only account for the working hours between 8AM-1AM. 

 

If an order and MessageSentTimeLocal are both not within working hours, the TAT should be 0.  

Help will be greatly appreciated!

1 ACCEPTED SOLUTION

Hi @mpalha04,

You could modify the former formula with this:

Hours = var a= IF([OrderedDateLocal]>=[Column],[OrderedDateLocal],[Column])
           var b= IF([MessageSentTimeLocal]<=[Column2],[MessageSentTimeLocal],[Column2])
           return IF(DATEDIFF(a,b,MINUTE)/60<0,DATEDIFF('Table1'[OrderedDateLocal],[MessageSentTimeLocal],MINUTE)/60,DATEDIFF(a,b,MINUTE)/60)+IF(DATEDIFF([Column2]+(7/24),[MessageSentTimeLocal],MINUTE)/60>0,DATEDIFF([Column2]+(7/24),[MessageSentTimeLocal],MINUTE)/60,0)

You can also download the PBIX file to have a view.

https://www.dropbox.com/s/adycmmdh40bm2gq/Calculate%20Time%20Difference%20with%20Odd%20Working%20Hou...

 

Regards,

Daniel He

Community Support Team _ Daniel He
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

4 REPLIES 4
v-danhe-msft
Employee
Employee

Hi @mpalha04,

Based on my test, you could refer to below steps:

Sample data:

1.PNG

Create three calculated columns:

Column = SUBSTITUTE('Table1'[OrderedDateLocal],RIGHT('Table1'[OrderedDateLocal],11)," 8:00:00 AM")
Column2 = (SUBSTITUTE('Table1'[MessageSentTimeLocal],RIGHT('Table1'[MessageSentTimeLocal],11)," 1:00:00 AM"))
Hours = var a= IF([OrderedDateLocal]>=[Column],[OrderedDateLocal],[Column])
var b= IF([MessageSentTimeLocal]<=[Column2],[MessageSentTimeLocal],[Column2])
return IF(DATEDIFF(a,b,MINUTE)/60<0,DATEDIFF('Table1'[OrderedDateLocal],[MessageSentTimeLocal],MINUTE)/60,DATEDIFF(a,b,MINUTE)/60)

Now you could get the correct result.

3.PNG

You can also download the PBIX file to have a view.

https://www.dropbox.com/s/r4kcu8g7niea2hf/Calculate%20Time%20Difference%20with%20Odd%20Working%20Hou...

 

Regards,

Daniel He

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

Hi Daniel,

 

This is great, but there are some cases that need to be resolved:

If the order is placed and MessageSentTimeLocal is the next day. Refer Order# 35386 - the hours should be about 20 hours since working hours are 8AM-1AM :

 

Capture.JPG

 

If the order is placed outside working hours and MessageSentTime local is outside of working hours. Refer to Order#s 35412, 35413 below - the hours should be 0.

Capture.JPG

If an order is placed on the weekend, the hours should start calculating beginning work hours. Please refer to all the data below: The orders highlighted should be about 10mins. Although August 4 is a Saturday, it before 1AM so the working hours from Friday carries over. No working hours should be calculated on Saturday and Sunday... working hours would begin calculating the following Monday at 8AM.

Capture.JPG

 

Hi @mpalha04,

Could you please tell me if your problem has been solved? If it is, could you please mark the helpful replies as Answered?

 

Regards,

Daniel He

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

Hi @mpalha04,

You could modify the former formula with this:

Hours = var a= IF([OrderedDateLocal]>=[Column],[OrderedDateLocal],[Column])
           var b= IF([MessageSentTimeLocal]<=[Column2],[MessageSentTimeLocal],[Column2])
           return IF(DATEDIFF(a,b,MINUTE)/60<0,DATEDIFF('Table1'[OrderedDateLocal],[MessageSentTimeLocal],MINUTE)/60,DATEDIFF(a,b,MINUTE)/60)+IF(DATEDIFF([Column2]+(7/24),[MessageSentTimeLocal],MINUTE)/60>0,DATEDIFF([Column2]+(7/24),[MessageSentTimeLocal],MINUTE)/60,0)

You can also download the PBIX file to have a view.

https://www.dropbox.com/s/adycmmdh40bm2gq/Calculate%20Time%20Difference%20with%20Odd%20Working%20Hou...

 

Regards,

Daniel He

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

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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