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 / Get position/index of ...
Power Apps
Unanswered

Get position/index of item in a collection

(0) ShareShare
ReportReport
Posted on by 70

Hi Community,

 

I simplify my scenario a little bit, but imagine I have a collection:

 

ColNames = [Bob, Jack, John, James]

 

and a label that displays one of these 4 different names.

 

What formula can I use to get the position of the name displayed in the collection?

 

For example, I want:

 

when the label reads "Jack", position_index = 2

when the label reads "John", position_index = 3

when the label reads "James", position_index = 4

 

etc. (my collection is much larger in reality and everything is dynamic so hard-coding is not an option)

 

 

Categories:
I have the same question (0)
  • Sundeep_Malik Profile Picture
    6,484 on at

    Hey @AntDim 

     

    Found a previous post which might help:

     

    https://powerusers.microsoft.com/t5/Building-Power-Apps/Find-Element-in-Array-and-get-Position-ID/td-p/1491919

  • AntDim Profile Picture
    70 on at

    I tried this formula but I don't think it's working 😞

  • AntDim Profile Picture
    70 on at

    I've got a Gallery where the title field is text from a collection text column. I want to set this up so that when I click on this title field, I get the position of this text in the collection

  • plinkplink Profile Picture
    2 on at
    ClearCollect(ColNames, {Value: "Bob"}, {Value: "Jack"}, {Value: "John"}, {Value: "James"});
    Clear(ColNamesIndexed);
    ForAll(
     ColNames,
     Collect(ColNamesIndexed, {Value: Value, Index: CountRows(ColNamesIndexed) + 1})
    );

     

    Output: ColNamesIndexed

    IndexValue
    1Bob
    2Jack
    3John
    4James

     

    Here's how it works:

     

    ClearCollect(ColNames, {Value: "Bob"}, {Value: "Jack"}, {Value: "John"}, {Value: "James"});

    This just recreates the collection from your example.

     

    Clear(ColNamesIndexed);

    It's a good idea to clear the collection at the start so you don't keep appending to it if this formula gets called again.

     

    ForAll( // Loop
     ColNames, // The original collection
     Collect(
     ColNamesIndexed, // The new collection with indexes
     {
     Value: Value, // This is the data from the original collection 
     Index: CountRows(ColNamesIndexed) + 1 // Current number of rows in the collection + 1
     }
     )
    );

     

    The part that does the trick is 

    CountRows(ColNamesIndexed)

     This is counting itself on each iteration and returning the current number of rows at each item. 

  • siwfy Profile Picture
    Microsoft Employee on at

    This is quite an innovative way of solving the problem. It may cause some performance issue at some point if the dataset is too large, but I think this solution is the best and simplest so far.

  • AntDim Profile Picture
    70 on at

    thanks @plinkplink  for your response. Where and how do you initialise the ColNamesIndexed collection? Using this formula in the Gallery Items property gave me an error in that the ColNamesIndexed collection is not recognized.

  • siwfy Profile Picture
    Microsoft Employee on at

    @AntDim The ColNamesIndexed collection is initialized with Clear(colNamesIndexed). And the ForAll() interates through your orginial collection, and then use Collect() to add an extra collumn of the index number and store all the records in this new collection colNamesIndexed row by row. I am guessing you are using the functions directly in the "Items" property of your gallery, which is why you are getting the error. If that's the case, you need to put the function in a behavior control (like a button), click on the button to generate this "colNamesIndexed" collection first, and then you can use it in your gallery.

     

    By the way, I am a little curious about why you need to add this index to the collection. If you are extracting items from specific positions in a collection, you can use Index() function. 

  • BenDonahue Profile Picture
    932 on at
    @AntDim, did you see @plinkplink's response? It is the best/fastest indexing code I have seen so far; if I had asked this question, I would have awarded them the Solution based on the effectiveness of the code they provided.
     
    For my part, TY plinkplink!!! 'the horns'

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 757 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 322 Super User 2025 Season 2

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 209 Super User 2025 Season 2

Last 30 days Overall leaderboard