When it comes to fundamental functions, nothing compares to the Power Automate “if “function. 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, where we check if a string is less than 255. If it is, then we return the string; otherwise, we do some operations to it. The point is that you can lest a lot of actions, and that’s fine. But keep things as readable as possible.
Finally, it’s important 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 if you have something over 30 levels, you should simplify the expression 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 minimal 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 a lot of nested layers will be hard when something goes wrong.
Back to the Power Automate Function Reference.