Is there any way to order/sort a For All operation? In the use case, I need to apply a row number to each row in a collection, sorted alphabetically by one of the values.
The easiest way would be to use a Power Automate flow to do the ordering and then send back the resulting collection. Flow loops have an option to run concurrently in parallel, but that isn't the standard. So using a flow you can do a true loop.
You can simulate one. Here is an example from my notes that I responded to on a similar type of question. The approach would be the same. The objective was to renumber the column PriorityRank
Hopefully it helps.
Add a Sequence Column to a Sorted Table
Depending on your answer you could do something like create a sorted collection with the ID and priority rank (this column is only needed for testing) SortByColumns(table, PriorityRank) You would then need to add a new PriorityRank column which would need to be the incremented value AddColumn(SortByColumns(table, PriorityRank), newPriorityRank, 0) To do this you use something like ClearCollect( SortedIndexedPriority, ForAll( Sequence(CountRows(SortedPriority)), With( { Index: Value, Item: Last( FirstN(SortedPriority, Value) ) }, { ItemField1: Item.Name, NewPriorityRank: Index*10, ID: Item.ID, PriorityRank: Item.PriorityRank } ) ) ); You would then use this collection to update the original records
If you read the documentation you will find that the ForAll() isn't a true loop. It applies whatever formula you supply to all the records simultaneously. It will often look like its doing things in order if processing the formula takes the same amount of time for each item. But that is just an illusion. There is no order to the way the formula is applied and no way to force one.
"When writing your formula, keep in mind that records can be processed in any order and, when possible, in parallel. The first record of the table may be processed after the last record."
----------------------------------------------------------------------------------
If this Post helped you, please click "Does this answer your question" and give it a like to help others in the community find the answer too!
Paul Papanek Stork, MVP
Blog: https://www.dontpapanic.com/blog
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.