Power Automate - Writing Efficient Flows with the Switch Operator

Sean Roque, 04 March 2022

In a Power Automate, the Switch Operator is a built-in action that allows us to execute a unique branch of flow actions depending on the value of a variable. This essentially functions the same as nesting Condition operators, except that it’s much more simplified, while providing a built-in ‘default’ branch as well.

In our example, we created a flow that on update of account, runs logic on its child contacts depending on the type of contact. For example, the account “The Army of Eternal Salvation” below has 4 child contacts each with a different value for the “Contact Type” field.

image

In this scenario, we want to record the Full Name (assuming there is only one of each per account) of the General, Captain and Agent. The rest of the contacts Full Names will be appended to a comma separated variable called “Who else came”.

Retrieving Data

1. This is easily achievable by first initializing variables using the Compose connector:

image

2. A list rows action using the Dataverse connector to list all the child Contacts for that Account

image

3. Finally, an Apply to Each operator with switch operators checking the “Contact type”, we can use the Set Variable action to populate each variable with the Full Name when the corresponding “Contact type” matches. For example, under this Apply to Each, you can see our condition checking the “Contact type” option set if the value is 100000002 or as per its label: “Captain”.

image

Nested Condition Statements

Now that we have our data being retrieved from Dynamics and our variables initialized, let’s explore the various ways we can use Control actions to achieve our requirement.

Firstly, we can build on the “No” branch of the condition to add another condition to check and set the variable with the General’s Full Name, the Agent’s Full Name and so on. The innermost “No” branch can contain the logic to append the Full Name to our “Who else came” variable.

image

image

However, you may notice that this can get more and more difficult to manage, depending on the amount of Contact types we have to accommodate for.

We also have the option of doing subsequent conditions as shown below. Where the bottommost condition for setting the variable “Who else came” is checking if the “Contact type” value does not match any of the other conditions above.

Note: the reason this is required is because Flow loops do not yet have an action that tells the loop to skip to the next iteration.

image

The issue with this is that a condition operator can only have a maximum of 10 rows per group in the criteria definition. As you can see in the image below, after adding total 10 rows, the “+Add” button is greyed out. This means that if we wanted to run logic on over 10 Contact Types e.g. Lieutenant, Major etc. then the bottommost condition will not be viable.

image

Switch Operator

image

By using the Switch Operator, not only are you able to handle the operations we described above, but the flow will also run more efficiently. You can easily add parallel switch cases for each value of Contact type. This is more efficient because unlike our previous examples, there will be no need to keep running flow steps and multiple condition checks.

image

It also provides a ‘default’ branch, which will allow us to set and concatenate values for the “Who else came” variable easily. image

The Switch operator is simpler to implement and is a more efficient method of setting variables from extracting values within a flow loop. Due to the ‘default’ branch, it also supplements the lack of a ‘break’ or ‘continue’ action that allows loops to iterate to the next item. Tools like this are what makes Power Automate an awesome product for solving simple, and even the more complex requirements. Come and see what the Power in Power Automate is and go ahead and try it out!