Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Automate - Building Flows
Answered

Is there a better way then 2 for each loops?

(0) ShareShare
ReportReport
Posted on by 150

Hi.
Been a while since I was here... but I have just created a flow that I can trigger on a file in a folder... the flow then gets all the files in that folder.
It then goes into an excelfile to get a table of contacts in which I have created a column with the filename for each contact.

 

After that I have two For each loops that goes through each line from the excelfile and each file in folder and when it finds the right file for the right contact it sends an email to that contact with the corresponding file.

And this I got to work... but there is 19 lines and 19 files... and of course that causes the flow to do 361 loops (one for each combination).


But I want to learn... so now I wonder if there is anyone who knows a "better way" to send the right file to the right contact.

Thanks in advance for any suggestions!

  • Verified answer
    FUS Profile Picture
    150 on at
    Re: Is there a better way then 2 for each loops?

    After some tinkering and experimenting I did found a slightly faster way.


    And instead of getting all files and all emails from the list and then loop through both until a match is found which cased 19*19 loops (361 loops)

    I looped the 19 rows and for each I did a "Get files" specifying the specific file for each receipient.

    Which in turn created a second For each loop... but with only 1 loop.
    This solution greatly decreased the number of loops (and the time) the flow took

     

    FUS_0-1685085632379.png

     

  • Matthy79 Profile Picture
    4,178 Super User 2024 Season 1 on at
    Re: Is there a better way then 2 for each loops?

    Sorry, but I can't help you in detail if you don't provide the data for each array and what should happen in each case (no hit found, maybe two hits found) so I just write down what I would do and hope that you can do it by yourself.

     

    After you get both arrays I would transform one of them to xml and do a select on the other one where I would perform a xpather to find the correct match (maybe joined together if it is possible for more matches).

  • FUS Profile Picture
    150 on at
    Re: Is there a better way then 2 for each loops?

    p.s. that compose I got there in the end is only there to help me with error handling during development

  • FUS Profile Picture
    150 on at
    Re: Is there a better way then 2 for each loops?

    Here is some images on my flow.
    I started out creating a flow that sent out the file I had selected... but I realised I wanted to do the same process for all 19 files in the folder... so I changed in to the following.

    If the conditions are false nothing happens... but still a lot of unnecessary repetition.
    And I actually had one test case where each contact got an email for each file... which is something I really don't want to happen when I'm finished... so some sort of failsafe would be appreachiated. 😅

    Flow1.pngFlow2.pngFlow3.png

  • Matthy79 Profile Picture
    4,178 Super User 2024 Season 1 on at
    Re: Is there a better way then 2 for each loops?

    Hello,

     

    you could try a select action and after that an apply to each. Can you provide sample data for each array? And what should happen if there is no match?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Paul Stork – Community Spotlight

We are honored to recognize Paul Stork as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 497 Super User 2025 Season 1

#2
David_MA Profile Picture

David_MA 436 Super User 2025 Season 1

#3
Riyaz_riz11 Profile Picture

Riyaz_riz11 244 Super User 2025 Season 1