web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Adding Unique Column t...
Power Apps
Answered

Adding Unique Column to collection

(0) ShareShare
ReportReport
Posted on by 202

Hi Reader,

I'm having a collection like below

ClearCollect(
 colStudents,
 Filter(
 StudentInformation,
 ID = vStudent.ID
 )
);

 

Now i want to add an unique id column to the collection. i tried using add columns as shown below

 

ClearCollect(
 colStudents,
 AddColumns(
 Filter(
 StudentInformation,
 ID = vStudent.ID
 ),
 "Index",
 Last(colStudents).Index + 1
 )
);

but it is not working as expected. and it is giving the same number for all the records in collection. 

Categories:
  • timl Profile Picture
    37,420 Super User 2026 Season 2 on at

    Hi @Sharuk 

    If you just want add a sequential number to each record in your collection, this should work for you.

    ClearCollect(colStudents,
     With({data: Filter(StudentInformation,ID = vStudent.ID)},
     ForAll(Sequence(CountRows(data)),
     Patch(Last(FirstN(data,Value),
     {Index:Value}
     )
     )
    )

     

     

  • Sharuk Profile Picture
    202 on at

    Hi @timl 

    I'm using the same collection in 2 places i.e. new mode & edit mode.

    in new mode. i've kept a button and on click of that i've been using the following formula

     

    Collect(
     colStudents,
     {
     Index: Last(colStudents).Index + 1,
     Student_Name: Blank(),
     Class:Blank(),
     Region:Blank()
     }
    )

     

    and passing this to gallery. there it is working perfect and i was able to get the index value

     

    but in the edit mode, i'm trying to add index column to available data as below

    ClearCollect(
     colStudents,
     AddColumns(
     Filter(
     StudentInformation,
     ID = vStudent.ID
     ),
     "Index",
     Last(colStudents).Index + 1
     )
    );

     but here i'm not able to get the different index value. and getting the common index value for all the records inside the collection

  • Sharuk Profile Picture
    202 on at

    Hi @timl 

     

    I've even tried your formula. but it is throwing some errors stating. forall & with has some invalid arguments

    ClearCollect(colStudents,
    With({data:Filter(studentsInformation,ID = vStudent.ID)},
    ForAll(Sequence(CountRows(data)),
    Patch(Last(FirstN(data,Value),{Index:Value})))));
  • Krishna_R Profile Picture
    525 on at

    @Sharuk 

    i believe you're trying to add index column. you can refer this thread by @RandyHayes

  • Verified answer
    timl Profile Picture
    37,420 Super User 2026 Season 2 on at

    Hi @Sharuk 

    I think some of your brackets might be in the wrong place. You could try this to see if it works better.

    ClearCollect(colStudents,
    With({data:Filter(studentsInformation,ID = vStudent.ID)},
    ForAll(Sequence(CountRows(data)),
     Patch(Last(FirstN(data,Value)),
     {Index:Value}
     )
    )
    );

     

     

  • Sharuk Profile Picture
    202 on at

    Hi @timl ,

    thanks it worked as expected. and out of curiosity i just wanted to know if this is the similar logic which was mentioned by @RandyHayes  in this thread

     

    https://powerusers.microsoft.com/t5/Building-Power-Apps/Adding-an-index-column-to-a-collection/m-p/394941#M115386

  • timl Profile Picture
    37,420 Super User 2026 Season 2 on at

    Hi @Sharuk 

    Yes, it's broadly very similar. I've posted Randy's code for reference. The difference here is that there's an 'index' collection which you don't have, so we use the Sequence function to generate the index, which also negates the requirement to call CountRows.

     

    Clear(Col_Table1_Indexed);
    
    ForAll(Col_Table1,
     Collect(Col_Table1_Indexed,
     Patch(Last(FirstN(Col_Table1, CountRows(Col_Table1_Indexed)+1)), 
     {Index: CountRows(Col_Table1_Indexed) + 1})
     )
     )

     

     

  • Sharuk Profile Picture
    202 on at

    @timl thanks much. got it and we're using last(firstn()) to get the last number of sequence and patching it to index ?

    ClearCollect(colStudents,
    With({data:Filter(studentsInformation,ID = vStudent.ID)},
    ForAll(Sequence(CountRows(data)),
     Patch(Last(FirstN(data,Value)),
     {Index: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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 248 Super User 2026 Season 2

#2
WarrenBelz Profile Picture

WarrenBelz 192 Most Valuable Professional

#3
Mohsin Ali Profile Picture

Mohsin Ali 135

Last 30 days Overall leaderboard