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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Automate
Unanswered

Nested Apply to Each

(0) ShareShare
ReportReport
Posted on by 67
Hello,
I would like some help with a nested flow. MY flow is pretty simple- I want to get the list items from the trigger 'When an item is created or modified' and email to different users based on location. Following instructions from Power Automate - How to Filter Excel Unique Email Address Column Values & Send Email I've manage to work up to the step to loop through the locations. Now the problem is the nested apply to each. I want to send an email based on location and group by another value (such as product ID). So for example, if the items contain:
 
Location: Singapore, Product ID: 12346
 
Location: Singapore, Product ID: 12346
 
Location: Charleston, Product ID: 25688
 
Location: Charleston, Product ID: 25688
 
The flow will filter items based on location AND Product ID (like group with like) and send an email.  The flow should route the items based on location and then send an email (usually containing multiple items) based on distinct Product ID. I've managed to group emails and send one email based on user location. The problem is with the nested 'Apply to Each' to further filter by ID. I don't know how to get it to only send 1 email with a Switch control based on user location when the Switch is  in the second 'Apply to Each' Below is my flow:
I've looked at some tutorials and similar  questions/answers posted beforehand, but the previous answers that do answer my question, the images are not available so I can't see a big portion of how they solved this issue. Thanks!
Categories:
I have the same question (0)
  • Suggested answer
    Michael E. Gernaey Profile Picture
    53,335 Super User 2025 Season 2 on at
    HI
     
    To be honest its confusing, because the data isn't clear, You talk about filtering by Product ID, then by ID, what ID? I dont see a third ID
     
     
    I see things like BOL, Branch etc and none of that is explained. So what I would prefer to see is not a flow that doesnt work, but the literally steps written in step form that you want, not in a paragraph. With data examples.
     
    I also do NOT understand why you say route them by Location, but then say by Unique ID. If you do that you will NEVER have a list of all Products that are uniquely assigned across Locations
     
    You would need to Filter Top Line by Product ID and then Locations
     
    Here is a fast bullet list of what you would do, if you wanted to send 
     
    1. Get Items
    2. Use Select to ONLY pull the Product ID 
    3. Do a Compose 
    --union(body('Select'), body('Select'))
     
    Now you have a unique list of all Product IDs
     
    4. Do an Apply to Each, using the output of the compose
    --Inside
    -----Filter(body/value of the original Get Items where the Product ID = the current item in the Apply to Each
    -----Add a Select
    ---------Use the output of the Filter here and only select the Location Name and the Product Id is the value from the Apply to each
    ---------use a Create HTML table, 
    ---------send your email
     
    Now based on what you have.. you have to figure out how to take these steps into what you have. Or explain them as I said and I will help
  • Verified answer
    kona134 Profile Picture
    67 on at

    I managed to figure this out. The goal was to filter by a distinct ID, do a switch based on location, and filter by location. So, for example, if the inputs were: 

     ID 

    Location 

    123456 

    Jakarta 

    123456 

    Jakarta 

    321010 

    Jakarta 

    321010 

    Jakarta 

    901022 

    Ibiza 

    901022 

    Ibiza 

    544563 

    Dubai 

     I would want the outputs to be (each is its own HTML table and sent in an email): 

    ID 

    Location 

    123456 

    Jakarta 

    123456 

    Jakarta 

     

    ID 

    Location 

    321010 

    Jakarta 

    321010 

    Jakarta 

     

    ID 

    Location 

    901022 

    Ibiza 

    901022 

    Ibiza 

     

     ID 

    Location 

    544563 

    Dubai 

     

    In other words, each distinct ID would be grouped and then further filtered based on location and then sent to that location. To do this, I first followed the first couple of steps in this video (here) to group by distinct ID. 

    1. 1. Select the trigger. Mine is based off a SharePoint List, so I used ‘When an item is created or modified’ or ‘When an item is created’.  Use the trigger that holds your inputs. 

    1.  2. Choose ‘Select’. This is going to be your distinct ID. The ‘From’ is going to be the ‘body/value’ from the SharePoint trigger and ‘Map’ is going to be the name of your distinct ID (in my case, BOL) for your key. The ‘value’ of the ‘Map’ should be the value in the dynamic content that you want to filter from (so, in my case, field_1). 

     

    You may be wondering, “Where is ‘Get Items’?”. I found this step unhelpful since it would just be repeating my inputs and found it confusing. The flow works fine without it.  

     

    1. 3. Next, use a ‘Compose’. The union function will select the distinct values and get rid of any duplicates. 

    4. 4. Next, use a ’Parse JSON’. The ’Content’ will be the output from the previous ’Compose’ step.(You can get the output of the ’Compose’ step to get the JSON schema by running the flow and pasting it into the ’Use sample payload to generate schema.) 

     

    1. 4. Next. Choose ‘Filter Array’. The ‘From’ will be the ‘body/value’ from the trigger step and the filter query will be the distinct ID from the trigger step and ‘is equal to’ the distinct ID from the previous ‘Parse JSON’ step. This will automatically create an ‘Apply to Each’/’For Each’ loop. This step will retrieve the items from the trigger step and group them by your chosen distinct ID and loop through each record. 

     

    The ‘For Each’/’Apply to Each’ body will be from the ‘Parse JSON’ step, using the ‘body’ from the ‘Parse JSON’. 

     

    1. 5. We’re going to do another ‘Parse JSON’, but this ‘Parse Json’ is used for parsing the ENTIRE collection from the 1st Filter Array so we can use it to filter our distinct IDs by location. So elements would include all fields (e.g. Product Type, Comments, etc.). This will be inside the ‘For Each’/’Apply to Each’ step. 

     

    The flow so far should look like this (ignore the actions with lines through them): 

     

    1. 6. (This step and every subsequent step is inside the Apply to Each/For Each!) Next, choose another ‘Select’. This ‘Select’ is for the location to be used in the ‘Switch’ action later on. The ‘Body’ of this Select’ will be from the previous ‘Parse JSON’ step. The value will be whatever you desire your ‘Switch’ (again, later on) to be. You can reference the field you want to use in the ‘value’ portion by using: 

     item()?[‘internal ID name’]. 

     

    1. 7. In my case, each distinct ID has only one location associated with it, so I can grab the common value and get rid of any duplicates. We are going to use another ‘Compose’, using union (again), but this time our union is going to use the location instead of the distinct ID. Example posted below: 

     

     

    1. 8. Now, we’re going to use another ‘Parse JSON’ action, with the ‘Content’ being the output from the previous ‘Compose’ action. Again, determine the schema by using the output in the ‘sample payload’.  

     

    1. 9. Almost done! Now we’re going to get the location (in my case ‘Branch Name’ ) by doing another ‘Compose’. This gets the result from the previous ‘Parse JSON’ and turns it into a string to use in the ‘Switch’ action next. In case the font is too small, use: 

     body(‘Parse JSON’)[0][‘Parse JSON key name’] 

     

    1. 10. Now we’ve finally reached our ‘Switch’ action! Use the ‘Compose’ from the previous step in the Switch’s ‘On’ field. 

     

    1. 11. In the Switch’s Case for the parameters (the ‘Equals’ field) put whichever location you would like. For our example, we will use Jakarta. 

     

    1. 12. To get only ‘Jakarta’, you will need to use another ‘Filter Array’ action in the Switch case. For the ‘Body’ use the Parse JSON in Step 5. For the filter query, choose the appropriate value field which can be referenced by using: 

     item()?[field_name’] 

    And ‘is equal to’ to whatever the location you want to filter by (example below): 

     

    1. You can now use the previous ‘Filter Array’ to create HTML tables, style them according to your preferences and send in an email. Thank you! 

    2. *Note: Each location needs to have its own switch case and 'Filter Array' (step 12).

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 522 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 364 Moderator

#3
abm abm Profile Picture

abm abm 243 Most Valuable Professional

Last 30 days Overall leaderboard