In this Power Automate Tutorial, we will discuss how to set variable based on condition in Power Automate using different examples.
Set Variable Based on Condition in Power Automate
Using Power Automate, let us see how to set a variable based on condition.
Example-1
In this example, we will see how to initialize a string variable and set the string value based on the condition using the set variable action in Power Automate.
- Initially, log in to Power Automate and click on the + Create -> Instant Cloud Flow.
- Then Build an Instant Cloud Flow window will appear; enter the Flow name, select the trigger ‘Manually trigger‘ and then Click on the Create option as highlighted below:

- Now, we will initialize a string variable. To do that, select the ‘+New Step‘ option, provide the variable name, and choose the variable type as ‘String‘.

- Then click on +New Step, to add the condition action; if the condition matches the initialized variable, then set the variable as true, else set the variable as false as highlighted below:

- Select +new step, add compose action to see the set variable action value, and pass the variable value as the input value.

- To test the flow, Choose Manually and Select the Save & Test option as displayed below. Once the flow has been saved, select the RunFlow option.

- As the Flow was run successfully, we can see the result in the compose action that displays the set variable value.

- In the same way, we will try to execute the else value by changing the condition value as highlighted below:

- Now save and run the Power Automate flow, and we can see the result in the compose action, which displays the set variable value.

This is how to set variables based on conditions using Power Automate.
Example:2
In this example, we will see how to set variables inside the Power Automate switch action.
Recently, I was working on Power Automate, where I was supposed to set a variable for further use based on the value of an option set field in an entity. Here, we will discuss it in detail.
- So, for this, I have created an instant cloud flow in Power Automate. Add a +New step to initialize the variable, which is of string data type, and manually pass the Product value that you have to check with the condition.
- The screenshot below shows that I have passed the Product Value as VTT, as highlighted.

- Now, select the + New step and add the switch control action. If the product value matches in the case section, we will set the variable value to true; otherwise, we will set the variable value to false for the default section.

- Select + New step, add a Compose action to see the set variable action value, and pass the variable value as the input value.

- Click on the Save & Test option below. Once the flow has been saved, select the RunFlow option. As the Flow was run successfully, we can see the result in the compose action, which displays the set variable value.

- In the same way, we will change the Product value as Velo in the Switch control action as shown below:
- If the condition Product value doesn’t match the initialized product value, then it executes the default section in the switch control action in Power Automate.

- Save the changes made in the flow and run the flow. Once the flow has been run, we can see the expected result value as displayed below:

This is how to set variables inside the Power Automate switch action.
Furthermore, you may like some more Power Automate tutorials:
- Create a Zip File Using Power Automate
- Get Start and End of Month in Power Automate
- Power Automate isInt() Function
- Add Days to Date in Power Automate
In this Power Automate Tutorial, we have discussed how to set variables based on conditions in Power Automate with different examples.

Preeti Sahu is an expert in Power Apps and has over six years of experience working with SharePoint Online and the Power Platform. She is the co-author of Microsoft Power Platform: A Deep Dive book. As a Power Platform developer, she has worked on developing various tools using Power Apps and Power Automate. She also makes Microsoft 365 videos and shares them on YouTube.