“I feel that I’ve got too many conditions to handle basic stuff, can I somehow reduce/replace them to simplify my Power Automate flow?”
Conditions, one of the key building blocks in Power Automate. A simple decision: if “something” is true, do this, otherwise do that. But often it’s not that simple and if a condition is false it leads to another condition, another true or false. And the more potential options there’re, the more conditions you’ll need.
Is that really necessary? Isn’t there an easier way than a tree of conditions?
Replace the conditions with if(…) expressions
The Power Automate if(…) expression is the equivalent of the ‘Condition’ action. Instead of adding the action and then deciding e.g. on a value for some field, you can use the if(…) expression to get the correct value right away.
The basic if(…) expression was already explained in the date-formatting article and looks as below.
if(<condition>, <ifConditionIsTrue>, <ifConditionIsFalse>)
What you maybe didn’t know is that you combine them inside each other. Evaluate the first condition, and if it’s false try another evaluation.
if(<condition>, <ifConditionIsTrue>, if(<condition2>, <ifCondition2IsTrue>, <ifCondition2IsFalse>) )
And it can go on an on, as many ifs as you need. But how do you build such expression?
Start from the last ‘Condition’
Let’s take an example, a flow where you translate a number into more generic value. If the number is less than 3 the value is LOW, for 3 or 4 it’s MID, and if it’s 5 or more it’s HIGH. When using the ‘Condition’ actions it’ll look as below.
To turn it into a single expression with if(…) start from the bottom. The last condition evaluates whether the value is less than 5.
You can use the trick with the ‘Filter array’ action if you’re not sure how to write such condition.
If it’s true, use the value MID, otherwise use the value HIGH.
if(less(outputs('Compose_-_Value'), 5), 'MID', 'HIGH')
That was the last condition, now let’s go one step higher, to the previous condition. The flow will reach the 2nd condition only if the first condition is false.
if(<condition>, <ifTrue>, if(less(outputs('Compose_-_Value'), 5), 'MID', 'HIGH'))
The first condition should evaluate whether the value is less than 3.
if(less(outputs('Compose_-_Value'), 3), <ifTrue>, if(less(outputs('Compose_-_Value'), 5), 'MID', 'HIGH' ) )
And if it’s true, it should use the value LOW.
if(less(outputs('Compose_-_Value'), 3), 'LOW', if(less(outputs('Compose_-_Value'), 5), 'MID', 'HIGH' ) )
Since there’re no other conditions in the flow, that’s it. A single expression that’ll get the value without branching the flow.
Whenever you start stacking conditions in your Power Automate flow, you should reconsider whether they are needed, or if you can replace them and simplify the flow. Start from the last condition, convert it into if(…) expression, and keep adding the other conditions around it. At the end it can be a single expression that can replace a whole bunch of the ‘Condition’ actions.
It’s another concept complementing the configuration lists that’ll help you build more efficient and easier to manage flows.