Blog
Dataverse - How to sort the available choice items from a choice column
May 12. 2022
With Dataverse choice columns, there's often the need to sort dropdowns in alphabetical or custom sequences. This post describes how to carry out this task.
Compared to lookup columns, choice columns in Dataverse can be difficult to work with, particularly when we want to sort or filter choice values.
To highlight a typical issue, the Sort formula that many users intuitively use will give the error "The function 'Sort' has some invalid arguments. Cannot sort on the expression type".
To highlight a typical issue, the Sort formula that many users intuitively use will give the error "The function 'Sort' has some invalid arguments. Cannot sort on the expression type".
What's the correct way to sort choice items in a canvas app? This post describes the available methods.
Setting up a choice column in Dataverse
To demonstrate the techniques in this post, we'll create a table called 'Office'. This table contains a choice column called 'Region' with the choices shown below.
Setting up a choice column in Dataverse
To demonstrate the techniques in this post, we'll create a table called 'Office'. This table contains a choice column called 'Region' with the choices shown below.If we now create an auto-generated app based on this table, the regions drop-down displays the regions in the order that they've been added as shown below. A common challenge for app builders is how to sort the choice items in alphabetical sequence.
Defining the sort order of choice items in Dataverse
The preferred way to define the sort sequence is to apply the change globally at the Dataverse level. When we apply a sort sequence globally, it applies to all apps, which saves us from having to duplicate the work in multiple apps. By using this technique, it's also easy to define custom sort sequences that are not in strict alphabetical sequence.
The slight difficulty with this method is that this feature is currently available only in the classic designer - it isn't yet available in the modern designer.
From the classic designer, we can locate the choice items by navigating to the table. Note that the classic designer uses the CDS naming terminology, therefore, the tables will appear beneath the 'entities' folder.
From the table, we then click the edit option against the choice column (Region in this example).
This opens the properties of the column, and there will be a link towards the bottom that opens the 'option set' window (option set is the CDS name for choices).
From this window, we can view the choice items (labelled 'Options'). As the screenshot beneath highlights, we can click the green up and down arrows to reorder the choice items.
How to sort Dataverse choice items in alphabetical sequence from a canvas app
How to apply a custom sort sequence to Dataverse choice items
Finally, we save and publish the changes here to apply the new sort sequence.
How to sort Dataverse choice items in alphabetical sequence from a canvas app
Instead of sorting the choice values from Dataverse, let's say we want to sort the values from within the canvas app. A use case scenario could be to apply a sort sequence that differs from the global sort sequence.
To overcome the common problem mentioned at the beginning of this post, the trick is to convert the choice items values to text by calling the Text function.
Sort(Choices(Region),
Text(Value)
)
If we set the Items property of a dropdown/combo box to this formula, it won't trigger any errors in the designer and will work as expected, as highlighted below.
The natural extension of this example is to sort the values in alphabetical descending sequence. We can achieve this by adapting the formula like so:
Sort(Choices(Region),
Text(Value),
Descending
)
How to apply a custom sort sequence to Dataverse choice items
Another common requirement is to apply a custom sort sequence. Taking our example, the use case scenario here could be to configure a dropdown so that "North America" and "Europe" appear at the top of the list (as these are most frequently used), and the remaining values should then appear in alphabetical sequence.
We can accomplish this with the
following formula:
Sort(AddColumns(
Choices(Region),
"SortColumn",
Switch(Text(Value),
"North America",
"1" & Text(Value),
"Europe",
"2" & Text(Value),
"3" & Text(Value)
)
),
SortColumn
)
Technically, this formula adds a column called SortColumn. The formula sets the value of SortColumn to the choice value text and prefixes the "North America" value with 1, "Europe" with 2, and everything else with 3. Visually, the result looks like this.
When we now sort this data by SortColumn, the choice items will appear in the required sequence as highlighted beneath.
A caveat with this technique is that if any of the choice values begin with a number, we would need to adapt the formula that sets the SortColumn value to take account of this.
Conclusion
With Dataverse choice columns, there is often the requirement to sort
dropdowns or combo boxes in alphabetical or custom sequences. This post describes how
we can carry out this task by defining the sort order in Dataverse, or by using formula in a canvas app.
- Categories:
- Dataverse
Related posts
- Error - Diagnosing the error "Network error when using the Patch function" when saving data with a form
- Dataverse - How to work around the error "multiple levels of many-to-one relationship expansion aren't supported"
- Dataverse - The easiest way to modify Dataverse data outside of Dataverse and Power Apps
- Dataverse - How to use a checkbox control a set a yes/no field in Dataverse
- Dataverse - How to create Entity Relationship diagrams
- Dataverse - How you can more quickly bulk update data using the SQL language
- Dataverse - How to fix the bug in the 'Business Rules' editor that prevents numeric values from saving
- Dataverse - What are the benefits, and how to create formula columns
- Error - Unable to modify Dataverse tables with error message, Language id should not be null
- Dataverse - How to retrieve FetchXML or SQL for Dataverse views
- Data - Why is Dataverse so fast?
- Dataverse - How to create and use Dataverse views in Canvas Apps
- Dataverse - How to filter Dataverse choice columns
- Dataverse - How to switch to the classic designer when the option is not available
- Licensing - What are Dataverse Restricted tables?
- Media - How to save and work with microphone recordings
- Dataverse - how to access tables from another environment
- Dataverse - How to Patch the 5 most complex data types