Sorting options for Power BI visuals
This article describes the different options available for to specifying the way a visual sorts items in Power BI.
By default, a visual doesn't support modifying its sorting order, unless stated otherwise in the capabilities.json file.
The sorting capability requires at least one of the following parameters.
Default sorting
The default
option is the simplest form. It allows the user to sort according to any one field and direction (ascending or descending). The user selects the direction and field from the more options menu.
To enable default sorting, add the following code to your capabilities.json file:
"sorting": {
"default": { }
}
Implicit sorting
Implicit sorting allows you to pre-define a sorting array using parameter clauses
, that describes sorting for each data role. The user can't change the sorting order, so Power BI doesn't display sorting options in the visual's menu. However, Power BI does sort data according to specified settings.
To enable implicit sorting, add the implicit clauses
to your capabilities.json file
clauses
parameters can contain several objects with two parameters each:
role
: DeterminesDataMapping
for sortingdirection
: Determines sort direction (1 = Ascending, 2 = Descending)
"sorting": {
"implicit": {
"clauses": [
{
"role": "category",
"direction": 1
},
{
"role": "measure",
"direction": 2
}
]
}
}
Custom sorting
Custom sorting gives the developer more flexibility when sorting. The developer can:
- Allow the user to sort by multiple fields at a time.
- Set a default sorting order for the data
- Allow custom sorting operations during runtime
Enable custom sorting
To enable custom sorting, add the following code to your capabilities.json file:
"sorting": {
"custom": {}
}
Example: Custom sort API
let queryName1 = this.dataView.matrix.columns.levels[0].sources[0].queryName;
let queryName2 = this.dataView.matrix.columns.levels[1].sources[0].queryName;
let args: CustomVisualApplyCustomSortArgs = {
sortDescriptors: [
{
queryName: queryName1,
sortDirection: powerbi.SortDirection.Ascending
},
{
queryName: queryName2,
sortDirection: powerbi.SortDirection.Descending
},
]
};
this.host.applyCustomSort(args);