The first tutorial I ever watched on writing expressions in Power Automate used the Compose action. So, I used Compose actions anytime I needed to write an expression until I ran into problems. But, Compose isn’t the only action you can use. This post explains three things you should take into consideration when choosing which action to use for writing expressions in Power Automate. Read on to learn more.
Like most applications, Power Automate (and Power Automate Desktop) offers users a wide range of data structures to work with. They include but are not limited to tables, strings, objects, and arrays. Many of these structures are visible when creating a variable, as you can see below.
Therefore, the first thing to consider before writing an expression is …what’s the natural structure of the data you are working with? Is it a simple string, a column of data, a table or something else? And, what do you want to do with it?
Let me give you an example, let’s say that you are parsing an email. You want to look for specific keywords in the email body. Does it make more sense to put the email body into a long string and search the string? Or, should you put each sentence of the email body into an array and search within each sentence? The answer to that question will influence the data structure you choose.
So, before doing anything, have a data structure in mind for your task.
Next, it’s important to understand how a particular action outputs data and whether or not that fits with the data structure you have in mind.
Variables are easy. When initializing a variable in Power Automate, you choose the structure. Notice that a table is not one of those structures. If you need a table, then Go with a Create CSV table or Create HTML table action instead. And, you can write expressions from a variable. This is a great option for writing expressions.
Another option is the Compose action. When I first started in Power Automate, I only used Compose. However, the output of a Compose action will always be an array. And, the tricky part is that the action gives no indication the output is an array. You just have to know.
Why does this matter? Well, not all expressions work with arrays. For example, the Replace and Substitute expressions only work with strings. When I tried to replace the word ‘Email’ with the work ‘Correspondence’ from the output of a Compose action, it generates an error.
Replace(outputs('Compose_22'), 'Email', 'Correspondence')
The problem shows up in this specific section of the error message. The function ‘replace’ requires a string, but I gave it the output from a compose action, which is an array. No worky.
Don’t get me wrong, you can still replace values in an array. You just have to do it in a different way (post coming soon).
What Did We Learn?
So, what did we cover in this post?
- Before diving into expressions, think about the natural structure of the data you are working with and the task at hand.
- Some actions output to a specific data structure, like the Compose action, which outputs to arrays.
- You can use Compose or Variable actions to write expressions in Power Automate.