Sub topic 7
Sorting Month Names Chronologically
This chapter assumes you can work in Power BI Desktop's Report, Table, and Model views. It also assumes you know basic DAX for calculated columns, and that your date table already has Month and Month Number columns, as built in the previous chapters.
Why month names sort alphabetically
Power BI sorts a text column by its text values. A column of month names such as "Jan", "Feb", and "Mar" appears in alphabetical order: Apr, Aug, Dec, Feb, and so on. A chart that uses this column as its axis shows the months in the wrong order, and a year-to-date trend becomes hard to read.
The fix is the Sort by column setting. It tells Power BI to order the values of one column by the values of another column. The sorting column is normally a number that matches each text value.
Columns that need a numeric partner
The calendar columns from the earlier chapter include text columns and the numeric columns that match them:
| Text column | Sort by this numeric column |
|---|---|
| Month | Month Number |
| Quarter | Quarter Number |
| Year-Month | Date (or a numeric `YearMonthNumber`) |
The Year column is numeric already, so it sorts correctly. Year-Month in the yyyy-MM format also sorts correctly as text, because the year comes first and the month is zero-padded. You can leave it alone.
Set the sort order in Power BI Desktop
- Open Data view (Table view) and select the date table.
- Click the
Monthcolumn. - On the Column tools ribbon, choose Sort by column, then select
Month Number. - Repeat for
Quarter, and chooseQuarter Number.
You can also do this in Model view. Select the column and use the Sort by column option in the Properties pane.
The effect is immediate. A visual that uses Month on its axis now shows Jan through Dec.
A one-to-one rule
Sort by column requires a one-to-one match between the two columns. Each month name must map to exactly one month number, and each number to exactly one name. If a name maps to several numbers, Power BI shows an error similar to "Cannot sort by column because it has multiple values".
This is why the sort column for Month is Month Number and not Date. A month name such as "Jan" appears in every year of the table, so it matches many dates. If you want the months to be distinct for each year, create a column that includes the year and a numeric partner for it:
Year-Month Number = YEAR ( [Date] ) * 100 + MONTH ( [Date] )Each value, such as 202603, maps to exactly one Year-Month text value, so you can sort Year-Month by it.
Check your work
Build a column chart with Month on the axis and Total Sales as the value. Confirm that:
- The months run from January to December from left to right.
- The sort order is not changed by switching the visual's sort option to a different field.
If the order is still alphabetical, check whether the text column is the one with Sort by column set. The setting belongs to the column you display, and it is not inherited by other columns.
Common problems
- Months still alphabetical: the Sort by column setting is on a different column from the one used in the visual.
- Multiple values error: the sorting column has more than one value for the same text value. Pick a column that matches one to one.
- Sort order resets after refresh: this happens when the column was removed and re-created. Reapply the setting.
Exercise
A colleague sets Month to sort by Date and gets an error. Why does it fail, and which column should they use?
Show answer
The month name "Jan" appears on many dates, so it has many Date values, and Sort by column needs a one-to-one match. They should sort Month by Month Number, which has one value for each month name. To keep each year's months distinct, they can use Year-Month sorted by a Year-Month Number column.
Exercise
Write the DAX for a calculated column that gives each month name in Month a numeric partner, then state the setting to apply.
Show answer
Month Number = MONTH ( [Date] ). Then select the Month column and, in Column tools, set Sort by column to Month Number.