cancel
Showing results for 
Search instead for 
Did you mean: 
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
Microsoft
Microsoft

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
August 2022 update 768x460.jpg

Check it Out!

Click here to learn more about the August 2022 updates!

August 1 episode 9_no_dates 768x460.jpg

The Power BI Community Show

Watch the playback when Priya Sathy and Charles Webb discuss Datamarts! Kelly also shares Power BI Community updates.

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Top Solution Authors
Top Kudoed Authors