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 / Alternative to Variabl...
Power Automate
Unanswered

Alternative to Variables in a ForEach loop

(0) ShareShare
ReportReport
Posted on by 117
Okay, before you answer with a 'Compose' let me explain the situation. My frustration is high at the end of the week so I hope the community can lend some support over the weekend so I may come back on Monday with some hope. 
 
Problem: 
With Power Automate and 'Variables' they are basically considered 'Global variables' on the flow. Because of this you are required to 'initialize' them outside any Scope or loops. In most cases variables are utilized to hold onto data and reference it inside loops, conditions, or the like. Think of a situation where the variable could be true or false depending on a condition, then later in the flow/loop you need to output the true or false. You can't do this with compose or individual actions because you would need to know which action to reference, the true action or false action. 
My particular issue is that I have a complex flow. It has multiple variables and variable types. I have a string variable with HTML that is appended to, string variables that are updated or changed based on conditions, and null values that get filled in during the process. I also have a foreach loop that processes the data concurrently, because of this the variables are overwritten at different points in the process manipulating the data. 
 
Solution???
I know that 'Compose' is what everyone says to use, but that would only work if literal references were possible. It won't work if i use conditions, it won't really work if i need to append string to them. All of it comes down to compose needs a direct reference and the whole point of using a variable is to avoid that. 
 
I am drawing so many blanks and dead ends. Any help would be greatly appreciated. 
Categories:
I have the same question (0)
  • Suggested answer
    tom_riha Profile Picture
    10,185 Most Valuable Professional on at
    Hello,
     
    I dealt with a similar situation in the past, and the solution was to store objects in the variables where I could always identify later which value from the variable is relevant for the current instance of the loop. I explained the solution in this article.
  • Verified answer
    Mark Nanneman Profile Picture
    991 Super User 2025 Season 2 on at
    I've built complicated flows like this in the past that generated very complex html strings for email reports--html that had links to records, even embedded thumbnail images relevant to each record, special highlighting of values in certain ranges, flag icons, etc., etc., in the report. 

    Once or twice I've had the time to think about a way to refactor them to run faster.  I found that in some cases I could get rid of the for each loop all together.  

    The way I did this was using a "Select" operation, and putting all of my conditions into the expressions in the Select action, then doing a Join on the Select output array. 

    Often I would create string "templates" and save them in compose actions above my select.  I would add my own variables/placeholders to these templates (e.g. "<a href="{link}">Open {recordName}</a>").  Then in my Select expression I would do Replace() functions to find my "placeholders" in my "templates" and replace them with an "item()?['property']" expression.

    Whatever logic you can accomplish in a "Condition" action, you can also accomplish in an expression in power automate.  The downside is it's painful to write it all out in the Power Automate Flow Designer, so I'd just write the code in a Notepad or VS Code even, then copy paste.

    It hurt my brain to plan it out and test it, but when it was finished it ran insanely fast. 

    I don't know if it'll help in your case, but just giving an outside the box idea!

    Power Platform Developer | LinkedIn: Mark Nanneman | Blog: Power Stuff  | YouTube: Mark's Power Stuff  |
  • Verified answer
    AsaurdiffADTill Profile Picture
    117 on at
    I wanted to mention that i went with 'Mark's' option in a way. 
     
    Because I had a LOT of 'append' to's and re-referencing variables I was able to use 'Child' flows in this process. I used the Parent to grab the reference table data/lists and i sent that into a Child Flow using a Base64 encoded string. Then in each child flow i processed the data using variables singularly. Then i pass a json formatted object of key/value pairs with the value, also being a base64 encoded string. Then in the parent i decode the values and apply them to compose actions as the 'variables' and distribute inside the loop. 
     
    Pros: 
    This process does fix my issue and minimizes the runs on the parent flow, So i can send batches to the parent flow. Since this is technically the most complex part of the process it save exceptional amounts of runtime when concurrency is enabled. 
     
    Cons: 
    This will see the child-flows run PER item in a batch PER child-Flow ( I have 5 child-flows running parallel in the parent-flow) BUT considering the process takes less then 5 seconds PER run, I am less worried. NOTE: these child-flows have minimal if no actions that do calls. Its mostly formatting the data from the parent-flow into a formatted HTML string. 
     
    Overall this process still saved exponential amounts of processing time :) Thanks Mark for getting me into the right mind set for this. 

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