Power Automate allows you to automate workflows between different applications and services, enhancing productivity by reducing manual tasks. Among the most essential concepts in creating effective flows are Variables and Conditions. These tools are fundamental for adding logic, storing temporary data, and controlling the flow of execution based on specific criteria.
In this guide, we’ll take a deep dive into how Variables and Conditions work in Power Automate, and how you can leverage them to enhance your flows.
What are Variables in Power Automate?
A Variable in Power Automate is a container used to store data temporarily while a flow is running. Variables allow you to track and manipulate data within your flow. You can use variables to store values such as numbers, text, dates, or even arrays (lists of values).
Variables are extremely useful in flows that involve multiple steps where you need to retain data and use it later in the flow. For example, you might use variables to store user input, calculation results, or a list of items that you need to process sequentially.
Types of Variables:
- String Variables: Used for storing text data.
- Example: Storing the name of a person.
- Integer Variables: Used for storing whole numbers.
- Example: Storing a count of items processed.
- Boolean Variables: Used to store true/false values.
- Example: Storing a flag to check if a condition has been met.
- Array Variables: Used to store multiple values (lists of items).
- Example: Storing a list of emails to be sent.
- Object Variables: Used for storing complex data structures (like JSON objects).
- Example: Storing the details of a SharePoint list item, such as its title, date, and description.
How to Use Variables:
- Initialize Variable:
- This is the first step in using a variable in your flow. You initialize a variable by defining its name, type, and initial value.
- Example: Create an integer variable called “Count” and set it to 0.
- Set Variable:
- After initializing, you can change the value of the variable during the flow. Use the “Set variable” action to assign new values to the variable.
- Example: Update the “Count” variable to increase by 1 every time an item is processed.
- Increment Variable:
- This is specifically used for adding a number to a variable, typically used for counting.
- Example: Use the “Increment variable” action to increase the “Count” variable by 1 after each loop.
- Use Variable:
- You can reference the variable in various actions throughout your flow. For instance, you can use a variable in a “Send an email” action to include dynamically calculated data.
Example:
Imagine a scenario where you need to track the number of files in a folder and send an email once a certain threshold is reached.
- Initialize Variable: Create a variable called “FileCount” and set its value to 0.
- Loop through Files: Use an action like “Get files (properties only)” to fetch files from a folder.
- Increment Variable: Inside a loop, increment the “FileCount” variable every time a new file is found.
- Condition: After the loop, check if “FileCount” exceeds 10. If it does, send an email.
What are Conditions in Power Automate?
A Condition in Power Automate allows you to add logic to your flow, enabling it to execute different actions based on whether certain criteria are met. Conditions let you define branches in your flow, which can be used to check whether data matches certain conditions and then decide what to do next.
Conditions are used for decision-making, such as checking if a number is above a certain threshold, verifying if a field is empty, or comparing values from different data sources.
How to Use Conditions:
- Define a Condition:
- A condition is expressed as an “if-then” statement. You specify a logical expression, such as “If X equals Y”, and the flow will follow one path if the condition is true and another if it is false.
- Example: “If the status of an item is ‘Approved’, then send a confirmation email.”
- Choose Operators:
- Power Automate provides several logical operators that you can use to evaluate data in a condition:
- Equals: Checks if two values are the same.
- Not equal to: Checks if two values are different.
- Greater than / Less than: Compares numerical values.
- Contains: Checks if a text field contains a specific substring.
- Is empty: Checks if a field is empty.
- Power Automate provides several logical operators that you can use to evaluate data in a condition:
- Branching Logic:
- After defining the condition, you can set actions for both the Yes branch (when the condition is true) and the No branch (when the condition is false).
- Example: If the item status is “Approved”, send an email. If the item status is not “Approved”, send a reminder email.
- Nesting Conditions:
- You can nest multiple conditions within each other, enabling you to check for multiple criteria at once.
- Example: Check if a file is in a specific folder, and if it is, verify that the user has the correct permissions before performing an action.
Example of Condition in a Flow:
Let’s say you want to automate the approval process for an expense report. The flow should check if the amount exceeds $500 and send the report for higher-level approval if it does.
- Trigger: When an item is created in the SharePoint list for expense reports.
- Condition: If the “Amount” field is greater than $500.
- Yes: Send an approval request to a manager.
- No: Automatically approve and notify the employee.
- Final Action: Based on approval, send the final email notification.
Using Variables and Conditions Together
One of the most powerful ways to use Variables and Conditions together in Power Automate is to track and control flow execution based on dynamic values.
Example Flow:
Consider a scenario where you are processing a list of orders. If an order value exceeds a threshold, you want to process it for expedited shipping, and if not, proceed with regular shipping.
- Trigger: When a new order is added to a SharePoint list.
- Initialize Variable: Set a variable “OrderValue” to the value of the order.
- Condition: If the “OrderValue” is greater than $1000, then:
- Yes: Update the order status to “Expedited” and send a notification to the warehouse.
- No: Update the order status to “Regular” and send a confirmation email to the customer.
Using variables allows you to capture the order value and use it for conditional logic in subsequent steps, ensuring that the flow adapts based on real-time data.
Best Practices for Using Variables and Conditions
- Keep Variable Names Clear: Name your variables descriptively so that it’s easy to understand their purpose.
- Limit the Number of Variables: Avoid creating too many variables unless necessary, as this can complicate the flow.
- Test Conditions Thoroughly: Make sure your conditions are well-defined and test them to ensure the correct actions are being triggered.
- Use Scope and Apply to Each for Loops: When working with multiple actions that need to reference the same set of data, use loops and scopes to keep your flow organized.