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 / I need some education ...
Power Apps
Answered

I need some education here regarding galleries and their engagement with the page upon which they sit

(0) ShareShare
ReportReport
Posted on by 235

If you ask on the internet how to sort a gallery or why my sort doesn't work, you'll get a lot of answers like this one by @mdevaney (see attached photo) which is a totally helpful answer to the question.  BUT my question is different.  It's more fundamental.  Instead of asking why a sort doesn't work, I'm asking why/how a sort works at all.  specifically, what about the code that mdevaney supplied causes the gallery's items property to be "re-executed" when you change the SortOrder value.   It's just an icon on the page that the gallery is on, with a little toggle code behind it to flip the value of variable holding the SortOrder.  So what about changing that global variable's value causes the gallery's items code to be "re-executed" (I know that's not the right word).

 

Is it just mentioning a global variable that happens to be used inside the Items property?  or is it how it's used there?   What does powerapps do with that, if you can tell me.     @mdevaney ...  Anyone?    Anyone? Bueller?    Many thanks to whomever can explain this to me.

mdevaney_post.PNG
Categories:
I have the same question (0)
  • Verified answer
    SebS Profile Picture
    4,748 Super User 2026 Season 1 on at

    @AmyF 

     

     

    To understand this first, we need to look into the SortByColumn():

     

    Syntax :

     

    SortByColumns( Table, ColumnName1 [, SortOrder1, ColumnName2, SortOrder2, ... ] )

     

    The basic use of this following mdevaney example will be

     

    SortByColumns( Engeener cost List, IncidentCity, SortOrder.Ascending)

     

    This will pull records from this List / Table and Sort them A-Z if we pick Descending, we will get results Z-A

    or You can call it from Small to large or large to Small.

     

    To create more dynamic behaviour of this syntax in the example rather than placing Fixed SortOrder.Ascending or SortOrder.Descending there is an IF statement.

     

    IF Statment works based on an assigned rule, in this case, AscendingOrder variable needs to be true to sort Gallery to A-Z by default.

     

    IF(ascendingOrder = true, SortOrder.Ascending,SortOrder.Descending)

     

    in human language "If ascendingOrder variable passing true, please sort all records in Ascending (A-Z) order if ascendingOrder is false, please sort them in Descending (Z-A) order"

     

    To use ascendingOrder and pass true/false to this IF statement, we need to create this variable first somewhere, and in this example, it is done on Screen Property called OnVisible and it is pre-set to true, which always means if the user gets to that screen, his default setting will be Ascending Sort till the user interact with an Icon and change true to false what will alter the behaviour of the IF statement in the result.

     

    to create variable You need to use Set() or UpdateContext() in this case Set() was used

     

    Set(ascendingOrder,true), which creates a variable called ascendingOrder which equals true or ascendingOrder = true

     

    The code in the Icon changes ascendingOrder =true to ascendingorder = false with a click, and that changes the way the Sort work in Gallery.

     

    I really hope I explain it in a way You understand it.

     

     

     

     

     

     

     

     

  • timl Profile Picture
    36,740 Super User 2026 Season 1 on at

    Hi @AmyF 

    Let's say that you set the items property of the gallery to the following formula (in this case, ascendingOrder is a global variable)

     

    SortByColumns(DataSource,
     "SortColumn",
     If(asendingOrder, Ascending, Descending)
    )

     

    This expression is not immutable. Because it references content can change, Power Apps sets up a dependency between the items property of the gallery and the global variable. This means that whenever the global variable changes, the items property will be re-executed (which correlates to the behaviour that you're seeing).

     

    The logical extension of this is that these dependencies can have a performance impact on an app. For further background reading, topics around 'inefficient delay loading' and 'cross screen dependencies' may help you understand this behaviour better.

    https://powerapps.microsoft.com/en-us/blog/app-checker-now-includes-performance-optimization-tips/

     


    Finally, as an analogy, let's imagine in Microsoft Excel that you set the cell formula of the A1 cell to this:

     

    "A2 Text:" + A2

     

    Whenever the A2 cell changes, Excel will recalculate the value of A1. Properties in Power Apps behave in exactly the same in way in terms of 'recalculation'.

  • Verified answer
    Chris-D Profile Picture
    1,246 on at

    Hi Amy, 

     

    The short and simple answer here is that if any variable changes within your app, Power Apps will automatically re-evaluate any formula or expression that relies on that variable. 

     

    In the post you mention, when the sort order variable changes, Power Apps responds by automatically re-evaluating the Items property of the gallery. 

  • AmyF Profile Picture
    235 on at

     So any change in any variable used in the code in the Items property code will cause the Items property to "fire" and the entire sort to be re-performed?  

     

    So after you, say, Patch a change to a datasource, and want to see those changes in your gallery, you have to somehow re-perform the code in the Items property in order to get the refreshed data showing in the gallery and I don't know how to cause that to re-evaluate or re-load itself.   In order for my changes to show in the gallery I have to manually click OnStart, then the fresh data is loaded but I can't get it to refresh and reload prior to returning to the page.  I even Navigate away from the page because I thought if I Navigated back vs doing a generic Back() might make a difference,  That didn't work, I cannot reload the gallery with the new data, which was my original problem I'm trying to solve.  Forgive me for not asking this question directly, I have but the answers I get don't seem to help.   So I was hoping to see a "trick" to get it to reload.  People have told me to re-apply the GroupBy code which is in the Items property.  I tried moving it to a button and then doing a Select on that button but I couldn't get that working either.    I know I'm not doing something right and that's the digging I'm doing because I'm a bit lost on this part and was hoping to find a "hack".     That said, any suggestions?

     

  • Verified answer
    Chris-D Profile Picture
    1,246 on at

    Any change in any variable in any property will cause that code to be re-evaluated. Like in Excel if you change a value, cells with formulas will automatically update. 

    The same should be true with data sources. If you're using a data source in the Items for the Gallery, it should refresh automatically if the data source is modified. 

     

    Can you post your code for your Items property please and the code you're using to change the data? 

  • SebS Profile Picture
    4,748 Super User 2026 Season 1 on at

    @AmyF 

     

    The data refresh depends on how you connect to the data source.

    If you directly connect, the Gallery's item property references the data source connector. When you add a record to the data source, like a SharePoint List, the Gallery updates automatically. However, this can slow down performance with large datasets, as it constantly loads all data even when you don't need it. You can limit the number of loaded records using Filter().

    The more common approach is to use Collections. Data is loaded into a collection at the app's start, and you use this collection in Galleries. This reduces waiting time for users because the data is loaded when the app starts. One common mistake new users make is forgetting to refresh this collection after UpdateForm or Patch, making it seem like updates to the data source don't affect the Gallery.

     

     

     

     

  • Verified answer
    AmyF Profile Picture
    235 on at

    Thank you SebS.  I did get to the solution but it was my use (over use?) of collections that messed me up... well one of the things.   Was told to refresh my datasource, I did, no diff, then I was told to refresh the collection, I did to no avail  THEN realized I'd forgotten to refresh the middle-man, the first collection.  I did a CC from Datasource into Collection1, then CC'd Collection 1 into Collection2.   So Refresh of data source didn't work, then since I used Collection2 in the Items, I refreshed that and was really disappointed that that didn't work... then I refreshed the first collection too and it works as expected.      Thanks I really appreciate your time in explaining it to me.

  • Verified answer
    AmyF Profile Picture
    235 on at

    Now that is interesting.  Thank you very much.  I wish I could give you the solution credit but I gave it to SebS since his answer dealt with the question that prompted your spectacular answer which is really great.  I wish we could choose two solutions because both informed me greatly.   Thanks.

  • SebS Profile Picture
    4,748 Super User 2026 Season 1 on at

    @AmyF 

     

    As you mentioned that both answer was satisfactory and you wish to provide credit to both of us I accepted both solutions for this topic to fulfil your wish and also give well-deserved credit to @Chris-D  🙂

     

     

  • Chris-D Profile Picture
    1,246 on at

    @AmyF no problem, glad I could help. I have made this mistake myself. There's a balance between caching data to speed up the app and ease of coding that can sometimes be hard to find. 

     

    If you're not using the 'middle men' anywhere else, you can probably cut down the process to make it more manageable. Happy to help if you want to DM me. 

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!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 531 Most Valuable Professional

#2
Haque Profile Picture

Haque 261

#3
Kalathiya Profile Picture

Kalathiya 221 Super User 2026 Season 1

Last 30 days Overall leaderboard