Power Automate Desktop (PAD) Best Practices: Part 3 - Loop vs For Each

Power Automate Desktop (PAD) Best Practices: Part 3 - Loop vs For Each

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:

No alt text provided for this image

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:

No alt text provided for this image

Now you know which one your on.

In a "Loop", that information is already available by the %LoopIndex% variable:

No alt text provided for this image

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.

Enzo Balderrama

Jefe en Kroger

1 个月

muhcas gracias, me sirvio mucho

Jakob Moehler

Information Systems @TU Munich

2 年

Very helpful, thanks ;)

要查看或添加评论,请登录

Michael Annis的更多文章

社区洞察

其他会员也浏览了