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 first row for each...
Power Apps
Unanswered

Get first row for each group

(0) ShareShare
ReportReport
Posted on by 22

I have a table for which I am trying to get the most recent record only for each Id according to the created column

 

ClearCollect(TestList, 
{Id: 1, created: 1, desc: "Lorem"}, 
{Id: 2, created: 3, desc: "Ipsum"}, 
{Id: 2, created: 2, desc: "Dolar"},
{Id: 3, created: 4, desc: "Sit"},
{Id: 1, created: 4, desc: "Amet"})

 

So I only want to keep the rows in green:
pleasehelpme2_0-1703767144935.png

 

 

I thought I could group the table, sort each resulting table by the created column, keep the first record and then ungroup again.

 

ClearCollect(filteredList,
 Ungroup(
 ForAll(
 GroupBy(TestList, "Id", "Result"),
 {Id: ThisRecord.Id, Result: First(Sort(ThisRecord.Result, created, SortOrder.Descending))}),
 "Result")
)

 

However, the Ungroup function doesn't seem to be ungrouping the table in the results column and I am left with a grouped table:

pleasehelpme2_1-1703767459801.png

Any help much appreciated!

Categories:
I have the same question (0)
  • SebS Profile Picture
    4,616 Moderator on at

    @pleasehelpme2 

     

    here :

     

    ClearCollect(TestList, 
    {Id: 1, created: 1, desc: "Lorem"}, 
    {Id: 2, created: 3, desc: "Ipsum"}, 
    {Id: 2, created: 2, desc: "Dolar"},
    {Id: 3, created: 4, desc: "Sit"},
    {Id: 1, created: 4, desc: "Amet"});
    
    ClearCollect(filteredList,
     Ungroup(
     ForAll(
     GroupBy(TestList, "Id", "Result"),
     {Id: ThisRecord.Id, Result: First(Sort(ThisRecord.Result, created, SortOrder.Ascending))}),
     "Result")
    )

     

    in gallery :

     

    SebS_0-1703769587667.png

     

    and labels You refer like this  :

     

    SebS_1-1703769630117.png

     

    Hope that helps

  • timl Profile Picture
    36,411 Super User 2025 Season 2 on at

    @pleasehelpme2 

    Alternatively, you could also extract the required details by calling AddColumns and referencing the first Result row.

    ClearCollect(filteredList,
     AddColumns(GroupBy(TestList,"Id","Result"),
     "created",First(Sort(Result,created)).created,
     "desc",First(Sort(Result,created)).desc
     )
     )

     

    timl_0-1703770888199.png

     

  • pleasehelpme2 Profile Picture
    22 on at

    Thanks for your answers. I actually want to show this in a table, and TestList is a sharepoint list. How can I do this?

  • SebS Profile Picture
    4,616 Moderator on at

    @pleasehelpme2 

     

    replace name of test list with name of your SharePoint list and change names of the columns to match SharePoint List columns and you will be ok

  • pleasehelpme2 Profile Picture
    22 on at

    The issue now is that if I set my table items to 

     

    Ungroup(
     ForAll(
     GroupBy(TestList, "Id", "Result"),
     {Id: ThisRecord.Id, Result: First(Sort(ThisRecord.Result, created, SortOrder.Ascending))}),
     "Result")

     

     it only allows me to add two fields: Id and Result, when really I need to be able to display Result.x Result.y, Result.z etc. 

     

    So I suppose I go back to my first question, which is, why doesn't the ungroup work?

  • WiZey Profile Picture
    3,023 Moderator on at

    Hello @pleasehelpme2 ,

     

    "Ungroup()" doesn't work because it's expecting a table, but "Result" here is a record.

     

    I've written another formula, can you try and see how it goes:

     

    ForAll(
     GroupBy(
     testList;
     "ID";
     "Grouped"
     );
     AddColumns(
     First(
     Sort(
     ThisRecord.Grouped; 
     Created; 
     SortOrder.Descending
     )
     );
     "ID";
     ID
     )
    )

     

  • timl Profile Picture
    36,411 Super User 2025 Season 2 on at

    @pleasehelpme2 

    With regards to the SharePoint issue and using a data table control, you can just set the items property of the data table control to the formula that I posted. If you use the AddColumns/First syntax, you can retrieve your Result.x Result.y, Result.z columns.

    timl_0-1703848016364.png

     

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

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 343 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard