This can be thorny problem. The issue is when you use the format function to extract the month from the date. You then, proud that you have accomplished that, insert the same month into a matrix visualisation or slicer only to find April at the top and September at the bottom.
You can create a formula similar to the following to extract the month name from the date:
Isn't the above a pain? You simply want to sort by month. What you can learn here will not only help you when you're sorting by month. It will also help when you want to sort any column by any other column. This can help with Month Names, Days of the Week and even regional areas. Let's start off with Month numbers then we'll try days of the week. At the end we'll have a look at a couple of issues that are common to sorting columns. Why they occur and how to overcome them.
Create a new calculated column called month number and enter in the following:
the data should look like follows:
Now that we've done that, ensure that the Month field is seleted.
Then choose Month Number from the Sort By Column at the top of the screen.
Swap back to your visualisation and see the months are in the correct order.
Now we have the month order we know and love. Well, probably not love but at least it's something we're familiar with. The astute of you will remember that I said, apart from sorting by month number, you can use the sort-by-column feature to sort any column by any column and that much is true. Next we'll use this same approach to sort by Weekday. You know? The day of the week. (Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday).
We shall first use the DAX FORMAT function to get the day of the week in text format. The. we will use the WEEKDAY function to extract the week day number. We need this so that we can correctly sort the text field. Have go at this example::
First create a calculated column to and enter a formula similar to the following where [Date] would be the date column or field for the data you wish to analyse.
You will see the following displaying the the full day of the week.
Pop that data into a slicer visualisation and you'll get the following:
So as you can see, we have encountered the same issue. The days of the week are sorted alphabetically. Let's solve this problem in exactly the same way as we did the months.
Add a new column with the name Weekday Number and enter in the following:
= WEEKDAY([Date Sold],2)
The number "2" after the column tells the function to take Monday as the first day of the week.
You should see a column that looks like:
Now click on the Weekday column and sort the column by Weekday number. Just like we did with the month number. Then swap back to the slicer and your weekdays should be in the correct order:
As you can see, the sort by column is very handy however there are a couple of warnings. The first of these warnings you may come across is this:
The first of the problems you may encounter is the above. Basically, Power BI needs to have a matching values in both columns. So when, as we have tried in the above example, you try to sort a column that doesn't have matching values you naturally get an error. As above we are trying to match Week Number with Weekday. This can't work! This is because Week day 1 could have numerous week number values. Basically, the number of times that Monday appears in the year. To get this to work you can only use Weekday number. Which returns a value of 1 - 7 depending on what day of the week it is.
The other error you may receive is:
This is another error you might come across. You, obviously, can't sort one column by the column it's already being sorted by. In the above example I was trying to sort Seasons. I used a SWITCH function work out the month and the seasons which looked something like:
Season = SWITCH([Month No],
Then I tried to work out the order of the seasons by using this same column to assign numbers to the seasons. Something like the SWITCH statement below. This seems like it works, but it doesn't :(
Season Number = SWITCH([Season],
I can see the error when I try to sort the Seasons by the Season number. When I do this I get the "Sort by another column" error as you can see above.
A possible solution to this problem is you do a longer switch statement looking to month again, such as:
Season No = SWITCH([Month No],
This way month 1 is season 4 (which is winter), month 2 is also season 4. Month 3 is season 1 or spring and so on. Because this column is linking to another column that is not dependant on this column for sorting you can use it to sort season.
Hope this makes sense and that you got something out of this. If you want more information check out our one day Power BI beginners training course, that should get you up to speed.
We also have some pretty nifty training vides about Power BI and DAX