Smart Systems for Home

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

Power BI Table Order by Multiple Columns

· 7 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 sort a Power BI table by multiple columns by using the Sort By Column feature in the Fields pane. This creates a hierarchical sort where the primary column determines the order, and then any secondary columns break ties between rows with identical values in the primary column. You do not need to write any DAX code to achieve this; it is a visual configuration within the report editor.

What You'll Need

  • A Microsoft Power BI account or access to the Power BI Service (web or desktop).
  • A data model or dataset loaded into Power BI that contains at least one table with multiple columns of data.
  • A report or dashboard currently open in the Power BI Desktop or web interface.

Step-by-Step

  1. Open your report. Launch Power BI Desktop and open the report file you wish to modify, or navigate to your existing report in the Power BI service.
  2. Locate the 'Fields' pane. On the left-hand side of the screen, find the pane labeled 'Fields'. If you cannot see it, go to the top menu and select 'View' > 'Fields'. This pane lists all the tables and fields available in your data model.
  3. Identify the target table. Find the table in the Fields pane that contains the columns you want to sort by. Click the little arrow next to the table name to expand it and reveal its columns.
  4. Select the primary sort column. Click on the column header inside the Fields pane that you want to be the main sort order. For example, if you want to sort by 'Category' first and then by 'Sales Amount' within each category, select 'Category' first. You will see a small arrow appear next to the column name in the pane, indicating it is currently sorted.
  5. Sort by a secondary column. With the primary column selected, you can now add a secondary sort. Right-click on the name of the secondary column (in this example, 'Sales Amount') within the Fields pane. From the context menu that appears, select Sort By Column.
  6. Choose the target. A dialog box will appear asking which column to sort by. Ensure the column you want to use as the secondary sort is selected (e.g., 'Sales Amount'). Click 'OK' to confirm the selection.
  7. Verify the sort order. Return to your visual (the table, card, or matrix) on the canvas. The rows should now be ordered first by your primary column and then by your secondary column. If you have duplicate values in the primary column, the secondary column will dictate their relative position.
  8. Repeat for further columns. If you need a tertiary or fourth sort order, simply repeat steps 5 and 6. Right-click a different column and select 'Sort By Column', choosing the next column in your desired hierarchy. You can add multiple levels of sorting to any table.

Common Mistakes

Sorting the visual instead of the field. A frequent error is trying to sort the actual table visual on the canvas by clicking the column headers within the visual itself. While this works for a single column sort, it overrides the hierarchical sort created in the Fields pane. Always sort in the Fields pane to maintain the multi-column order.

Not understanding the hierarchy. The sort order is strict. If you have a primary column of 'Region' and a secondary column of 'City', all 'North' regions will be grouped together. Within that group, they will be sorted by 'City'. You cannot have 'City' appear before 'Region' in the sort hierarchy; the order in the Fields pane dictates the final visual order.

Sorting DAX measures. You cannot sort by a measure (a calculated value) in the same way. The 'Sort By Column' feature is strictly for columns in your data model. If you want to sort by a calculated value, you must create a calculated column using DAX and sort by that new column instead.

Tips

Use the Sort By Column menu for speed. Instead of right-clicking every time, you can access the sort options via the column header in the Fields pane. Click the arrow next to the column you want to sort by. This will open a small menu where you can select 'Sort Ascending', 'Sort Descending', or 'Sort By Column'. This is often faster than right-clicking the column name directly.

Sort Descending is useful for rankings. When sorting by sales figures or revenue, you typically want to see the highest values at the top. After setting up your multi-column sort, click the sort arrow in the visual and select 'Sort Descending' for the primary column.

Matrix visuals handle hierarchy automatically. If you are using a Matrix visual, the hierarchy of your fields in the 'Rows' area often dictates the sort order. However, you can still use 'Sort By Column' on specific fields within the matrix to refine the order of the innermost columns if the automatic hierarchy doesn't match your needs.

Troubleshooting

The table is not sorting correctly. If you have followed the steps and the visual still appears unsorted, check that the field is actually part of the visual. Ensure that the column you selected to sort by is included in the table visual on your report canvas. If a field is in the Fields pane but not in the Values or Rows areas of the visual, it will not affect the sort order.

The sort is not persistent. In the Power BI Service (web), if you share your report and the sort order disappears for other viewers, it is likely because the sort is not set on the data source or dataset itself. The sorting in the report editor is often a visual state. To make the sort permanent for everyone, you may need to edit the data view and set the sort order there, or ensure the report is saved as an app with the appropriate permissions.

Can I sort by a calculated column? Yes, absolutely. If you need to sort by a complex DAX calculation, you must first create a 'Calculated Column' within the table in the Table view or Data view. Once the column exists, you can treat it exactly like any other column in the data model and use the 'Sort By Column' feature on it.

Why is my list not unique? If you have multiple rows with identical values for both your primary and secondary columns, the sort will place them adjacent to one another. Power BI will not automatically remove duplicates. If you need to see only unique values, you will need to use a DAX measure with the DISTINCTCOUNT function or create a separate table with unique records.

Frequently Asked Questions

Can I sort by more than two columns?

Yes, you can add an unlimited number of secondary sorts. Simply right-click a new column and select 'Sort By Column' for each additional level of hierarchy you require. The sort will process from the primary column down to the last secondary column you added.

Does this work in the Power BI mobile app?

Sorting functionality in the mobile app is limited. You can view the data, but you generally cannot change the sort order of the visuals. The sort order you set in the desktop or web editor is usually preserved when viewing the report on mobile, but you cannot edit the sort hierarchy from the phone.

How do I clear a sort order?

To remove a sort, right-click the column header in the Fields pane and select Clear Sort. This will revert the column to its default order (usually alphabetical or by the order the data was imported).

Is the sort order saved with the dataset?

In Power BI Desktop, the sort order is saved with the report file (.pbix). In the Power BI Service, the sort is typically associated with the report view. If you edit the data model directly, you can set the sort order permanently on the column metadata itself, which will apply to all reports that use that dataset.

Can I sort by a measure in a table visual?

Directly sorting by a measure is not supported via the 'Sort By Column' menu. However, you can sort by a measure by adding a calculated column that uses the measure's value, or by using the 'Sort By' feature on the visual itself (where you select the measure and choose 'Ascending' or 'Descending'). The latter is a visual sort, whereas the Fields pane method is a data model sort.