cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
uselesstool
Frequent Visitor

Reduce Table to Latest Record for Each User

I am working on a PowerApp that pulls data from an Azure SQL instance.  This table has multiple records per person and I would like to create a collection of just the latest record per user.  Example:

 

CurrentTable

NameApplicationStateDateRecordCreated
ShaneTerminated2022-05-22
MarryPending2022-07-15
BobActive2022-06-11
SonyaActive2022-05-22
ShaneActive2022-07-05
BobPending2022-05-22
MarryTerminated2022-05-22

 

I would like to reduce CurrentTable down to the latest records per user, NewTable:

 

NewTable

NameApplicationStateDateRecordCreated
ShaneActive2022-07-05
BobActive2022-06-11
MarryPending2022-07-15
SonyaActive2022-05-22

 

I have tried nested filtering and using GroupBy within a filter.  I feel like I've gotten close but can't quite get what I'm looking for.  Thank you in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User
Super User

@uselesstool 

What the formula is doing is to first sort the table by the created date.  Then it groups those results by the "Name" column.  All associated records will then be in the "_items" column.  Since the list was sorted in descending date order, the first record of each individual group will be the most recent.

So, the ForAll then is returning the table of records that would be the group name, and then the first record values.

 

If you have a lot of columns to contend with, then you can change to the following Formula so that you don't need to specify each column by hand:

ForAll(
    GroupBy(
        Sort(CurrentTable, DateRecordCreated, Descending),
        "Name",
        "_items"
    ),
    Patch({Name: Name}, First(_items))
)
_____________________________________________________________________________________
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

3 REPLIES 3
RandyHayes
Super User
Super User

@uselesstool 

Please consider the following Formula:

ForAll(
    GroupBy(
        Sort(CurrentTable, DateRecordCreated, Descending),
        "Name",
        "_items"
    ),
    {Name: Name,
     ApplicationState: First(_items).ApplicationState,
     DateRecordCreated: First(_items).DateRecordCreated
    }
)

This would return a table like you are looking for.

 

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!

That looks interesting, I will try to apply that now.  The real table I'm working with has many more columns.  Would you mind explaining to me what the code is doing?

 

Thanks

RandyHayes
Super User
Super User

@uselesstool 

What the formula is doing is to first sort the table by the created date.  Then it groups those results by the "Name" column.  All associated records will then be in the "_items" column.  Since the list was sorted in descending date order, the first record of each individual group will be the most recent.

So, the ForAll then is returning the table of records that would be the group name, and then the first record values.

 

If you have a lot of columns to contend with, then you can change to the following Formula so that you don't need to specify each column by hand:

ForAll(
    GroupBy(
        Sort(CurrentTable, DateRecordCreated, Descending),
        "Name",
        "_items"
    ),
    Patch({Name: Name}, First(_items))
)
_____________________________________________________________________________________
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!

Helpful resources

Announcements
Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Difinity Conference 2022

Difinity Conference 2022

Register today for two amazing days of learning, featuring intensive learning sessions across multiple tracks, led by engaging and dynamic experts.

European SharePoint Conference

European SharePoint Conference

The European SharePoint Conference returns live and in-person November 28-December 1 with 4 Microsoft Keynotes, 9 Tutorials, and 120 Sessions.

Power Apps Ideas

Changes to Ideas Coming

We are excited to announce a new way to share your ideas for Power Apps!

Top Solution Authors
Top Kudoed Authors
Users online (3,811)