Skip to main content

Notifications

Power Apps - Power Apps Pro Dev & ISV
Answered

For All Ordering

Posted on 27 Nov 2024 19:22:50 by 24
Hi,
 
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.
 
Kind Regards,
  • Verified answer
    Pstork1 Profile Picture
    Pstork1 63,813 on 27 Nov 2024 at 20:57:00
    For All Ordering
    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.
  • Suggested answer
    ronaldwalcott Profile Picture
    ronaldwalcott 800 on 27 Nov 2024 at 20:56:26
    For All Ordering
    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
     
  • Arrow3009 Profile Picture
    Arrow3009 24 on 27 Nov 2024 at 20:35:55
    For All Ordering
    Hi Paul,
     
    Thanks for the reply, that makes sense considering it isn't a true loop.
     
    Do you know of any workaround whereby we could effectively loop through a collection to achieve this?
     
     
  • Suggested answer
    Pstork1 Profile Picture
    Pstork1 63,813 on 27 Nov 2024 at 20:24:22
    For All Ordering
    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.

Helpful resources

Quick Links

November 2024 Newsletter…

November 2024 Community Newsletter…

Community Update Oct 28…

Power Platform Community Update…

Tuesday Tip #7 Community Profile Tips…

Welcome to a brand new series, Tuesday Tips…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 143,137

#2
RandyHayes Profile Picture

RandyHayes 76,308

#3
Pstork1 Profile Picture

Pstork1 63,813

Leaderboard