cancel
Showing results for
Did you mean:
Frequent Visitor

## 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
Frequent Visitor

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

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.
New Member

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.

Frequent Visitor

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.

New Member

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

Frequent Visitor

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.

New Member

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

=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

Frequent Visitor
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.

Announcements

#### Global Power Platform Bootcamp

Dive into the Power Platform stack with hands-on sessions and labs, virtually delivered to you by experts and community leaders.

#### Power Platform Community Conference On Demand

Watch Nick Doelman's session from the 2020 Power Platform Community Conference on demand!

#### Power Automate Community Blog

Check out the community blog page where you can find valuable learning material from community and product team members!

Top Solution Authors
Top Kudoed Authors
Users online (7,397)