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

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
Microsoft

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)```

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.
4 REPLIES 4
Microsoft

Hi @mpalha04,

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

Sample data:

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.

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.
Helper III

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 :

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.

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.

Microsoft

Hi @mpalha04,

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.
Microsoft

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)```

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.

Announcements