Introduction of Power Automate has been a revolution when it comes to automations and connecting disparate systems. Stuffs which used to take days can now be done in few hours.
And yet, sometimes the simplest of tasks in Power automate require some workarounds. And one such requirement is to compare dates in Power Automate.
If you are looking to compare two date fields from DataVerse or from SharePoint, things work fine. However the problem start when the dates are in string format and you need to perform the date comparison. Let’s take an example here.
I have declared couple of variables namely Start Date and EndDate as specified in the below screenshot.
As a next step I perform a Do-Until till until the start date is greater than the end date. In each counter, I increment the start date by 7 days.
As you can see in the above screenshot, I am using the formatDateTime function to convert the string to date and then compare. And again I use the addDays function along with formatDateTime to increment start date by 7 days.
Everything seem’s great. Now I run the flow. And below is the number of times the flow ran.
The flow ran only 3 times. And even though we did the date comparison using formatDateTime, it actually did a string comparison.
But how can we get rid of this? Well, there is function called ticks() which come to the rescue. I now modify the above Do Until condition to below.
@greater(ticks(formatDateTime(variables('StartDate'), 'MM/dd/yyyy')), ticks(formatDateTime(variables('EndDate'), 'MM/dd/yyyy')))
And now when it runs, it iterates the correct number of times.
Hope this helped!
You will also like the below posts.
Debajit Dutta
Business Solutions MVP
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.
Good old ticks to the rescue. Thank you for this tip.
Or 1 line of code in c# 🙂 date1>date2 🙂
having come from core .NET and C# background, I can understand the pain here. 🙂
Debajit