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 / HELP! Anyone know how ...
Power Apps
Unanswered

HELP! Anyone know how to filter a collection but also show the first record value that matches a filter?

(1) ShareShare
ReportReport
Posted on by 466

Greetings, all! Really hope someone can help with this as I've been going round in circles.

 

I need to create a collection that filters a collection to show specific records but also shows the longest text value for items with the same ID in the original collection. It's probably easier with an example.

 

Here's the collection I'm starting with named Data:

ItemflagFieldPositionLength
Category118
ABC013
DEF Corporation0115
Subcategory1211
HI022

 

Here's the new collection I want to create called TableFields:

FieldPositionMaxLengthText
Category1DEF Corporation
Subcategory2Subcategory

 

I currently get close to this by doing the following:

 

 ClearCollect(
 TableHeaders,
 Filter(
 Data,
 flagField = 1
));

 

 I thought I could get the record with the greatest length this way but without success:

 

 ClearCollect(
 TableFields,
 AddColumns(Filter(
 Data,
 flagField= 1
 ),"MaxLengthRecord",FirstN(Sort(Filter(Data, flagField>0 && Position=Position),"Length",Descending),1)
 ));

 

 Anyone have any ideas?

Categories:
  • AJ_Z Profile Picture
    3,711 Super User 2024 Season 1 on at

    put this in a button and add a gallery with the datasource TableFields.
    :

    Clear(TableFields);ClearCollect(TableHeaders,Filter(
    AddColumns(Data,"MaxLengthText",""),
    flagField = 1
    )); ForAll(TableHeaders As TfTest, Collect(TableFields,{MaxLengthText:First(Sort(Filter(Data,Position = TfTest.Position),Length,Descending)).Item,Field:TfTest.Item,Position:TfTest.Position}))

     

  • AJ_Z Profile Picture
    3,711 Super User 2024 Season 1 on at

    Let me know what happens when you do that

     

  • AJ_Z Profile Picture
    3,711 Super User 2024 Season 1 on at

    I was able to mimic your case and create a solution this way

    AJ_Z_0-1639505789412.png

     

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

    @arpost 

    Using your provided data, the following formula in a Gallery Items property would give you what you want:

    ForAll(
     GroupBy(Data, "Position", "_data") As _items,
     {Field: First(_items._data).Item,
     Postion: _items.Position,
     MaxLengthText: LookUp(_items._data, Length= Max(_items._data, Length), Item)
     }
    )

    If there is a specific reason you need an in-memory database Collection, then your formula would be:

    ClearCollect(TableFields,
     ForAll(
     GroupBy(Data, "Position", "_data") As _items,
     {Field: First(_items._data).Item,
     Postion: _items.Position,
     MaxLengthText: LookUp(_items._data, Length= Max(_items._data, Length), Item)
     }
     )
    )

     

    Keep in mind that ForAll is NOT a ForLoop development function, it is a function that returns a Table of records based on your parameters.  The above provides the three columns you specified in the data sample.

     

    I hope this is helpful for you.

  • arpost Profile Picture
    466 on at

    Thanks for the replies, @AJ_Z and @RandyHayes. I'm thinking I'll still need to capture this in a collection as I need to reference the collected data in other ways in my app. @RandyHayes, I'll give the second technique you shared a try when I have a chance.

  • AJ_Z Profile Picture
    3,711 Super User 2024 Season 1 on at

    More than happy to help did my solution help?

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

    @arpost 

    If you are using the formula to populate a Gallery...then you already have all the values for anywhere in your app.  You can reference the Gallery.AllItems property from wherever you want and it will have the work already done for you.

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 397 Super User 2026 Season 2

#2
Mohsin Ali Profile Picture

Mohsin Ali 354

#3
WarrenBelz Profile Picture

WarrenBelz 232 Most Valuable Professional

Last 30 days Overall leaderboard