Forum Discussion

mpalha04's avatar
mpalha04
Icon for Helper III rankHelper III
8 years ago
Solved

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!

4 Replies

  • v-danhe-msft's avatar
    v-danhe-msft
    Icon for Microsoft Employee rankMicrosoft Employee

    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.

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

    https://www.dropbox.com/s/r4kcu8g7niea2hf/Calculate%20Time%20Difference%20with%20Odd%20Working%20Hours.pbix?dl=0

     

    Regards,

    Daniel He

    • mpalha04's avatar
      mpalha04
      Icon for Helper III rankHelper 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.

       

      • v-danhe-msft's avatar
        v-danhe-msft
        Icon for Microsoft Employee rankMicrosoft Employee

        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%20Hours2.pbix?dl=0

         

        Regards,

        Daniel He