cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Damo_R
Level: Powered On

Calculating the difference between two date/times

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?
1 ACCEPTED SOLUTION

Accepted Solutions
Damo_R
Level: Powered On

Re: Calculating the difference between two date/times

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. 

 

 

7 REPLIES 7
Community Support Team
Community Support Team

Re: Calculating the difference between two date/times

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

Community Support Team _ Mona Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Rachit
Level: Power Up

Re: Calculating the difference between two date/times

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.

Damo_R
Level: Powered On

Re: Calculating the difference between two date/times

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
Level: Power Up

Re: Calculating the difference between two date/times

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

Damo_R
Level: Powered On

Re: Calculating the difference between two date/times

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
Level: Power Up

Re: Calculating the difference between two date/times

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
Level: Powered On

Re: Calculating the difference between two date/times

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.

Helpful resources

Announcements
firstImage

Microsoft Business Applications Virtual Launch Event

Watch the event on demand for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

firstImage

Watch Sessions On Demand!

Continue your learning in our online communities.

Power Platform 2019 Release Wave 2 Plan

Power Platform 2019 Release Wave 2 Plan

Features releasing from October 2019 through March 2020.

thirdimage

Flow Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

sixthImage

Power Platform World Tour

Find out where you can attend!

seventhimage

Webinars & Video Gallery

Watch & learn from the Flow Community Video Gallery!

Users Online
Currently online: 527 members 5,321 guests
Please welcome our newest community members: