In this Power Automate Desktop tutorial, we will see how to use different Loop actions in Power Automate Desktop.
What is Loops in Power Automate Desktop? Loops are fundamental in desktop flow creation and extremely useful in complex flows. A loop’s principal purpose is to make a desktop flow repeat one or more activities several times.
Different types of loops are available in Power Automate Desktop. These are:
- Loops (or Simple Loops)
- Loop condition
- For each loop
Simple Loops Action in Power Automate Desktop
Here we will see what is Simple loop action and how to use it in Power Automate Desktop.
Simple loops in Power Automate Desktop will iterate the block of actions for a specified number of times.
For example, we will take the input from the user once and write in the text file provided below.
Step 1: Open Power Automate Desktop, provide the flow name, and click the Create button.
Step 2: We will take the user input, so expand the Message box section and drag and drop the ‘Display Input Dialog’ action. Then provide the below information:
- Input dialog title: Provide the title like below.
Step 3: Now we will loop through the user input 4 times, and write in the text file. Then expand the Loops section in PAD, drag and drop the Loops action, and provide the below information:
- Start From: The Loop will start from 1.
- End to: Loop will end at 4
- Increment by: the counter value for the loop is 1.
Step 4: Now, we will write the user input in the text file. For this, create a text file and copy the address. Then, expand the File section, and drag and drop the ‘Write text to file’ action. Then provide the below information:
- File path: Provide the file path you have copied.
- Text to write: Provide the text like below:
Step 5: Now run the flow by clicking on the run button. Now open the text file and you can see the 4 the time the text is written.
This is an example of simple loops in Power Automate Desktop.
Loop condition action in Power Automate Desktop
Here we will see What is loop condition action and how to use this action in Power Automate Desktop.
As long as a condition is true, the Loop condition allows a desktop flow to repeat one or more operations.
For example, we will create a desktop flow that will receive user input as a number and generate a random number between 1 and 10; if the input number equals the random number, the success message will be displayed.
Otherwise, it will loop through and take the user input; if the random number and the number you estimate are too high, guess another number; similarly, if the user input is lower than the random number, guess another number. This loop will be executed if the user input number is not equal to the random number.
Step 1: Open Power Automate Desktop, provide the flow name, and click the Create button.
Step 2: This step will generate a random number, so expand the variable section and drag and drop the Generate random number action. Then provide the below information:
- Minimum value: Provide the minimum value as 0.
- Maximum value: Provide the maximum value as 10.
Step 3: Next, we will take the user input, so, expand the Message box section. Then drag and drop the Display Input Dialog action. Then provide the below information:
- Input Dialog Title: Provide the title like below
- Input Dialog Message: Provide the message like below
Step 4: The above user input is of text data type, so we will convert it to a number. For this, expand the Text section -> then drag and drop Convert text to number action and provide the below information:
- Text to convert: click on {x} variable, select the UserInput, and then click on Save.
Step 5: Now we will loop the condition until UserInput is not equal to the random number, so expand the loops section -> select Loop condition action. Then provide the below information:
- First Operand: click on {x} variable, select the RandomNumber, and then click on Save
- Operator: Select the operator as Not equal to.
- Second Operand: click on {x} variable, select the UserInput, and then click on Save.
Step 6: Now we will check whether the user input is less than a random number or not, so expand the Condition section, then drag and drop the IF action. Then provide the below information:
- First Operand: Provide the first operand as a TextAsNumber variable.
- Operator: Select the operator as less than
- Second Operand: Select the second operand as RandomNumbervariable.
Step 7: Next, we will take the user input, so, expand the Message box section. Then drag and drop the Display Input Dialog action inside the if action. Then provide the below information:
- Input Dialog Title: Provide the title like below
- Input Dialog Message: Provide the message below.
Step 8: The above user input is of text data type, so we will convert it to a number. For this, expand the Text section -> then drag and drop Convert text to number action and provide the below information:
- Text to convert: click on {x} variable, select the UserInput, and then click on Save.
Step 9: Now we will check whether the user input is greater than a random number or not, so expand the Condition section, then drag and drop the Else IF action. Then provide the below information:
- First Operand: Provide the first operand as a TextAsNumber variable.
- Operator: Select the operator as greater than
- Second Operand: Select the second operand as RandomNumbervariable.
Step 10: Next, we will take the user input, so, expand the Message box section. Then drag and drop the Display Input Dialog action inside the if action. Then provide the below information:
- Input Dialog Title: Provide the title like below
- Input Dialog Message: Provide the message below.
Step 11: The above user input is of text data type, so we will convert it to a number. For this, expand the Text section -> then drag and drop Convert text to number action and provide the below information:
- Text to convert: click on {x} variable, select the UserInput, and then click on Save.
Step 12: At last we will display the success message, so expand the Message box section, then drag and drop the Display Message action. Then provide the below information:
- Message box title: Provide the title like below
- Message to display: Provide the message below.
Step 13: Now run the flow by clicking on the run button. Then provide the user Input here I have provided 5.
When the guess is correct, it will display the message below. Otherwise, it will ask to guess the number.
This is an example of Loop condition action in Power Automate Desktop.
For each loop action in Power Automate Desktop
Here we will see what is for each loop and how to use it in Power Automate Desktop.
For each loop in Power Automate Desktop loops a list (or data table) and saves the current element in a variable. Its principal function is to retrieve each element in a list (or row in a data table) and use it in the following actions.
For example, we will create a flow that will loop through the list of numbers, and check whether each number is an even number or not. If it is an even number then it will show the message is an even number else it will display that it is an odd number.
Step 1: Open Power Automate Desktop, provide the flow name, and click the Create button.
Step 2: Now we will create a List variable called Numbers, so, expand the variable section. Then drag and drop the Create new list action to Canvas and provide the below information:
- Now provide the new list name as Numbers, then click on Save.
Step 3: Next, we will add names to the list, for this expand the variable section, then drag and drop the ‘Add item to list ‘ action to canvas. Then provide the below information:
- Add item: Provide the item name ‘1’.
- Into List: Click on the variable icon {x} -> select Names, then click Save.
Similarly, add numbers to the list Numbers. Here I have added the numbers 2, 3, and 4, as you can see in the screenshot below.
Step 4: Now we will loop through each item, so expand the loops section, and then drag and drop the For each action and provide the below information:
- Value to iterate: Click on variable icon{x}, select the Numbers, and click on Save.
Step 5: We will see the current number %2 =0, then it is an even number, For this expand the condition section -> drag and drop the If condition action. Provide the below information:
- First Operand: Provide the first operand as ‘%CurrentItem mod 2%’
- Operator: Select the operator as equal to.
- Second Operand: Select the second operand as 0.
Step 6: Now we will display the message, so expand the Message box section, then drag and drop the Display Message action inside the If action. Then provide the below information:
- Message box title: Provide the title like below
- Message to display: Provide the message below.
Step 7: Next, we will add an else action, so drag and drop the Else action from the condition section.
Step 8: We will display the message a number is an odd number, so from the message box section, drag and drop the Display message action inside the Else action. Then provide the below information:
- Message box title: Provide the title like below
- Message to display: Provide the message below.
Step 9: Now run the flow by clicking on the run button, it will loop through each item and display the message based on the condition. Like, if 1 is an odd number, so, it will display ‘it is an odd number’ if it is 2 ‘it will display it as an even number’.
This is an example of the ‘For each’ loop action in Power Automate Desktop.
Conclusion
In this Power Automate Desktop tutorial, we saw what loops and what are the different types of loops available in Power Automate Desktop. Also, we saw how to use Loop Actions in Power Automate Desktop.
You may also like the following tutorials:
- Send Email Reminders from a SharePoint List using Power Automate
- Power Automate desktop concatenate strings
- Extract substring from text using Power Automate Desktop
- Power Automate Desktop Mathematical Operations
- How to Convert Data Table Columns to List in Power Automate Desktop?
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com