web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Calculating the differ...
Power Automate
Unanswered

Calculating the difference between two date/times

(0) ShareShare
ReportReport
Posted on by 34

i am trying to calculate the number of days between 2 dates/times using: 

 

datediff(body('Get_Shift_Request_details')?['shiftRequest']?['startTime'],body('Get_Shift_Request_details')?['shiftRequest']?['endTime'])
 
but it keeps failing with this error: 
 
InvalidTemplate. Unable to process template language expressions in action 'Send_an_email' inputs at line '1' and column '1481': 'The template function 'datediff' is not defined or not valid.'.
 
is there a way to do this?
Categories:
I have the same question (0)
  • v-monli-msft Profile Picture
    on at

    Hi @Damo_R,

     

    Please try to first format the data to only date format and then calculate them using datediff. For your reference:

    https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-workflow-definition-language

     

    Regards,

    Mona

  • Rachit Profile Picture
    2 on at

    Hi there,

     

    I'm trying a similar expression which gives me this error:

     

    The template validation failed: 'The template action 'Condition' at line '1' and column '1599' is not valid: "Unable to parse template language expression 'less(datediff(second, convertTimeZone(utcNow(), 1230, 1720), items('Apply_to_each')?['Project_x0020_Deadline']), 172800)'

     

    All of the expression has already been covered above. Can you help me spot the error and remove it, @v-monli-msft?

     

    Thanks,

    Rachit B.

  • Verified answer
    Damo_R Profile Picture
    34 on at

    Hi, 

     

    Thank you for taking the time to reply to my issue, however formatting the date does not appear to work. 

     

    i have decided to go down another route and create a physical column in a list that will handle a complex calculatoin, then using the right connector i can pull the information through to the flow. 

     

    Calculation for working out weekdays  between 2 dates: 

     

    =IF([Hours]>4,IF(AND((WEEKDAY([End Of Holiday],2))<(WEEKDAY([Start of Holiday],2)),((WEEKDAY([Start of Holiday],2))-(WEEKDAY([End Of Holiday],2)))>1),(((DATEDIF([Start of Holiday],[End Of Holiday],"D")+1))-(FLOOR((DATEDIF([Start of Holiday],[End Of Holiday],"D")+1)/7,1)*2)-2),(((DATEDIF([Start of Holiday],[End Of Holiday],"D")+1))-(FLOOR((DATEDIF([Start of Holiday],[End Of Holiday],"D")+1)/7,1)*2))),"0.5")

     

    i have another column that calulates the same hours and using the above formula i have included a function to see if the hours column is less than 4 hours it will display 0.5 for a half day. 

     

    For simply calculateing week days between 2 dates the bold section could be removed, this may be able to be used within a flow but i have not tested it. 

     

     

  • emon40 Profile Picture
    4 on at

    How do you do it for working hours between 2 dates (7.5 per day)?

  • Damo_R Profile Picture
    34 on at

    Hi, 

     

    The above formula has since been adjusted and now calculates the hours between 2 dates excluding weekends based on serial hours. 

     

    =IF(OR([Start Of Holiday]="",[End Of Holiday]="",[End Of Holiday]<[Start Of Holiday],[End Of Holiday]=[Start Of Holiday]),"",IF(DATEDIF([Start Of Holiday],[End Of Holiday],"d")-(((DATEDIF([Start Of Holiday],[End Of Holiday],"d")+WEEKDAY([Start Of Holiday],3)-WEEKDAY([End Of Holiday],3))/7)*2)+1*(WEEKDAY([Start Of Holiday],3)>5)-(WEEKDAY([End Of Holiday],3)-4)*(WEEKDAY([End Of Holiday],3)>4)<0,0,IF(([End Of Holiday]-TRUNC([End Of Holiday])/1)-([Start Of Holiday]-TRUNC([Start Of Holiday])/1)>0.3,((DATEDIF([Start Of Holiday],[End Of Holiday],"d")-(((DATEDIF([Start Of Holiday],[End Of Holiday],"d")+WEEKDAY([Start Of Holiday],3)-WEEKDAY([End Of Holiday],3))/7)*2)+1*(WEEKDAY([Start Of Holiday],3)>5)-(WEEKDAY([End Of Holiday],3)-4)*(WEEKDAY([End Of Holiday],3)>4))+1),((DATEDIF([Start Of Holiday],[End Of Holiday],"d")-(((DATEDIF([Start Of Holiday],[End Of Holiday],"d")+WEEKDAY([Start Of Holiday],3)-WEEKDAY([End Of Holiday],3))/7)*2)+1*(WEEKDAY([Start Of Holiday],3)>5)-(WEEKDAY([End Of Holiday],3)-4)*(WEEKDAY([End Of Holiday],3)>4))+0.5))))

     

    Basically if its a weekday and the difference in hours is less than 0.3 (serial hours) it will mark as half day. Over 0.3 is a full day

     

    This should work for you also once you have replaced the column names. 

  • emon40 Profile Picture
    4 on at

    Thanks Damo for your help.

     

    Sorry it's not working for me. Showing few errors. Please see the screenshot for reference.

     

    PowerApps Hours Error.jpg

    =IF(OR([Date_x0020_From]="",[Date_x0020_To]="",[Date_x0020_To]<[Date_x0020_From],[Date_x0020_To]=[Date_x0020_From]),"",IF(DATEDIF([Date_x0020_From],[Date_x0020_To],"d")-(((DATEDIF([Date_x0020_From],[Date_x0020_To],"d")+WEEKDAY([Date_x0020_From],3)-WEEKDAY([Date_x0020_To],3))/7)*2)+1*(WEEKDAY([Date_x0020_From],3)>5)-(WEEKDAY([Date_x0020_To],3)-4)*(WEEKDAY([Date_x0020_To],3)>4)<0,0,IF(([Date_x0020_To]-TRUNC([Date_x0020_To])/1)-([Date_x0020_From]-TRUNC([Date_x0020_From])/1)>0.3,((DATEDIF([Date_x0020_From],[Date_x0020_To],"d")-(((DATEDIF([Date_x0020_From],[Date_x0020_To],"d")+WEEKDAY([Date_x0020_From],3)-WEEKDAY([Date_x0020_To],3))/7)*2)+1*(WEEKDAY([Date_x0020_From],3)>5)-(WEEKDAY([Date_x0020_To],3)-4)*(WEEKDAY([Date_x0020_To],3)>4))+1),((DATEDIF([Date_x0020_From],[Date_x0020_To],"d")-(((DATEDIF([Date_x0020_From],[Date_x0020_To],"d")+WEEKDAY([Date_x0020_From],3)-WEEKDAY([Date_x0020_To],3))/7)*2)+1*(WEEKDAY([Date_x0020_From],3)>5)-(WEEKDAY([Date_x0020_To],3)-4)*(WEEKDAY([Date_x0020_To],3)>4))+0.5))))

     

    My fields:

    Date_x0020_From
    Date_x0020_To

  • Damo_R Profile Picture
    34 on at
    Are your fields date or date/time fields?

    Looks as though it's just dates.. which you could do a formula to sum the difference between dates multiplied by 7.5

    E.g =if(sum(Date_x0020_To - Date_x0020_From) >=1, sum(Date_x0020_To - Date_x0020_From)*7.5,"")

    This will look at the difference between the 2 dates if greater than or equal to 1 it will multiply the difference between the dates by 7.5 and display the result. If it's less than a day it will show nothing.
    This does however not take into consideration half days or working days.

    My original formula above will only work with date time fields as calculates the number of hours and daily 0.5 or 1 you are after actual hours being displayed in the suggested hours.

    Also this calculation had to be done in the SharePoint list and not the flow.

    The flow now pulls list items and total days is displayed from that.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 538 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 405 Moderator

#3
abm abm Profile Picture

abm abm 252 Most Valuable Professional

Last 30 days Overall leaderboard