Sorting Columns in the PowerApps Data Table

I’m hoping that by the time most people read this, native support for filtering and sorting the new Data Table control in PowerApps will be released, but since some users may be wanting this functionality now, here’s a quick guide to adding sorting to your columns:

In my sample app, I have added a Data Table and connected it to a simple SharePoint list.  This Data Table has 3 columns, and I have added Sort Icons on each column:

dt1

I have set the OnSelect property of each of these icons to UpdateContext to trigger the context to change on selection.  Each icon uses a separate context number so as to keep the variable separate:

UpdateContext({SortDescending1: !SortDescending1,SortField: “Title”})

UpdateContext({SortDescending2: !SortDescending2, SortField: “Location”})

UpdateContext({SortDescending3: !SortDescending3, SortField: “Status”})

Then I set the ‘Items’ property of the Data Table to the following:

If(SortField=”Title”, Sort(‘Item Tracking’, Title, If(SortDescending1, Descending, Ascending)),
If(SortField=”Location”, Sort(‘Item Tracking’, Location, If(SortDescending2, Descending, Ascending)),

If(SortField=”Status”, Sort(‘Item Tracking’, Status, If(SortDescending3, Descending, Ascending)),
‘Item Tracking’)))

Now, when ever you click one of the Sort buttons, it will sort that column either Ascending or toggle to Descending!

If you’d like to learn more about PowerApps, or are interested in building some for your organization, please feel free to reach out to me at jo.karnes@centricconsulting.com

4 Comments Add yours

  1. Does this work for People and Group,Lookup,Choice Columns ?

    Like

  2. amac44 says:

    I like the approach here. I think the Items property could be simplified with a Switch statement — Sort(‘Item Tracking’, Switch(SortField, “Title”, Title, “Location”, Location, “Status”, Status), If(SortDescending, Descending, Ascending)) . And I also collapse all the SortDescending’s into one. If there’s a reason to track them separately (multi-column sort?) then you could convert my If into another Switch.

    Like

  3. Shweta says:

    If the datatable has horizontal scroll, then the sort icon remains in the same place even when the scroll is moved.
    How to solve this issue?

    Like

  4. Glenn Jones says:

    Slightly off topic, however do you know how to move the sort buttons if the columns resize or user scrolls off to the right, making the first column(s) out of sight?

    Like

Leave a Reply