Power Community

Power Community

Sort an array by dates in Power Automate

Sort Dates

When we use the Sort function to sort arrays in Power Automate, it is soo much easier than the 3 step approach that we had before. But there are a few potential issues when you really start to use this function.

Sort Dates

When we sort dates in Power Automate we really want to to work with dates like this:

Sort an array by dates in Power Automate Microsoft Power Automate image 1

We have some dates generated by for example a get items action in SharePoint. Or anything else that generates the dates and all dates are nicely formatted optimized for automation processes.

But what if we have a bad date format returned by an API? Yesterday I was contacted on the chat about an API that didn’t return these nice to work with dates.

In the example below you can see the issue.

Sort an array by dates in Power Automate Microsoft Power Automate image 2

As you can see the dates are sorted by month, then day and then year as the dates are sorted alphabetically. This is of course no use at all.

How can we deal with that?

Obviously just using the sort date in this case will not be good enough.

Sort an array by dates in Power Automate Microsoft Power Automate image 3

Fixing the dates

To fix the dates, all we have to do is use a select action that converts the date format often used in the US to the sortable date format. The expression used for this is:

formatDateTime( item(), ‘yyyy-MM-dd’)

Sort an array by dates in Power Automate

And for the sorting we now use the following one liner:

sort(body('Select'),'Date')

And our dates are sorted!

This post was originally published on this site

- Advertisement -spot_img

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement - Advertisement

Latest News

ChatGPT – Insights

Hi Folks, In this modern era where AI/ML is ruling the world and automating all the possible day to day...

More Articles Like This

- Advertisement -spot_img