cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
HansHeintz
Responsive Resident
Responsive Resident

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

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
                    )
               )    
       )
)

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User
Super User

@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.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

4 REPLIES 4
RandyHayes
Super User
Super User

@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.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Thanks! That helps!!

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

RandyHayes
Super User
Super User

@HansHeintz 

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

Glad to help.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

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?

Helpful resources

Announcements
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Community Call Conversations

Introducing the Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (2,369)