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 Platform Community / Forums / Power Automate / Working with Variables...
Power Automate
Unanswered

Working with Variables in Apply to Each

(0) ShareShare
ReportReport
Posted on by

Hi all,

 

I think I should know this but I just can't get there.

 

I have 2 apply to each loops. In the 2nd loop, I'm appending to a string variable a name, I then get the outputs of this variable in the 'Compose' below it, which is outside the 2nd loop but still inside the first loop. I'm using append because if there's 3 names in the 2nd loop I want to append the 3 names together.

 

This works fine for the 1st loop, however, when the 1st loop moves on to the next value the append variable updates with more names - this I don't want!

 

What I'm looking to achieve is for the variable to store all the names from the 2nd loop but not all the names from the 1st loop - just the 2nd loop and reset for each 1st loop.

 

Maybe append to variable isn't the right approach here?

 

I hope that makes sense!

 

 

mccardj_1-1660719365498.png

 

Categories:
I have the same question (0)
  • MarconettiMarco Profile Picture
    3,812 Super User 2024 Season 1 on at

    Hello @Anonymous ,

    you can use two string variable, one of them to use temporary.

    So, you can append all the values to the temp variable, copy it in the def variable and clear the temp variable values.

     

    In that way, you can grab only the names you want.

     

    If I have answered your question, please mark my post as Solved.
    If you like my response, please give it a Thumbs Up.

    BR,

    Marco

  • Community Power Platform Member Profile Picture
    on at

    Hi @MarconettiMarco,

     

    How do I go about clearing the temp variable values?

     

    I thought it was as simple as setting the variable to null at the end of the flow but that didn't work - so I tried appending the variable to null and that didn't work either.

     

    See the image below - the "Clear Participants" is setting the variable to null at the end of the flow but the variable is still appending across the 1st loop

     

    mccardj_0-1660724836361.png

     

  • Ashrestha Profile Picture
    12 on at

    This is how I did and it worked for me. 

    1. Initialize Your Variables: Start by initializing your variables. You will need two string variables: one for temporary storage and one for the final result.

      • Create a variable for temporary storage. Let's call it "TempVar."
      • Create a variable for the final result. Let's call it "FinalVar."
    2. Append Values to the Temporary Variable: Use the "Append to string variable" action to add values to the "TempVar" variable. This action can be used within a loop or any other relevant context where you are collecting values.

    3. Copy Temp Variable to Def Variable: To copy the values from "TempVar" to the "FinalVar," you can use the "Set variable" action. Set the value of "FinalVar" to the value of "TempVar" using an expression like variables('FinalVar') = variables('TempVar').

    4. Clear Temp Variable: To clear the values in the "TempVar," you can use the "Initialize variable" action with an empty string as the initial value. This will effectively clear the temporary variable. For example, set variables('TempVar') to an empty string ''.

    5. Repeat as Needed: Repeat steps 2 to 4 as many times as necessary within your workflow to accumulate and copy the values you want into "FinalVar."

       

      Initialized two variables

      Ashrestha_0-1697920457447.png

       Rest of the flow.


      Ashrestha_1-1697920708411.png
    6. Result:

    PowerAutomate1.png

    PowerAutomate1.png


    PowerAutomate2.png 

     

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 503 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 321 Moderator

#3
abm abm Profile Picture

abm abm 237 Most Valuable Professional

Last 30 days Overall leaderboard