Nothing compares to the Power Automate “if “function when it comes to fundamental functions. It exists in all platforms, programming languages, and tools. You name it, and if it allows some customization, it contains a Power Automate “if “function.
So let’s look at the if function for Power Automate.
It follows a simple pattern.
- Expression to check
- The value/expression if the result is “true.”
- The value/expression if the result is “false.”
Let’s start with a simple example:
if(equals(1,2),'equal','not equal') will return not equal
It’s a silly example, but let’s look at the expression from the “Add Attachments to Planner from Teams” template:
We’re using the lessOrEquals function to check if a string is less than 255. If it is, we return the string; otherwise, we do some operations to it. The point is that you can lest many actions, and that’s fine. But keep things as readable as possible.
Finally, it’s essential to understand that the string “true” is not a boolean. Be especially careful with these cases because if the function will return an error:
Unable to process template language expressions in action 'Compose' inputs at line '1' and column '6311': 'The template language function 'if' expects its first parameter to be of type boolean. The provided value is of type 'String.' Please see https://aka.ms/logicexpressions#if for usage details.'.
It’s not quite a limitation, but you can only have, in the expression, boolean values. Any variation, like “True” or one, will not be accepted, and Power Automate will return an error. Also, be careful with the nesting level. I’ve tried with 30, and all worked fine, but you should simplify the expression if you have something over 30 levels before continuing. Finally, remember that you have the Switch Action that could be a valid alternative if you’re comparing values.
Here are some things to keep in mind.
If you don’t have an expression, you probably don’t need it.
If you don’t have an expression in the first parameter, you probably don’t need the Power Automate “if “function. Why? Because if you don’t have an expression, the result will always be the same. So if it’s always the same, you don’t need it. For example:
if(true,'All good','Will never use this option')
There’s only one exception where you have a variable in the first parameter. That variable can contain true or false, but if it’s the case, don’t forget to add a note explaining why you’re using a variable.
It’s possible to nest, and it’s even recommended in some cases. But keep nesting to a minimum since the more layers you have, the harder it is to debug. Also, avoid nesting the expression—same reason as above. Analyzing an expression with many nested layers will be hard when something goes wrong.
Microsoft’s if Function Reference
Back to the Power Automate Function Reference.
Photo by Jac Alexandru on Unsplash
2 thoughts on “Power Automate: if function”
Great stuff! But… How would you evaluate a boolean (true vs false) then? Should the value be converted into an integer first?
You can use the bool function to convert the values.
Integer values are always compared as they are not as boolean.