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 Apps / Adding an "index" numb...
Power Apps
Answered

Adding an "index" number column to a collection (fast)

(0) ShareShare
ReportReport
Posted on by 704

Hi,

 

I need to add an index column to a collection so my in my gallery an item can look into the previous item to hide data if it is the same.

I found this code somwhere here which works but on revieweing the speed of the process that builds the collection this step appears to be incredibly slow (3 seconds for 40 items, a bit less than half the total process that involves getting data from dataverse and sql tables). I don't know why it is is so slow but might someone now a quicker way?

;;ForAll(
 Collection1 
 ;Collect(Collection2;
 Last(FirstN(AddColumns(Collection1;
 "SortNr";
 CountRows(Collection2)+1
 ); 
 CountRows(Collection2)+1
 )
 ) 
 )
)

 

 

Categories:
I have the same question (0)
  • Verified answer
    RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @HansHeintz 

    Any time you use a ForAll table creating function to act like a ForLoop, you will experience performance issues.

    The formula should be the following:

     

    ForAll(Sequence(CountRows(Collection1));
     Patch(Last(FirstN(Collection1; Value)); {SortNr: Value})
    )

     

    This will return a table with all of your rows of collection1 and an added column with the row number.

     

    I hope this is helpful for you.

  • HansHeintz Profile Picture
    704 on at

    Thanks! That helps!!

    For anyone copy pasting in europe: the , after "FirstN(Collection1" should be an ; of course

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @HansHeintz 

    I tried to remember to replace commas with semi's...forgot one!  I have changed it in the posted formula.

    Glad to help.

  • MustafaHussain Profile Picture
    173 on at

    Hi @RandyHayes 
    I tried your code but its not working as expected
    App OnStart Code:

    ClearCollect(MeetingDetailsCol, MeetingDetails);
    
    ForAll(Sequence(CountRows(MeetingDetailsCol));
    Patch(Last(FirstN(MeetingDetailsCol, Value)), {SortNr: Value})
    )


    I cant see any Index Column in my collection any help?

  • Daved5 Profile Picture
    54 on at

    Hello

     

    I got this to work - here is how i used it - first I added the new column to a new collection - colAllUsersSort - (based on old collection colAllUsers)

     

    ClearCollect(colAllUsersSort,
    AddColumns(colAllUsers,
    "SortNr", 0
    )
    );

     

    Then I added the sequence number to the new column SortNr


    ClearCollect(colAllUsersSort,
    ForAll(Sequence(CountRows(colAllUsers)),
    Patch(Last(FirstN(colAllUsers, Value)),
    {
    SortNr: Value
    }
    )
    )
    )

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 Apps

#1
WarrenBelz Profile Picture

WarrenBelz 721 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 320 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard