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 / Sort A Gallery Based o...
Power Apps
Answered

Sort A Gallery Based on SharePoint List

(0) ShareShare
ReportReport
Posted on by 5

Hello,

 

I have a gallery that is tied to a SharePoint list. That list has several lookup columns that I'm pulling in. The gallery is set to load a filtered list of items based on a drop-down selection from a previous screen (ClientId), sorted by Category...

 

jrothame_0-1607363171114.png

 

 

Items = 

SortByColumns(
Filter(
AddColumns('Client Solutions', "CategoryText", 'Solution:Category'.Value),
'Client:ID'.Id = selectedId
),"CategoryText",
Ascending
)

 

However, I'd like to sort the gallery by several other similar columns. I've setup an icon, just to test, and set it to sort by the same column but in descending order. However, when I click the icon, nothing happens. I'm having a hard time figuring out where I went wrong. I set the icon's OnSelect to the exact same formula above except with Descending as the order.  I'm sure I'm missing something obvious here. I'm wondering if it has something to do with the fact that I'm having to use "AddColumns" to properly pull in the text value of the lookup column? Any help is greatly appreciated!

 

SortByColumns(
Filter(
AddColumns('Client Solutions', "CategoryText", 'Solution:Category'.Value),
'Client:ID'.Id = selectedId
),"CategoryText",
Descending
)

 

Categories:
I have the same question (0)
  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @jrothame 

    You mention that you are putting these formulas you showed in your OnSelect action....the formula should be on the Gallery items, not in the OnSelect action.

    This is most likely why you appear to have a warning error on the icon in your picture.

    I hope this is helpful for you.

     

  • jrothame Profile Picture
    5 on at

    In that case, how would I go about triggering a sort against the gallery when I click my sort icon? I assumed I needed to call "SortColumns" via the OnSelect of the icon itself.

  • notj Profile Picture
    782 on at

    @jrothame use variables. 

     

    In the OnSelect you can set a variable using Set(VariableName, "value"). So do something like Set(SortColumn, "Category") for your button you want to change the sort to a name. If you want it by Solution, set another button to change your variable to Set(SortColumn, "Solution")

     

    Then in your gallery your SortByColumn formula will reference that variable instead of a specific column name. 

     

    The variable value has to match the name of the column though. Be mindful of that.

  • Verified answer
    RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @jrothame 

    So typically, if you want to have sorting by different columns, then you need to "snapshot" the name of the column you want.  This is done with a variable. 

     

    So, for example, if you wanted to sort by Category and also by SubCategory, then on the Category label (I'd suggest, or some other indicative icon) you would have the following formula:

    For Category OnSelect:
    UpdateConext({lclSortColumn:"CategoryText"})
    
    and for the SubCatgegory OnSelect:
    UpdateConext({lclSortColumn:"SubCategory"})
    

    Now, for your Gallery, change the Items formula to the following:

    SortByColumns(
     Filter(
     AddColumns('Client Solutions', 
     "CategoryText", 'Solution:Category'.Value),
     'Client:ID'.Id = selectedId
     ),
     lclSortColumn,
     Ascending
    )

    Note, if you need to AddColumns in order to get the SubCategory in the same way, then do so, but change the UpdateContext on the SubCategory to match the added column name.

     

    If you want to implement a click for ascending and click for descending, then add the following to your OnSelects:

    UpdateContext({lclSortOrder: If(lclSortColumn="Category", !lclSortOrder, true)});
    UpdateContext({lclSortColumn:"Category"})

    Repeat for the SubCategory, but change the values for it.

     

    Then finally your formula for Items would be the following:

    SortByColumns(
     Filter(
     AddColumns('Client Solutions', 
     "CategoryText", 'Solution:Category'.Value),
     'Client:ID'.Id = selectedId
     ),
     lclSortColumn,
     If(lclSortOrder, Ascending, Descending)
    )

     

    That should give you what you want.  Please observe the pattern here and just implement that for any other columns you need to sort by.

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 765 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 272

Last 30 days Overall leaderboard