If, Else If, Else in Power Automate Simplified

You know the logic, if a logical expression is met, perform an action or expression, else if it matches another expression follow that outcome, otherwise do the fall back option. How do you create this in Power Automate? Well, you could use the switch action but that requires the condition to be equal to. What if you have a lot of possible cases? Your flow will start to get pretty crowded. If the actions in your flow are generic or can be influenced with a parameter, you could simply look at using an array and even benefit from other logical expressions such as contains.

The scenario. You have a mailbox that you are looking to monitor with a cloud flow and if the subject line matches one of ten possible strings, you want to save the body to a list and email a specific line manager. I’ve started to build the switch solution and then realise, one this is getting out of control, my screen isn’t big enough and two, the condition I need is subject line contains. Surely there is a better way?

Switch Statement in Power Automate

Because we have now identified that the subject must contain a word, we look to use the condition action and as we have multiple scenarios, we can look to use parallel actions. I start to build up my parallel actions and start to get into an even bigger mess.

Case Statement in Power Automate

How about using an array?

Using an array to store parameters is not a new concept and potentially you could use a SharePoint list or Dataverse Table to store the data if you would rather a more visual datasource. The advantage of using a compose action in the Cloud Flow is that everything is together in one place and there are no other dependencies.

Array of parameters in cloud flow
[{
  "id": 1,
  "first_name": "Darrell",
  "last_name": "Fomichyov",
  "email": "dfomichyov0@ourITcompany.com.uk",
  "department": "Business Development"
}, {
  "id": 2,
  "first_name": "Mathian",
  "last_name": "Menichelli",
  "email": "mmenichelli1@ourITcompany.com.uk",
  "department": "Legal"
}, {
  "id": 3,
  "first_name": "Harlan",
  "last_name": "Rogger",
  "email": "hrogger2@ourITcompany.com.uk",
  "department": "Marketing"
}, {
  "id": 4,
  "first_name": "Griz",
  "last_name": "Mangeot",
  "email": "gmangeot3@ourITcompany.com.uk",
  "department": "Human Resources"
}, {
  "id": 5,
  "first_name": "Remus",
  "last_name": "Orbon",
  "email": "rorbon4@ourITcompany.com.uk",
  "department": "Accounting"
}, {
  "id": 6,
  "first_name": "Koralle",
  "last_name": "Lecordier",
  "email": "klecordier5@ourITcompany.com.uk",
  "department": "Training"
}, {
  "id": 7,
  "first_name": "Fancie",
  "last_name": "Kornes",
  "email": "fkornes6@ourITcompany.com.uk",
  "department": "ICT"
}, {
  "id": 8,
  "first_name": "Hannie",
  "last_name": "Lichfield",
  "email": "hlichfield7@ourITcompany.com.uk",
  "department": "Sales"
}, {
  "id": 9,
  "first_name": "Fallon",
  "last_name": "Edowes",
  "email": "fedowes8@ourITcompany.com.uk",
  "department": "Support"
}, {
  "id": 10,
  "first_name": "Garnet",
  "last_name": "Duchenne",
  "email": "gduchenne9@ourITcompany.com.uk",
  "department": "Product Management"
}]

With your parameters nicely stored in a formatted JSON file (and note I used Mockaroo to build this sample very quickly), you can then use a filter array. Using the indexof expression you can check if the subject line contains one of the many subjects in your array. If the Indexof expression returns -1, there is no match and so the filter will only return the objects where there is a match. You could equally perform a filter on an integer if your scenario required a numerical match.

Filter array for subject containing

Now you have a couple of considerations for the final ELSE scenario. If the length of the Filter Array is 0, you have no match and you can use a condition to branch off the no route and perform your alternative outcome. This will be the example I provide below.

If the outcome includes the same actions but different parameters, you could define a simple array for this outcome and perform an if expression. If the length of the filter array is 0, use alternative array, otherwise use result returned by filter. If you want more details on this scenario, leave me a comment. For now, I will use a single condition.

If then ELSE in Cloud Flow

Within the to and body of the send an email action I call upon the values from the filter array. This is called using the first expression and then the json key i.e. email. Expressions used are as follows:

first(body('Filter_array'))?['email']
first(body('Filter_array'))?['first_name']
first(body('Filter_array'))?['last_name']

Seeing it in action

I receive an email with the subject heading “This is a sales email subject line” and the flow is triggered by the when a new email arrives action. The filter array returns the single subject line match.

Subject Match in Cloud Flow

The length of the filter array is then checked to be equal to 1 and if true, we branch down the yes route. An email is sent to the email contained within the object of the first and (hopefully) only match of the filter array.

Email with dynamic variables Power Automate

Conclusion

Had we chosen the switch route, the cloud flow logic wouldn’t have worked as we were looking for a subject containing a word. The switch route, whilst it would work, would become very busy with all the switch cases and what if we needed to introduce another case or we needed to change a parameter or maybe even the logic across all of the cases? A real headache!

Using an array to store your parameters and then the filter array action, we can perform many different scenarios of logical expression. Whilst I used indexof to check for the subject string containing a word, we could look for an exact match by using “is equal to” but other examples include comparisons on integers, where numbers might be greater than or equal to. You can really simplify your cloud flow using this if elseif else concept.

Any questions, please leave your comments below.

Share