You can achieve a multi-level sort in Power BI by adding secondary and tertiary columns to your data model and then setting the sort order on the visual field list or the field well. This allows you to organize data first by a primary key, then by a secondary metric, and finally by a tertiary category, ensuring your table always displays information in the exact hierarchy you need.
What You'll Need
- A Power BI Desktop application installed on your computer.
- A data source connected to your report, such as an Excel file, a SQL Server database, or a CSV.
- Basic familiarity with the Power BI Desktop interface and the data model view.
Step-by-Step
- Connect to your data and load it into the model. Open Power BI Desktop and connect to your desired data source. Ensure the tables are loaded into the model. If your data is already loaded from a previous session, you can skip to the next step.
- Open the Data view. In the left-hand navigation pane, click on the "Data" icon. This view is dedicated to inspecting the raw data and the relationships between tables.
- Identify the column you want to sort by first. In the Data view, look at the table that corresponds to the visual you want to sort. For example, if you want to sort a sales table by Product Category first, then by Sales Amount, you will need to work with the "Product" table and the "Sales" table.
- Create a new calculated column for the first sort level. In the Data view, right-click on the table you want to modify and select "New column." In the formula bar, write a DAX expression that returns the value you want to use for the primary sort. This is often the column you want to sort by but is not already in the table. For instance, if your table is a "Sales" table but you want to sort by the "Product Name" that is in a related "Product" table, you would create a calculated column in the Sales table that performs a lookup. The DAX formula might look like this: . If the tables are not related, you will need to create a relationship between them first in the Relationships view.
- Create a calculated column for the second sort level. Repeat the process to create another calculated column. This column should represent the value you want to use for the secondary sort. For example, you could create a calculated column called
Sales Amount Sortedthat returns the Sales Amount but with negative values converted to positive values so that they appear at the end of the list. The DAX formula might look like this:Sales Amount Sorted = IF('Sales'[Sales Amount] < 0, 'Sales'[Sales Amount] * -1, 'Sales'[Sales Amount]). This is a common technique to ensure that negative numbers (like returns) do not appear before positive numbers (like sales) in your sort. - Create a calculated column for the third sort level. For a three-level sort, you would repeat the process one more time to create a calculated column for the tertiary sort. This column should represent the value you want to use for the third sort. For example, you could create a calculated column called
Sales Date Sortedthat returns the Sales Date but with future dates converted to a date in the distant past so that they appear at the end of the list. The DAX formula might look like this:Sales Date Sorted = IF('Sales'[Sales Date] > TODAY(), 'Sales'[Sales Date] - 10000, 'Sales'[Sales Date]). This is a common technique to ensure that future-dated orders do not appear before past-dated orders in your sort. - Return to the Report view. Click on the "Report" icon in the left-hand navigation pane. This is where you build your visualizations and interact with your data.
- Add your table visual. From the "Visualizations" pane on the right, click on the "Table" icon (the one that looks like a grid). This will add an empty table to your canvas.
- Add your primary sort field to the field list. In the "Visualizations" pane, you will see a section labeled "Values.” Drag and drop the calculated column you created in Step 4 (e.g.,
Product Name Lookup) into the Values section. This is the column that will be sorted by first. - Add your secondary sort field to the field list. Drag and drop the calculated column you created in Step 5 (e.g.,
Sales Amount Sorted) into the Values section. - Add your tertiary sort field to the field list. Drag and drop the calculated column you created in Step 6 (e.g.,
Sales Date Sorted) into the Values section. - Set the sort order on the visual field list. Click on the arrow icon next to the field name in the Values section of the Visualizations pane. A menu will appear. Select Sort by and then choose the calculated column you want to use for that specific sort level. For example, you would select Sort by >
Sales Amount Sortedfor theProduct Name Lookupfield. This tells Power BI that theProduct Name Lookupcolumn should be sorted based on the values in theSales Amount Sortedcolumn. Repeat this process for the remaining fields. Click on the arrow next toSales Amount Sorted, select Sort by >Sales Date Sorted, and then click on the arrow next toSales Date Sorted, select Sort by >Sales Date Sorted(or the original date column if you didn't use a calculated one). - Verify the sort order. Your table should now be sorted by the primary field, and within each group of the primary field, it should be sorted by the secondary field, and so on. Check the order of the data to ensure it matches your desired hierarchy.
Common Mistakes
Sorting the wrong field in the field list. A common mistake is to try to sort a field by the original, unmodified column instead of the calculated column. For example, you might try to sort the Product Name Lookup field by the original Product Name column. This will not work because the calculated column is the one that has the values you want to sort by. You must sort the field by the calculated column that contains the values you want to use for the sort.
Not creating a relationship between tables. If you are trying to sort a field from one table by a field from another table, you must have a relationship between the two tables. If there is no relationship, you will not be able to create the calculated column that performs the lookup. You will need to go to the Relationships view and create a relationship between the two tables before you can proceed.
Using the wrong DAX function. The RELATED function is used to retrieve a value from a related table. It assumes that there is a one-to-many relationship between the tables, with the current table on the one side. If your relationship is many-to-many, you will need to use the RELATEDTABLE function instead. If you are unsure about which function to use, you can consult the DAX documentation.
Forgetting to clear the old sort order. If you have previously sorted a field in the field list, you must clear the old sort order before you can set a new one. To clear the sort order, click on the arrow icon next to the field name in the Values section of the Visualizations pane and select Clear Sort.
Tips
Use the field well for quick sorting in Report view. If you have a table or matrix visual on your report canvas, you can quickly set the sort order for a field by right-clicking on the field name in the visual itself. Select Sort by and then choose the column you want to sort by. This is a faster way to set the sort order than using the field list in the Visualizations pane.
Sort by multiple columns in the same visual. The method described above allows you to sort by multiple columns in the same visual. This is useful for creating complex hierarchies. For example, you could sort a sales table by Product Category, then by Sales Amount, and then by Sales Date.
Use calculated columns for complex sorting logic. Calculated columns are powerful tools that allow you to create custom sorting logic that is not possible with the built-in sort options. For example, you can create a calculated column that sorts text values in a custom order, or you can create a calculated column that sorts dates in a custom order.
Be aware of performance implications. Calculated columns are calculated when the data is refreshed. This means that they can slow down the refresh time of your report. If you have a large dataset, you may want to consider using measures instead of calculated columns for sorting. However, measures are calculated dynamically and cannot be used for sorting in the same way.
Troubleshooting
The table is not sorting correctly. If your table is not sorting correctly, first check that you have added the correct calculated columns to the field list. Then, verify that you have set the sort order on the correct fields in the field list. Make sure you are sorting the field by the calculated column, not the original column.
The calculated column is not returning the expected values. If your calculated column is not returning the expected values, check the DAX formula you used. Make sure that the table and column names are correct and that the relationship between the tables is correct. You can also try copying the formula into the DAX editor to see if there are any syntax errors.
The sort order is not stable. If the sort order is changing unexpectedly, it could be because the calculated columns are not stable. For example, if you used a function like TODAY() in your calculated column, the values in the column will change every day. This can cause the sort order to change every day. To fix this, you can use a static date in your calculated column instead of TODAY().
Power BI is not allowing me to sort by a calculated column. If Power BI is not allowing you to sort by a calculated column, it could be because the calculated column is not added to the Values section of the field list. Make sure that you have dragged and dropped the calculated column into the Values section. You can also try creating a measure instead of a calculated column.
Frequently Asked Questions
Can I sort a table by a measure?
No, you cannot sort a table or matrix visual by a measure directly. You can only sort by columns. To sort by a measure, you must create a calculated column that returns the value of the measure for each row of the table.
How do I sort a table by multiple columns in ascending and descending order?
Power BI allows you to sort by multiple columns, but all columns will be sorted in the same direction (ascending or descending). To sort by multiple columns in different directions, you must use the method described in this guide, which involves creating calculated columns with modified values.
Is there a way to sort a table by a calculated column without creating a new column?
There is no direct way to sort by a calculated column without creating a new column. However, you can use a trick to achieve the same result. You can create a calculated column that returns a static number for each row, and then sort by that column. This can be useful if you want to sort by a complex calculation without having to create a separate column for it.
Can I use this method to sort a slicer?
No, this method cannot be used to sort a slicer. Slicers are used to filter data, not to sort it. To sort a slicer, you would need to use the sort order options in the slicer itself.
Why is my calculated column taking a long time to refresh?
Calculated columns are calculated when the data is refreshed. If your dataset is large, the refresh time can be long. You can improve the performance of your calculated columns by using efficient DAX formulas and by avoiding complex calculations.