Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power BI Table Order Categories

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Yes, you can reorder categories in Power BI tables to match your specific reporting needs, but you cannot sort a static matrix table directly. Instead, you must create a calculated column that assigns a numeric sort order to your categories, which Power BI uses to arrange the table rows automatically. This effectively sorts the table by the calculated column, placing your defined categories in the correct sequence.

What You'll Need

  • A Power BI Desktop file (.pbix) containing at least one table with a text column for the categories you wish to reorder.
  • A basic understanding of Data Modeling concepts, specifically the difference between a calculated column and a measure.
  • A working installation of Power BI Desktop (the steps below are for the Desktop application).

Step-by-Step

Follow these steps to reorder your categories in a table visual. This method uses a calculated column to force a specific order, such as chronological, priority-based, or alphabetical, rather than the default alphabetical order.

  1. Open your report and select the visual that displays the table with the categories you want to reorder. This visual must be a standard Table visual, not a Matrix visual, as Matrix visuals do not support sorting by a calculated column directly from the Columns pane.

  2. Create a calculated column to define the order. Go to the Modeling tab on the ribbon and select New Column. This action opens the formula bar at the top of the screen.

  3. Write the DAX formula to assign a number to each category based on your desired order. The exact formula depends on how you want to sort the data. For example, if you want to sort by a specific priority list, you can use the SWITCH function. Here is an example formula for a column named CategorySortOrder:

    CategorySortOrder = 
    SWITCH(
        TRUE(),
        'YourTableName'[CategoryColumn] = "High Priority", 1,
        'YourTableName'[CategoryColumn] = "Medium Priority", 2,
        'YourTableName'[CategoryColumn] = "Low Priority", 3,
        4
    )

    In this formula, "High Priority" is assigned the number 1, "Medium Priority" is 2, and so on. If a category is not found in the list, it is assigned the default number 4.

  4. Add the calculated column to the visual. Go back to the Fields pane and expand your table. You will see the new CategorySortOrder column. Drag this column and drop it into the Values area of the Fields pane for your table visual.

  5. Sort the table by the calculated column. With the CategorySortOrder column in the Values area, Power BI will automatically sort the table rows based on this numeric column. The rows will now appear in the order you defined in the SWITCH formula.

  6. Remove the calculated column from the visual (optional). If you do not want the sort order number to be visible in your final report, you can remove the CategorySortOrder column from the Values area of the visual. The sort order will remain applied, but the column itself will not be displayed.

  7. Save your report to preserve the changes.

Common Mistakes

  • Using a Matrix visual: The most common mistake is trying to apply this method to a Matrix visual. Matrix visuals have a dedicated Sort by column feature in the visual header that is not connected to the table’s calculated columns. The method described above works only for Table visuals.
  • Forgetting to refresh data: If your category list is dynamic (e.g., coming from a database where new categories are added), the calculated column must be refreshed. In Power BI Desktop, simply clicking Refresh on the Home tab will recalculate the column based on the latest data.
  • Using a Measure instead of a Column: Measures are calculated at query time and cannot be used to sort a table. They are aggregations like sums or averages. You must use a Calculated Column, which is stored in the data model, to create a static sort order.

Tips

  • Use the SEQUENCE function for alphabetical sorting: If you simply want to sort categories alphabetically but in reverse order (Z to A), you can use the SEQUENCE function. This is a more concise way to achieve the same result without listing every category manually. For example: CategorySortOrder = RANKX(ALL('YourTableName'[CategoryColumn]), 'YourTableName'[CategoryColumn], , ASC). This ranks all unique categories in the table in ascending order.
  • Consider a slicer: If you want to allow users to dynamically change the sort order of the table, you can create a slicer based on your calculated sort order column. This would allow users to toggle between different sorting views.
  • Performance considerations: Calculated columns are calculated for every row in the table, which can increase the size of your data model and slow down report performance. Only use calculated columns for sorting if the performance impact is acceptable for your use case.

Troubleshooting

If your categories are not appearing in the correct order after following the steps, check the following.

  • Check the visual type: Ensure you are working with a Table visual, not a Matrix visual. As mentioned, the sort by calculated column method does not work on Matrix visuals.
  • Verify the column in the Values area: Ensure that the calculated column is actually in the Values area of the Fields pane for the visual. Sometimes, the column is created but forgotten to be dragged into the visual.
  • Check for blank values: If your calculated column contains blank values, Power BI may place them at the end of the list by default. You can modify your DAX formula to handle blanks explicitly, such as by assigning them a specific number (e.g., 0) or excluding them from the sort order.
  • Verify data types: Ensure that the data type of your calculated column is set to Whole Number. If it is set to Text, Power BI will sort it alphabetically, which will not provide the desired numeric order.

Frequently Asked Questions

Can I sort a Matrix visual in a custom order?

No, you cannot sort a Matrix visual directly by a calculated column created in the table. Matrix visuals have their own sort functionality that is separate from the table’s calculated columns. To achieve a custom sort order in a Matrix visual, you typically need to use a measure or a slicer to control the sort order of the underlying data.

Does the calculated column need to be visible in the table?

No, it does not. Once you have added the calculated column to the Values area to establish the sort order, you can remove it from the visual. The sort order will remain applied to the table rows, but the column itself will not be displayed to the end-user.

Why is my calculated column not updating?

If you have made changes to the underlying data source and the calculated column is not updating, ensure that you have refreshed the report. Click the Refresh button on the Home ribbon. If the column still does not update, check the DAX formula for any errors or syntax issues that might prevent it from recalculating.

Can I use this method to sort by multiple criteria?

Yes, you can use the RANKX function to create a calculated column that sorts by multiple criteria. For example, you can create a column that first sorts by a priority number and then by a date. The RANKX function allows you to specify a sort order for each column in the ranking expression.

Does this work in Power BI Service?

Yes, this method works in both Power BI Desktop and the Power BI Service. The calculated column is part of the data model, so once the report is published to the Service, the sort order will be maintained for all viewers.