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 Gallery based on ...
Power Apps
Answered

Sort Gallery based on a value

(0) ShareShare
ReportReport
Posted on by 252

Hey guys,

 

Same thing here. Founds tons of post about the subject but none if it helped my case and I believe mine is one of the simplest, please forgive me to ask you.

 

I've got a SharePoint list and one of the column automatically changes its values, either "Ok" or "Low" based on a quick calculation which is simply: If value in column "Stock" is less than value in column "threshold", the value in column "Status" changes to Low and vice versa).

 

This is just to see what items in my stock is low and which one is not.

 

What I'm trying to achieve here is simply to have a button that would display the "Low" first and the "Ok" last. A simple sorting.

I'd like to do that by just dividing the number in stock by the threshold, and the lowest value returned would be in top of the results.

 

I could also just put the "Low" on top but then within the low, there would be no sorting within (like the lowest of the "low" would not be at the top for example.

 

There is the default button of the app when you build it you know, with the below code:

 

UpdateContext({SortDescending1: !SortDescending1})

 

I was thinking of reusing it. Doesnt matter at the end...

 

Appreciate your help!

 

Thanks

Anthony

Categories:
I have the same question (0)
  • mdevaney Profile Picture
    29,991 Moderator on at

    @AnthonyRegnier 
    Put this code in the Items property of your gallery.

    Sort(
     AddColumns(
     your_datasource_name,
     "SortRank", (your_stockquantity_column / your_stockthreshold_column)
     ),
     SortRank,
     If(SortDescending1, Descending, Ascending)
    )

     

    If you want the gallery to default to show the lowest level stock first put this code in the OnVisible property of your screen

    UpdateContext({SortDescending1: true})

     

    ---
    Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

  • AnthonyRegnier Profile Picture
    252 on at

    Hi!

     

    Thank you, it does work. However the search is not working anymore when I change the code in the Gallery items.

     

    It's currently the following:

    SortByColumns(Filter([@'Stock Management'],StartsWith(Title,TextSearchBox1.Text)),"Title",If(SortDescending1,Descending,Ascending))

     

  • mdevaney Profile Picture
    29,991 Moderator on at
    Sort(
     AddColumns(
     Filter(
     [@'Stock Management'],
     StartsWith(Title,TextSearchBox1.Text)
     ),
     "SortRank", (your_stockquantity_column / your_stockthreshold_column)
     ),
     SortRank,
     If(SortDescending1, Descending, Ascending)
    )


    ---
    Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

    @AnthonyRegnier
    Please try this idea...

  • AnthonyRegnier Profile Picture
    252 on at

    Hey man!

     

    You're a real champion I really must analyze your formulas and try to understand them.

    It works perfectly!

     

    However when I open one of the product there is an error showing with the DetailForm1:

     

    BrowseGallery1.Selected

     

     

    If we fix that, we'll then just need to be able to filter the results using the drop down...

     

    Thanks! Amazing job!

    Anthony

  • mdevaney Profile Picture
    29,991 Moderator on at

    @AnthonyRegnier 

    What error is displaying for the BrowseGallery1?  Can you please take a screenshot of the error message?

  • AnthonyRegnier Profile Picture
    252 on at

    When I go to the other screens (Product details and NewItem), I've got an error on the formula: BrowseGallery1.Selected

     

    It tells me "Invalid fomula - expected a value compatible with datasource.

     

     

    ProductDetails.png
    NewItemScreen.png
  • Verified answer
    mdevaney Profile Picture
    29,991 Moderator on at

    I know what's going on here!  The gallery contents and the datasource do not match anymore due to the changes we have been making for the purposes of display.  Use this code in your form's Item property to find the record instead.

     

    LookUp([@'Stock Management'], ID = BrowseGallery1.Selected.ID)

     

    ---
    Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up." 

    @AnthonyRegnier 

  • AnthonyRegnier Profile Picture
    252 on at

    You are a real champ!

  • mdevaney Profile Picture
    29,991 Moderator on at

    @AnthonyRegnier 

    Thanks dude.  Best of luck in building your app!

  • AnthonyRegnier Profile Picture
    252 on at

    Thanks to you, really.

     

    I have already bunch of other questions to post... Next one will be interesting for you I'm sure... See you there!

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 551

#2
WarrenBelz Profile Picture

WarrenBelz 430 Most Valuable Professional

#3
Valantis Profile Picture

Valantis 298

Last 30 days Overall leaderboard