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 / Sort Table 1 by newest...
Power Apps
Answered

Sort Table 1 by newest available record in Table 2

(0) ShareShare
ReportReport
Posted on by 116

Hi all,

 

I am attempting to sort Gallery items by a value found in a related table.

My primary goal is to sort the stocks in the gallery, with the least recently inspected stock appearing at the top, and the most recently inspected at the bottom. This will help prioritise workflow as you can imagine.

There can be many inspections to a stock.

 

So,

I would like to sort CashedStocks by the newest available record in CashedInspections

(I am aware that it should have been cached btw, but these things happen!)

Both are Collections

 

PK in CashedStocks is StockUniqueID

FK in CashedInspections is StockID 

 

I have attempted to use the "AddColumns" function, and this works to a certain extent. It is sorting the Stocks by date of Inspections, but not by most the recent inspection.

 

I am not sure how to add that layer of sorting to the formula

 

Any help greatly appreciated

 

 

SortByColumns(
 AddColumns(
 CashedStocks,
 "Date",
 LookUp(
 CashedInspections,
 Stock = StockUniqueID,
 Created
 )
 ),
 "Date",
 Ascending
)

Many Thanks

Categories:
I have the same question (0)
  • Verified answer
    v-bofeng-msft Profile Picture
    Microsoft Employee on at

    Hi @maxtarneberg :

    Do you want to sort CashedStocks by the newest available record in CashedInspections?

    Could you tell me:

    Is the value of the field "Created" the creation time recorded in CashedInspections?

    Is the Stock in the formula the StockID you mentioned earlier?

    Whether the two tables are mapped through the two fields "Stock" and "StockUniqueID"?

    I assume the above assumption holds.

    The key is to use the characteristics of the lookup function.

    The LookUp function finds the first record in a table that satisfies a formula. 

    I suggest you try this code.

     

    SortByColumns(
     AddColumns(
     CashedStocks,
     "Date",
     LookUp(
     Sort(CashedInspections,Created,Descending),
     Stock = StockUniqueID
     ).Created /* Sort records in CashedInspections by date */
     ), /* The LookUp function finds the first record in a table that satisfies a formula. Since we have placed the newly created record to the front, the returned record is the most recently created*/ 
     "Date",
     Descending
    )

     

    I think this link will help you a lot:

    Filter, Search, and LookUp functions in Power Apps

    Best Regards,

    Bof

  • maxtarneberg Profile Picture
    116 on at

    Hi @v-bofeng-msft ,

     

    Thank you, your assumptions were correct, and all of the answers were 'Yes!'

     

    Your code works perfectly, thank you.

     

    Just to further improve on it and make it perfect for my needs, could the Stocks be sorted so that those with no inspection against them appear first, followed by those with the least recent inspection (which we have achieved), followed by alphabetically by "GROWER", and finally filtered/search by entries into TextSearchBox1 under columns "GROWER", "Variety", "CertificateNo".

    I feel that it would need more add columns, but am unsure of how to write that syntax correctly.

     

    I'm sure this is a useful exercise for any job based app that needs to prioritise jobs against customers etc.

     

    Many Thanks for your ongoing support,

    Max 

  • maxtarneberg Profile Picture
    116 on at

     

    I have solved my own question with some tough perseverance.

     

    When added to the Gallery Items property, this will sort records in the gallery by least recently created record in a related table, but gallery records with no related records in the other table appear first. It will also filter by 3 different columns in a search box on top of all this.

     

    This is handy in prioritising work as stocks/customers/sites that have least recently had a job logged against it will appear at the top.

     

    I share the solution below in case its useful to anyone. First using my source names:

     

    SortByColumns(
     AddColumns(
     Search(
     CashedStocks,
     TextSearchBox1.Text,
     "VARIETY",
     "GROWER",
     "CertificateNo"
     ),
     "Date",
     LookUp(
     CashedInspections,
     Stock = StockUniqueID,
     Last(
     Sort(
     CashedInspections,
     Stock = StockUniqueID,
     Ascending
     )
     ).Created
     ),
     "NotInspected",
     Coalesce(StockUniqueID in CashedInspections. Stock)
     ),
     "NotInspected",
     Ascending,
     "Date",
     Ascending
    )

     

    And now in more generic and adaptable code:

    SortByColumns(
     AddColumns(
     Search(
     Table1,
     TextSearchBox1.Text,
     "Column1",
     "Column2",
     "Column3"
     ),
     "Date",
     LookUp(
     Table2,
     T2ID = T1ID,
     Last(
     Sort(
     Table2,
     T2ID = T1ID,
     Ascending
     )
     ).Created
     ),
     "NoRelatedRecordYet",
     Coalesce(T1ID in Table2.T2ID)
     ),
     "NoRelatedRecordYet",
     Ascending,
     "Date",
     Ascending
    )

     

    I hope this is useful to somebody.

     

    Thank you for your help @v-bofeng-msft 

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

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 316 Most Valuable Professional

#2
11manish Profile Picture

11manish 242

#3
Valantis Profile Picture

Valantis 198

Last 30 days Overall leaderboard