October 17, 2019   Tips and Tricks /

Power BI: Custom sort a column

A question often asked is 'how can I do a custom sort in Power BI'. It's a common need but its implementation is not straightforward in Power BI. In this post, I will walk you through it in the easiest possible way.

As an example, I will start with a Sales table that has 3 columns: business regions (EMEA stands for Europe, Middle East, and Africa), month of sales, and sales amount.

Initial table Power BI custom sort
Initial table

This table is the base for a slicer and a bar chart.

Initial slicer and chart
Initial slicer and chart in Power BI

Power BI sorts the regions in alphabetical order, while a more meaningful order for the business is Asia first, followed by North America, Latin America, EMEA, Asia Pacific.

Copy the values to sort

The first step in creating a custom sort is to copy the list of regions. For that, we will use a DAX formula that returns the list of Regions, without duplicates.

Click on Modeling, then New Table. This creates a Power BI calculated table based on a DAX formula.

Create a new calculated table
Create a new calculated table

A text field appears. Enter the following DAX formula and click on Commit:

Table = VALUES('Sales'[Regions])

DAX formula for creating a new table in Power BI
DAX formula for creating a new table in Power BI

The newly created table appears just below.

New calculated table appears
Calculated table

Right-click on Regions and choose Copy. This copies the list of Regions.

Copy values
Copy values

Create a new Power BI table with the custom sort order

With the list of regions that are now copied, we create a regular (non-calculated) table. In the Home tab, click on Enter Data.

Enter Data in Power BI
Enter Data

Paste the Regions list in Column 1.

Paste the values
Paste the values

Click on the star at the top-right to insert a new column.

Add a column
Add a column

Type your custom order.

Enter the custom order
Enter the custom order

Give a name to the table, Regions for example, and click Load.

Load the table
Load the table

The copied list of Regions is now no longer necessary, so you can delete the first added table: right-click on it and choose Delete from the model.

Delete calculated table
Delete calculated table

Sort Regions based on the newly created column

Select the Regions column. 

Select the Regions column
Select the Regions column

Then in 'Sort by Column' choose Regions order.

Sort by Column
Sort by Column

The last step is to make sure there is a connection between the two tables.

Click on 'Model' on the left side to visualize the data model. Here, verify that there is a connection between the initial table and the table with the order. If there is not a connection, drag the Regions column from the Regions table to the Sales table, this creates a connection.

Create relationships in the Power BI model
Create relationships in the Power BI model

You are now done with the preparation part! You can update your visualization

Update your Power BI visualizations and obtain your custom sort

Go back to your visualization and replace Regions from the Sales with Regions from the new table.

Update your Power BI visualization
Update your visualization

If the visualization is not correct yet, click on the three dots at the top right and choose Sort by Regions.

Sort visualization by Regions
Sort visualization by Regions

Congratulations! Your Power BI now has the columns sorted in your custom order!

Final result - custom sort in Power BI
Final result – custom sort in Power BI

Enjoyed the article? Subscribe to the newsletter and download the Power BI of the article for free.

    Share this post

    AUTHORLuca
    I help SMEs who want to get a clear view on what drives their profit. After becoming a CPA and 10 years in Corporate Finance between the US, Europe, and Asia I discovered my real passion. I love helping Small and Medium Companies figure out how to improve their profit and be sustainable.

    Join the discussion

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    LN Consulting
    Tired Of Building A Dashboard By Yourself?

    Continue Reading...

    May 2, 2022
    A recurring problem that Power BI users find is that of incorrect totals. Here is an example: I have a table with Salespeople and their monthly sales, based on that I want to calculate their sales incentive. The sales incentive is equal to 5% of the sales if they sell more than 80k$, otherwise it’s ...
    Read More
    October 9, 2019
    In this tutorial, I’ll walk you through the creation in Power BI of a dynamic list with the Top N products and Other. You can see the result below, where the list is filtered based on the selected month.    Load the external file in Power BI, for creating the top N table For this ...
    Read More