How to Set Variable Action in Power Automate?

While working on Power Automate, I needed to automate a sales discount approval process. The “Set Variable” action helped me manage variables like DiscountAmount, ApproverComments, and ApprovalStatus. I thought it would be helpful to explain how the Set Variable action works in Power Automate and how we can use it in different scenarios.

Also, we will discuss the following points:

  • Power Automate initialize variable action vs set variable action
  • Set variable to null in Power Automate
  • Set variable if in Power Automate
  • Set variables from Excel in Power Automate

What is Set Variable Action in Power Automate?

In Power Automate, “Set Variable” is an action that lets you store a value in a variable. This value can be a piece of text, a number, or other data that you want to use later in your flow. It’s like saving information in a temporary box that you can refer to whenever needed as your flow runs.

In Power Automate, before we use the “Set Variable” action, we must first use the “Initialize Variable” action. Initialize Variable action creates the variable and sets its initial value.

The Properties of Power Automate Initialize Variable action:

  • Name: Give your variable a name.
  • Type: Choose the type of data it will store (e.g., String, Integer, Boolean, Float, Object, Array).
  • Value: Set the initial value of the variable.
Power Automate Initialize Variable action Properties

The Properties of Power Automate Set Variable action:

  • Name: Choose the variable you initialized earlier.
  • Value: Set the new value for the variable.
Power Automate set Variable action Properties

Let’s see some of the comparisons between initialize variables and set variables.

Power Automate Initialize Variable VS Set Variable

Initialize variableSet variable
However, set variables can be used at the local level, which means we can set the variable with the value in the condition, loop, and scope.But in the Set variable, we will use the variable which we have initialized and set the value.
In the Initialize variable, variables are declared globally, not in the condition, loop, and scope.But set variables can be used local level, which means we can set the variable with the value in the condition, loop, and scope.

Set Variable to Current Date in Power Automate

Let’s see how to set variables to the current date in Power Automate.

Create an Instant cloud flow. Give the flow name and select the trigger ‘Manually triggered Flow.’ Click on Create.

date variable power automate

Click the + icon to add action, then search and Select the initialize variable action. Provide the variable Name as Date and Type it as a String.

power automate set variable to current date

Now, we can set the variable to the current date, so click on the + (Add an action). Select Set variable action, then select a variable Name as Date and set the Value with the below expression, which returns the current date:

utcNow()
power automate set variable to current date and time

Now click on Save and run the flow manually. You can see that the current date is assigned to a variable.

Set Variable to Current Date in Power Automate

This is an example of a Power Automate set variable to the current date.

Set Variable as null in Power Automate

Here, we see how to set our variable as null using the set variable action in Power Automate. To do this, follow the below steps:

Create an Instant cloud flow. Give the flow name and select the trigger ‘Manually triggered Flow.’ Click on Create.

set variable power automate

Select the Initialize variable action to initialize a variable with a date value. Provide the variable Name, then type it as a String and the value with the expression below.

utcNow()
how to set variable in power automate

Now, we need to set the data variable to null. Add an action to set a variable, then select the variable name from the dropdown menu as ‘Date.’ Use the insert expression feature to set the data variable to null with the following expression.

null
create variable in power automate

Now click on Save and run the flow manually. You can see that the date variable value is stored as null.

Set Variable as null in Power Automate

Set Variable as Expression in Power Automate

Here, we will see how to use the expression to set a variable using Power Automate to set variable action.

While working with Power Automate, I needed a flow that retrieves the parent directory name. The requirement is to monitor a SharePoint document library with many folders. Whenever a new file or folder is created, I must capture its parent directory name.

Microsoft Flow set variable expression

Let’s see how we create:

1. In Power Automate, select the trigger ‘When a File is created (properties only).’ Then, Provide the site address and library name.

power automate variables expression

2. Click the + icon to add action, select compose action, then in the Input box, click insert expression, and write the below expression:

split(triggerBody()?['{Path}'],'/')
power automate set variable to null

3. Then, add the Initialize variable action to store the parent folder, provide the variable name as parentFolder, and type it as a string.

power automate set variable expression

4. Now again, add compose action. In input, write the below expression:

The expression varies based on the action name you add.

length(outputs('Compose'))
how to use set variable in power automate

5. In the last, select Set variable action, then select the variable name from drop-down, and then in value, write the below expression:

outputs('Compose')[sub(outputs('Compose_1'),2)]
Power Automate Use variable in expression

6. Now click Save to run the flow and create a file in the folder. Here, I created a file in the Annual_Sales_Report. Check the screenshot below.

power automate increment variable

So, in the set variable, the output value must be Annual_Sales_Report.

Set Variable as Expression in Power Automate

This is an example of a Power Automate set variable expression.

Set Variable Based on Condition in Power Automate

While working with Power Automate on a leave request approval flow, I needed to set variables based on conditions. For example, if an employee requested one day of sick leave, the ApprovalStatus variable was set to “Approved.” For more than three days of sick leave, it was set to “Pending Manager Approval.”

Here, I will show you how to set variables based on conditions using the Power Automate Set Variable action.

1. Create an Instant cloud flow. Give the flow name and select the trigger ‘Manually triggered Flow.’ Click on Create.

Microsoft flow set variable if

2. Select Initialize variable action, then set the variable Name as DaysRequested, Type as Integer, and Value as 1.

power automate set variable if

3. Again add Initialize variable action, then set the variable Name as ApprovalStatus, Type as String.

power automate set variable based on condition

4. Add the condition action if the condition matches the initialized variable, like if DaysRequested is equal to 1. Here, take DaysRequested from dynamic content.

Set Variable Based on Condition in Power Automate

5. In the True section, add the set variable action and set the ApprovalStatus as Approved. Then, in the False section, add the set variable action and set the ApprovalStatus as Pending Manager Approval.

power automate set variable if statement

6. Now click on Save and run the flow manually. You can see the True section run, and the ApprovalStatus is Approved because, in the first, we initialized the variable DaysRequested value as 1.

Power Automate Set Variable Based Condition

This way, you can set the variable based on the condition using the Power Automate Set variable action.

Set Variable from Excel in Power Automate

Last weekend, I created a Power Automate flow to fulfill the HR team’s requirement for quarterly employee reviews. The flow automatically imports review data from an Excel sheet named “Quarterly_Reviews” into a SharePoint list.

Check the below screenshot of the Excel sheet:

This Excel sheet presents one of my SharePoint Document Library.

Power automate set variable in apply to each

Before we create a Flow, we have to create a SharePoint list like the below screenshot.

Column NameData Type
Employee [Title Column]Single line of text
CommentMulti lines of text
Performance ScoreNumber
Review DateDate and time
power automate variable

Let’s see how we can import:

1. Select the manually triggered Flow in Power Automate, then click Next step.

Microsoft flow set variable if

2. Add an Initialize variable action and set the variable Name to ReviewDate and Type to String.

power automate set variable from excel cell

3. To get the data from Excel, select the ‘List rows present in a table‘ action. Then, provide the Location, Document Library, File, and table.

add file power automate variable

4. Next, click on add an action to add Apply to each action, then set the Excel value.

Using an Excel cell as input variable for Power Automate

5. Inside apply to each action, click on Add an action and then select Set variable action. Now provide the variable name, and in value, write the below expression.

addDays('1899-12-30',int(item()?['Date']),'yyyy-MM-dd')
Store and manage values in variables in Power Automate

You may think you can directly import data into the SharePoint List, but the format is different, so this expression converts the Excel date format to a SharePoint-compatible date format.

6. Now, we will import the Excel sheet to the SharePoint list. So, inside apply to each add another action, select the Create item action. Now, provide the site address and list name from the dynamic content. Then, map the Excel value with the SharePoint list field.

power automate set variable from excel

When I tried to add the Performance Score, it was unavailable in the dynamic content. So, I went to the SharePoint list and changed the Performance Score data type to “Single line of text”. After refreshing the flow page, I added the Performance Score from the dynamic content, saved the flow, and then went back to the SharePoint list to change the Performance Score data type back to “Number.”

Now click Save and run the flow manually, and you will see the updated SharePoint list.

Set Variable from Excel in Power Automate

Set Variable as the Boolean value in Power Automate

While working with Power Automate for the finance department, I received a requirement: When an expense report is submitted, the flow checks if the total amount exceeds $5000. If it does, the flow sets the “RequiresSeniorApproval” variable to true; otherwise, it sets the “RequiresSeniorApproval” variable to false.

In this example, I will explain how to set a boolean value using the Power Automate set variable action.

So go to the Power Automate site and Create an Instant cloud flow. Give the flow a name and select the trigger ‘Manually triggered Flow.’ Click on Create.

Microsoft flow set variable if

Select Initialize variable action, then set the variable parameters Name as Total Amount, Type as Integer, and put the Value as 6000.

power automate set variable boolean

I am taking the Total Amount using the Initialize variable, which you can take as per your requirement.

Again, Select Initialize variable action, then set the variable parameters Name as RequiresSeniorApproval, Type as Boolean, and put the Value as false.

power automate set date variable

Add the condition action if the condition matches the initialized variable, like if the Total Amount is greater than 5000.

power automate set variable boolean using Set variable action

Add the set variable action in the True section and set the RequiresSeniorApproval as true. Then, in the False section, add the set variable action and set the RequiresSeniorApproval as false.

Set Variable as the Boolean value in Power Automate

Now click on Save and run the flow manually. You can see the True section run, and the RequiresSeniorApproval is true because, in the first, we initialized the variable Total Amount value as 6000.

power automate boolean variable

This is an example of a set variable boolean value using Power Automate set variable action.

I hope you followed all those examples. Now, you have an Idea of how to use set variable actions in Power Automate.

You may also like:

>

Build a High-Performance Project Management Site in SharePoint Online

User registration Power Apps canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

Power Platform Tutorial FREE PDF Download

FREE Power Platform Tutorial PDF

Download 135 Pages FREE PDF on Microsoft Power Platform Tutorial. Learn Now…