web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / If Tree to iterate Sev...
Power Automate
Answered

If Tree to iterate Several Folders' Items

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

I'm struggling to word this meaningfully so if anything more needs to be said from me, please let me know.

 

I've created two Flows. One Flow puts all of the attachments from my emails into a folder. The same flow iterates over these items to determine which month folder to put them in determined by the text present in the name. The number of items put into any month folder is counted and turned into a variable. That count variable is passed to the second Flow.

 

The second Flow determines if there are any files in any of the month folders using an if statement tree, "If CountVar >= 1 Then"... This part is working flawlessly. When I have 11 items for October, 11 files are put into my October folder and 11 items are passed to this second Flow.

 

The issue is that the second Flow will always iterate only one folder's items. As though it can only "see" the one count variable.

 

My current Flow If / Else If statements are designed as such:If Tree Design.PNG

(FileCount - FileCount gets me to a solid starting point to count up again. I know I have a number items from my previous Flow, but I want to make certain that this second Flow is actually counting/iterating all of the items. Later in my For-Each loop I add +1 to the count variable to return back to the original count... assuming everything went without hitch.)

 

The first If statement is to check December. I figured this part was rather arbitrary, as to which end of the year I begin my evaluation from.

 

The Else Ifs flow from December to January and each evaluate the same thing. If the var passed from the first flow is 1 or greater, then check the contents of that folder and later grab those files for further processing.

 

As I was saying earlier, this process works flawlessly... once. I could have 60 items for October and 5 for November, and since November iterates first, only 5 November are processed. The purpose of passing this file count var is to prevent unnecessary checks and processing. If I know for a fact I only have files to do with October, there's no point in checking January through September, or November and December.

 

I've been working on this project in small pieces for about 2 weeks now. I'm starting to go cross-eyed trying to battle my way through new issues that keep cropping up due to oversight or poor design.

 

I need this Flow to accept whatever number variable it was passed and do an action for each corresponding folder instead of exclusively performing actions on whichever folder comes first numerically.

I have the same question (0)
  • Verified answer
    MichaelAnnis Profile Picture
    5,727 Moderator on at

    These all need to be separate if statements. ElseIF will not run if the first if statement is met.

     

    Therefore, you need it to look like this. 

    IF augnotenum >=1 Then

     ‘Do something

    EndIF

    IF julnotenum >=1 Then

     ‘Do something

    EndiF

     

    that way each one will be ignored if it is 0 and it will attempt to process all of them. 

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    I see now. Thank you, MichaelAnnis!

     

    I should've been thinking in my VBA headspace. I've had to do several nested if-trees where the "do this" action is predicated on one or two determinations.

     

    Thank you again!

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    I gave this a swing and I couldn't get it to produce the desired behavior. I think this has to do with my use of the For-Each loop.

     

    The condition to fire the For Each loop, and what to iterate over, are predicated on the results of any single If statement above it. Whatever If evaluates to true provides the For Each the target folder. That methodology right there is the issue.

     

    To continue using the above method I mentioned I formatted my Ifs to be as you had, Michael.

     

    If NovNoteNum >=1

    Do something

    End If

    If OctNoteNum >=1

    Do something

    End If

     

    Except, in addition to the reformatting I've also added an additional If statement after the For Each loop and a new boolean variable.

     

    If NovNoteNum >=1

    NovCounter = 1

    Do something

    Go To For-Each loop starter label

    ... Some code...

     

    At the end of the For Each statements:

    If NovCounter = 0

    Go to NovNoteNum If statement label

     

    This allows for the desired effect from the Ifs but not so directly. Now the whole process must complete before it can target a new folder with new items to iterate, but at least it works from end to end.

     

    Thank you very much for your help, MichaelAnnis!!!

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 274 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 175

#3
Haque Profile Picture

Haque 166

Last 30 days Overall leaderboard