How to use conditionals in Microsoft Power Automate Desktop

Microsoft Power Automate Desktop provides you with conditional actions to develop flows when you want to automate your tasks. 

What are conditionals? Conditionals allow you to modify a flow’s behavior based on certain conditions. Conditions may include the comparison of two values, or more specific information, such as the existence of a file or the contents of a web page.

There are 2 groups of conditional actions:

  • The If group of actions
  • The Switch group of actions

 

The If group of actions

The If group of actions allow users to evaluate whether a certain condition is true. The structure of the If conditions are:

  • Initial conditions (which includes all the If conditions)
  • Alternative condition (which may be added using the ‘Else if’ action) This condition will only be considered if all the previous conditions were false.
  • An Unconditional alternative may also be added, using the ‘Else’ action. This will run only if all the conditions before it has failed
  • All conditional blocks in the ‘If’ group end with the ‘End’ action

In this example, we will deploy some of the available If actions to understand what functions they have.

  1. Use a Get special folder action to retrieve the path to the desktop.
  2.  To check if a folder named Records exists in the desktop, use the If Folder Exists action, and configure its input (refer to image below):              

3.  Add an If File Exists action inside the If block to check if the Expenses Excel file exists inside the Records folder. Configure its input (refer to image below):

4.  If the file exists, move it to the desktop folder using the Move Files action. Otherwise, the user should be notified.

5.  Add an Else action inside the second if block, and a Display Message action to inform the user that the file has been moved.

6.  Finally, in case the folder itself does not exist, add an Else action inside the first If block, and a second Display Message action to inform the user that the Records folder does not exist.

Note : Remember to save your file.

When you run your flow, you should see this display message box stating that the Records folder does not exist when the condition falls under the Else statement.

Note : Do try this out with different scenarios to get a better understanding of the If action.

The Switch group of actions

The Switch group of actions is used when a flow’s next steps depend on the value of a specific variable.

Let’s go through an example to see the Switch group of conditions in action.

  1. Get the Current date time using GetCurrentDateTime action

2.  To get the current day of the week, use Switch action and enter the following inputs (refer to image down below)

The value indicated in the Switch, will be used for conditional checking

3.  To check if the day is Saturday, use the Case action. Use the Display message action to display the message box title “it’s Saturday” along with the message “Make the most of it!” when the CurrentDateTime action detects that it is a Saturday.

4.  Repeat these steps (using Case action and Display message action) for when it is a Sunday.  Use the Display message action to display the message box title “it’s Sunday” along with the message “Make the most of it!” 

5.  For other days, add display message “Let’s get cracking” with title “it’s weekday’ by using the Default case action

6.  Once you are done, save the file and run the flow. If it is currently a weekday, this will be how the display message box will look like.

Conclusion

To conclude let us go through the differences between the different conditional actions. An If Else statement can test expression based on range or values or conditions. Whereas a Switch statement is based only on a single integer, a numeric value or a string object. Using a Switch statement is usually more efficient than a set of nested If.