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 table by a dyna...
Power Apps
Answered

Sort a table by a dynamic column

(0) ShareShare
ReportReport
Posted on by 140

In my app i have a table that i want to give the user the option to sort by different columns.

the user choose the column name through a combobox and the sort type by another combobox 

combobox1: items : ["column1","column2","column3"]

combobox2: items: ["SortOrder.Ascending","SortOrder.Descending"]

button : OnSelect: Set(selectedSort,ComboBox1.Selected.Value);
                              Set(selectedColumn,ComboBox1.Selected.Value)

 

Table1 : Items: Sort('DataBase','selectedColumn',SortOrder.Ascending)

 

It doesn't work ! i cann't find what's wrong

 

Categories:
I have the same question (0)
  • Scott_Parker Profile Picture
    1,090 on at

    It is a limitation with the Sort() function and other functions that refer to columns by name. You must, without exception, provide column names as primitive strings. That is, hard-coded text values. You can't change the column name dynamically using a variable like you are attempting to do here.

     

    To get around this limitation, you will have to write separate Sort() formulas, one for each column you wish to sort by, and then pick one using a Switch() or If() function.

  • Verified answer
    v-qiaqi@microsoft.com Profile Picture
    on at

    Hi @Hanan-khamissy,

    As @Scott_Parker mentioned, you need to write an if statement to sort by columns one by one based on selected column name.

    If(ComboBox1.Selected.Value="column1","column2","column3"
    Sort('DataBase',"column1",If(ComboBox2.Selected.Value="SortOrder.Ascending", SortOrder.Ascending,SortOrder.Descending)),
    ComboBox1.Selected.Value="column1",
    Sort('DataBase',"column2",If(ComboBox2.Selected.Value="SortOrder.Ascending", SortOrder.Ascending,SortOrder.Descending)),
    ComboBox1.Selected.Value="column1",
    Sort('DataBase',"column3",If(ComboBox2.Selected.Value="SortOrder.Ascending", SortOrder.Ascending,SortOrder.Descending))
    )

     

  • Hanan-khamissy Profile Picture
    140 on at

    The problem is that in addition to the sorting I have a complete set of filters 

    It means in each  

    'DataBase'

    i need to replace it with  a complex of filters 

  • Verified answer
    Scott_Parker Profile Picture
    1,090 on at

    Not necessarily. You can use use With() to so you only have to write the filters for 'DataBase' in one place in the code. Such as:

    With(
    	{DataBase: Filter(<your code here, etc.>)},
    	If(ComboBox1.Selected.Value="column1",
    		Sort('DataBase',"column1",If(ComboBox2.Selected.Value="SortOrder.Ascending", SortOrder.Ascending,SortOrder.Descending)),
    		ComboBox1.Selected.Value="column2",
    		Sort('DataBase',"column2",If(ComboBox2.Selected.Value="SortOrder.Ascending", SortOrder.Ascending,SortOrder.Descending)),
    		ComboBox1.Selected.Value="column3",
    		Sort('DataBase',"column3",If(ComboBox2.Selected.Value="SortOrder.Ascending", SortOrder.Ascending,SortOrder.Descending))
    	)
    )

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 721 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 320 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard