“I know what the expression should do, I just don’t know how to build it! What Power Automate expressions should I use and how to combine them?”
Using expressions is similar to using the existing Power Automate actions. You’ve got these small building blocks, each of them with some functionality, and you combine them together to do a specific task.
While the designer makes it easy to drag and drop actions, with expressions it’s mostly on your shoulders. It gives you more freedom, you’re not limited by the existing actions, but you get also more responsibility. Unless you use the right expression or combination of multiple expressions, the flow won’t work.
How do you then build the right expression from the small building blocks?
Follow your thought process
Let’s show it on an example – a PDF file with a date in the name, e.g. Tom_Riha_overview_13/05/2022.pdf, and the goal is to rename the file while changing the date format: 2022-05-13_Tom_Riha_overview.pdf. How would you do that using expressions?
Isolate the date string
The first step would be to isolate the date. But Power Automate won’t “just look and see the date”, it doesn’t even know there’s a date. Power Automate sees the whole file name as a text with various characters. If you look on it in the same way, you’ll notice that the date is after the last underscore character. The extraction will take two steps – split(…) the string by the underscore:
split('Tom_Riha_overview_13/05/2022.pdf', '_')
and take only the last(…) value:
last(split('Tom_Riha_overview_13/05/2022.pdf', '_'))
You just reduced the string to: 13/05/2022.pdf. It removed big part of the text, but it still has the file extension. Following the same logic, you can split(…) the result again, this time by a comma:
split(last(split('Tom_Riha_overview_13/05/2022.pdf', '_')), '.')
and take only the first(…) part:
first(split(last(split('Tom_Riha_overview_13/05/2022.pdf', '_')), '.'))
The result of this expression will be the date string alone: 13/05/2022.
Convert the string to a date
You know it’s a date and as such it’s easy for you to change its format. But again, Power Automate doesn’t know that it’s a date. That the 13 represents day, 05 month, and 2022 a year. You must tell it that it’s a date and in what format with the parseDateTime(…) expression:
parseDateTime(first(split(last(split('Tom_Riha_overview_13/05/2022.pdf', '_')), '.')), 'en-US', 'dd/MM/yyyy')
Format the date
Now, when the flow knows that the string represents a date and what date it is, you can change its format with the formatDateTime(…) expression:
formatDateTime(parseDateTime(first(split(last(split('Tom_Riha_overview_13/05/2022.pdf', '_')), '.')), 'en-US', 'dd/MM/yyyy'), 'yyyy-MM-dd')
Get the original text and the file extension
You’ve got the date in the right format, what’s left is to add the remaining text from the file name. Again, you must tell the flow which part of the text it should add, it doesn’t know that it already processed the date.
You can use the same approach as when isolating the date. Split(…) by underscore:
split('Tom_Riha_overview_13/05/2022.pdf', '_')
take(…) the 3 pieces before the date:
take(split('Tom_Riha_overview_13/05/2022.pdf', '_'), 3)
and join(…) them back into a string using underscore as the delimiter:
join(take(split('Tom_Riha_overview_13/05/2022.pdf', '_'), 3), '_')
You’ll get also the file extension with a similar approach:
last(split('Tom_Riha_overview_13/05/2022.pdf', '.'))
Put all the pieces together in a single expression
Use the concat(…) expression to put all the pieces together to build the new file name.
concat(
formatDateTime(parseDateTime(first(split(last(split('Tom_Riha_overview_13/05/2022.pdf', '_')), '.')), 'en-US', 'dd/MM/yyyy'), 'yyyy-MM-dd'),
'_',
join(take(split('Tom_Riha_overview_13/05/2022.pdf', '_') ,3), '_'),
'.',
last(split('Tom_Riha_overview_13/05/2022.pdf', '.'))
)
Summary
The final expression probably looks scary, but all we did was to follow a thought process and combine the steps into Power Automate expressions. Slowly, step by step, without any assumptions, using only the visible data. It is maybe a bit extreme example and your expressions will be simpler, but learning to combine expressions together will make it easier to build any flow.
That’s why I included also some common combinations of the expressions into the expressions cheat sheet.
1 thought on “How to combine expressions in your Power Automate flows”