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.

Usage

It follows a simple pattern.

  1. Expression to check
  2. The value/expression if the result is “true”
  3. The value/expression if the result is “false”
The “expression to check” in the Power Automate “if“ function usually requires other functions to work. Any function that returns a true/false result is useful in this case.

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:

if(lessOrEquals(length(outputs('Html_to_text')?['body']),255),outputs('Html_to_text')?['body'],substring(outputs('Html_to_text')?['body'],0,255))

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.'.

Limitations

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.

Recommendations:

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.

Minimize nesting

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.

Sources:

Microsoft’s if Function Reference

Back to the Power Automate Function Reference.

Photo by Jac Alexandru on Unsplash

Manuel Gomes

I'm a Project Manager with experience in large projects and companies. I've worked in the past for companies like Bayer, Sybase (now SAP) and I'm currently working for Pestana Hotel Group.

View all posts by Manuel Gomes →

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: