Power Automate Desktop (PAD) Best Practices: Part 3 - Loop vs For Each
Michael Annis
Fractional Chief Automation Officer (fCAO) | Digital Transformation Consultant | CPA | Geneva Investor Accounting SME | Private Equity/Hedge Fund Automation |
When it comes to repetitive actions, any type of loop is paramount to simplifying flows and having the ability to conduct repeated actions as many times as necessary.
"Loop" allows you to process a specific number of times and you are able to identify a starting number, an ending number, and an interval. These numbers can also be identified by variables, allowing the "Loop" to be as restrictive or dynamic as we would like.
"For Each" defines these numbers by iterating through items in a list, columns in a datarow, or rows in a datatable, but is limited to only those 3 options.
What does this mean for us? Well, "For Each" is great if I just want to do something quick to each item in the list, and I don't need to reference it again. For example, if I just used "Get Files" which gets the full file path and name of all files in a particular folder, maybe I want to make another list of all their filenames (without the path). So, I could do:
However, let's say all the files are in Excel, listed from A1 to A?, and I want to put various information in columns B, C, and D. Could I do it with a "For Each"; absolutely, but I don't know what number "CurrentItem" is, so I would have to create that. Inside of my "For Each", I would have to put an index identifier, like this:
领英推荐
Now you know which one your on.
In a "Loop", that information is already available by the %LoopIndex% variable:
While setting the %ForEachIndex% is not difficult, I cannot count the number of times we have seen community users saying "I can't get my For Each to do...something!" and when we respond with, "make it a loop and do this," and it works every time.
In short, if you need to get through a list one time and quick, "For Each" is probably the faster way to go, but if you want a more accurate action that you can reference back to know where you are currently, "Loop" is going to be much easier for you to manage and is not limited to just lists, datarows, and datatables.
Michael Annis has no affiliation with Microsoft or its subsidiaries.?The “Best Practices” are purely compilations of his opinion and observations of conversations on the Power Automate Desktop User Community; an individual’s use of them should be taken as such.?Michael Annis has been using Robotic Process Automation (RPA) for over 2 years and maintains a “Top Solution Author” position on the Microsoft Power Automate Desktop User Community standings.?The best place for users to get their questions answered is through the User Community located here:?https://powerusers.microsoft.com/t5/Power-Automate-Desktop/bd-p/MPADesktop.
Jefe en Kroger
1 个月muhcas gracias, me sirvio mucho
Information Systems @TU Munich
2 年Very helpful, thanks ;)