To custom sort a table in Power BI, you create a separate sorting column (like a numeric index) and then use the "Sort by Column" feature to apply it. This lets you define a logical order (e.g., Monday to Sunday, Low to High) that alphabetical or default sorting can't provide.

What You'll Need

  • Power BI Desktop (free version works fine).
  • A table or matrix visual already added to your report.
  • Access to the data model (you can add a new column if needed).

Step-by-Step

  1. Identify the column you want to custom sort. For example, a column named "MonthName" containing "Jan", "Feb", etc., or a column with statuses like "Low", "Medium", "High".
  2. Create a sort order column. In the Fields pane, right-click your table and select "New column". Write a DAX formula that assigns a numeric order. For months, you could use MonthNumber = MONTH(DATE(2024, [MonthName] & 1, 1)) (assuming your month names are text). For statuses, use a SWITCH: StatusOrder = SWITCH([Status], "Low", 1, "Medium", 2, "High", 3). This column will drive the sorting.
  3. Select the column to sort. In the Fields pane, click on the column you want to sort (e.g., MonthName). This selects it.
  4. Apply "Sort by Column". Go to the "Column tools" tab on the ribbon (appears when you select the column). Click "Sort by Column". In the dialog, choose the sort order column you created (e.g., MonthNumber) from the dropdown. Click OK.
  5. Verify the sort in your visual. Click on your table visual. Ensure the field is added to the visual (if not, drag it in). The table should now display in the custom order. If it doesn't, try removing and re-adding the field to the visual.
  6. Optional: Hide the sort column. If you don't want the numeric column to appear in the report, right-click it in the Fields pane and select "Hide". It still works for sorting but won't clutter the field list.

Common Mistakes

  • Sorting the wrong column: You must select the column you want to sort (the text one) before clicking "Sort by Column", not the numeric one.
  • Using a column that has multiple values per row: The sort column must have a one-to-one mapping with the column being sorted. If a sort value maps to multiple text values, sorting may be inconsistent.
  • Forgetting to refresh the visual: After applying the sort, sometimes the visual needs a moment to update. Click on the visual or refresh the report.
  • Sort column not in the same table: The sort column must be in the same table as the column you're sorting. If it's in a related table, Power BI won't allow it.

Tips

  • Use a separate lookup table: For cleaner modeling, create a separate table with the text values and their sort orders, then relate it to your main table. This is especially useful for dynamic slicers.
  • For weekdays: Use WEEKDAY() in DAX or create a mapping based on your locale (e.g., Monday=1, Sunday=7).
  • For quarters: Map Q1, Q2, Q3, Q4 to 1,2,3,4.
  • Sort by multiple columns: You can't directly sort by two columns in a table visual, but you can create a combined sort column (e.g., Year & Month number).
  • Use in matrix visuals: The same "Sort by Column" works for matrix row and column headers.

Troubleshooting

If the sort doesn't apply:

  • Make sure the sort column is numeric (or at least sortable). If it's text, Power BI will sort alphabetically.
  • Check that the sort column has no blank values for any rows. Blanks may cause the visual to sort unpredictably.
  • Try removing the field from the visual, then re-adding it.

If the sort order is wrong (e.g., months still alphabetical):

  • Re-check your DAX formula. For months, ensure your month names exactly match what's in the data (e.g., "Jan" vs "January").
  • In the "Sort by Column" dialog, verify you selected the correct sort column.

If the sort column disappears:

  • If you hid the column and then edit the sort, it may revert. Unhide it temporarily, reapply sort, then hide again.

If you get an error about multiple values:

  • This means your sort column has duplicate values for different text values. Ensure each text value has a unique sort number.

Frequently Asked Questions

Can I sort by a column from another table?

No, the sort column must be in the same table as the column you're sorting. If you need to use a related table, create a calculated column in the original table that references the related value.

Does this work in Power BI Service (online)?

Yes, the sort order is saved with the report and will display correctly in the service, as long as the underlying data model is the same.

Can I sort by a measure?

No, the "Sort by Column" feature works only with columns, not measures. You can create a calculated column that calculates the measure's value per row, but that may not be dynamic.

How do I sort months in fiscal year order?

Create a sort column that assigns numbers based on your fiscal year start. For example, if fiscal year starts in July, map July=1, August=2, ... June=12.

Why does my sort order change when I filter the data?

If your sort column is based on a DAX expression that depends on the filter context, it may change. Use a static mapping (like SWITCH) to avoid this.

Can I use this for a slicer?

Yes, the sort order applies to slicers too. Just ensure the slicer uses the column that has the custom sort applied.