Forum Discussion

ArtFi's avatar
ArtFi
Frequent Visitor
9 years ago
Solved

Sort months in chronological order

I know this question has been asked a lot, but my specific issue is that I have lot of different data for one chart (which I select with a slicer) and not every year starts with January, eg if it's May, May gets number 1 in the column next to it (all different options are in the same column, so each month and therefore each month number are mentioned multiple times).

That way, when I try to sort the months by month number (the most common solution that I encountered), I get the following error: [...] You can't have more than one value in 'Number Month' for the same value in 'Month'[...]. I guess that's because the same month in the first column sees multiple values for month number in the other column (as the year does not always begin with the same month like I said).

Does anyone have an idea for a possible workaround?

Thanks, Arthur

  • Hi ArtFi. Can you combine your desired month number with a year prefix to give it a list of one-to-one values? For example, if you want 2015 to be ordered Jan then Feb etc., they would be numbered 201501, 201502. If you wanted 2016 to be ordered Mar, Jan, Feb, then March would still get 201601, then 201602 for Jan and 201603 for Feb. You'd just need to find some way to define the custom sort order in your column, either through a conditional column or maybe hard-coding the values in an Excel table.

     

    If it's just that a year doesn't start with January because there is no January for that year (e.g., no sales for that month), you could still use the YYYYMM format to sort your months, and if the year starts with March, it won't hurt your chronological sort order anyway.

17 Replies

  • KGrice's avatar
    KGrice
    Memorable Member

    Hi ArtFi. Can you combine your desired month number with a year prefix to give it a list of one-to-one values? For example, if you want 2015 to be ordered Jan then Feb etc., they would be numbered 201501, 201502. If you wanted 2016 to be ordered Mar, Jan, Feb, then March would still get 201601, then 201602 for Jan and 201603 for Feb. You'd just need to find some way to define the custom sort order in your column, either through a conditional column or maybe hard-coding the values in an Excel table.

     

    If it's just that a year doesn't start with January because there is no January for that year (e.g., no sales for that month), you could still use the YYYYMM format to sort your months, and if the year starts with March, it won't hurt your chronological sort order anyway.

  • Anonymous's avatar
    Anonymous
    Not applicable

    ArtFi

     

    Can you post some sample data and output you expect.

     

    Cheers

     

    CheenuSing

  • v-haibl-msft's avatar
    v-haibl-msft
    Microsoft Employee

    ArtFi

     

    I’m a little confused about one thing. If one year starts with May, why you need May to return number 1 in the column next to it?

    If you use following column formula for MonthNum, it will return number 5 and you’ll not encounter the problem you have now.

    MonthNum =
    MONTH ( Table1[Date] )

     

    Best Regards,

    Herbert

    • ArtFi's avatar
      ArtFi
      Frequent Visitor

      v-haibl-msft May needs to return 1, because otherwise January of the next year would be 1, February 2 and so on. If I'd try to sort now, January of eg 2016 would before May 2015 in the chart, you see?

      • v-haibl-msft's avatar
        v-haibl-msft
        Microsoft Employee

        ArtFi

         

        As CheenuSing mentioned, you’d better create a calendar table with similar formula as below if you don’t have.

        CalendarTable =
        CALENDAR ( "1/1/2015", "12/31/2016" )

        Then create relationship between these two tables with date key.

         

        Create three columns in the Calendar table. Select MonthName column and make it sorted by YearMonth column.

        YearMonth = 
        YEAR ( CalendarTable[Date] ) * 100
        + MONTH ( CalendarTable[Date] )
        MonthName = 
        FORMAT ( CalendarTable[Date], "mmmm yyyy" )
        Year = 
        YEAR ( CalendarTable[Date] )

        Now you should be able to sort months as below. Make sure you select the MonthName column in Calendar table for Axis.

         

        Best Regards,

        Herbert

  • ArtFi's avatar
    ArtFi
    Frequent Visitor

    Thanks everyone for the effort, I changed month numbers to eg 201505 and changed the month name to May-15, this way it will always sort right, no matter what year or month, and moreover, there is no more confusion between name month and number!

  • I think I have a much simpler answer.  The 2 main issues are the first letters of each month of the year are not in alphabetical order, so Excel always wants to put April first instead of January.  And if you try to give each month a number, October, November, and December present problems because 10, 11, and 12 get put before 1 (which would be January).

    So, however you're going to label your months (typically January = 1, February = 2 and so on), make October = 91 (because September would have equaled 9), make November = 92 and December = 93. 

    This gets your pivot table sorted correctly and when you go to make your graph, you can just rename each of those fields to be whatever you want.  And most importantly, they'll stay in the same order you want them.