cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ale_BI
Helper I
Helper I

Ordering table not by alphabetical sort, or index

ale_BI_0-1669714913414.png

 

I have this table and I want to order it so that the order is Men, Women, Other, Total. How can I do that with Power Query? Also, when I change the order in DAX, and do a sort column on [Gender] by [Cod. Gender], it removes the Total from the graphs. 

Instead of looking like this:

ale_BI_1-1669715047166.png

 

It looks like this when I do the sort by: 

 

ale_BI_2-1669715082595.png

 

 

1 ACCEPTED SOLUTION
BA_Pete
Super User
Super User

Hi @ale_BI ,

 

To get a custom sort order, you need to define it in a new column in your table.

For your scenario, you would add another column like this:

 

// In Power Query M
if [Cod. Gender] = "M" then 1
else if [Cod. Gender] = "F" then 2
else if [Cod. Gender] = "O" then 3
else 99

// OR

// In DAX
SWITCH(
  [Cod. Gender],
  "M", 1,
  "F", 2,
  "O", 3,
  99
)

 

 

Once you have your new column, go the Data View 

BA_Pete_0-1669730668173.png

 

Select your [Gender] column, then go to the Column Tools tab > Sort by Column

BA_Pete_1-1669730765325.png

 

Select your new sort key column from the list an the text values will now be sorted in order of your sort column instead.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

1 REPLY 1
BA_Pete
Super User
Super User

Hi @ale_BI ,

 

To get a custom sort order, you need to define it in a new column in your table.

For your scenario, you would add another column like this:

 

// In Power Query M
if [Cod. Gender] = "M" then 1
else if [Cod. Gender] = "F" then 2
else if [Cod. Gender] = "O" then 3
else 99

// OR

// In DAX
SWITCH(
  [Cod. Gender],
  "M", 1,
  "F", 2,
  "O", 3,
  99
)

 

 

Once you have your new column, go the Data View 

BA_Pete_0-1669730668173.png

 

Select your [Gender] column, then go to the Column Tools tab > Sort by Column

BA_Pete_1-1669730765325.png

 

Select your new sort key column from the list an the text values will now be sorted in order of your sort column instead.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Helpful resources

Announcements
PBI Sept Update Carousel

Power BI September 2023 Update

Take a look at the September 2023 Power BI update to learn more.

Learn Live

Learn Live: Event Series

Join Microsoft Reactor and learn from developers.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

Top Solution Authors
Top Kudoed Authors