How to compare dates in Power Automate

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.

compare date in power automate

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.

compare date in power automate

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.

compare date in power automate

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.

compare dates in Power Automate
@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.

compare date in power automate

Hope this helped!

You will also like the below posts.

Debajit Dutta
Business Solutions MVP

3 thoughts on “How to compare dates in Power Automate”

    1. Debajit Dutta (Business Solutions MVP)

      having come from core .NET and C# background, I can understand the pain here. 🙂

      Debajit

Comments are closed.