Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
ShivGC
Helper I
Helper I

Reversing Date Hierarchy

Hi, 

Does anyone know how to reverse date hierarchy?

I have added the date into a matrix visual in the 'Columns' section but the years are showing from 2021 upwards, however I want the most recent year to be dipslayed first?

 

1 ACCEPTED SOLUTION
danextian
Super User
Super User

Hi @ShivGC 

 

Matrix can be sorted only by the measure or row categories. Column categories are either sorted alphabetically or by using a custom column sort. You can rank the year in your dates table to get the reverse sort.

Year Sort = 
RANKX(ALL(Dates[Year]), Dates[Year],,DESC,Dense)

danextian_0-1738232625223.png

As the rank calc column is referencing the year column, year cannot be sorted directly by this column as this will cause circular dependency.

danextian_1-1738232681704.png

Your alternative is to create another calculated column referencing the original year column, custom sort that instead and then use it in your viz.

danextian_2-1738232764208.png

Alternatively and if you are using  DAX calendar, you can the sort column directly in to the table itself and not as a calc column. Here's a sample DAX calendar with a year sort/rank column built-in

Dates = 
VAR __MIN =
   DATE ( 2022, 1, 1 )  
VAR __MAX =
    TODAY()
VAR __BASE =
    CALENDAR ( __MIN, __MAX )
VAR __RESULT =
    ADDCOLUMNS (
        ADDCOLUMNS (
            __BASE,
            "Year", YEAR ( [Date] ),
            "Month Long", FORMAT ( [Date], "mmmm" ),
            "Month Short", FORMAT ( [Date], "mmm" ),
            "Month Number", MONTH ( [Date] ),
            "Month and Year", FORMAT ( [Date], "mmm-yy" ),
            "YYYYMM", FORMAT ( [Date], "YYYYMM" ),
            "Quarter", "Q" & QUARTER ( [Date] ),
            "Day of Week Long", FORMAT ( [Date], "dddd" ),
            "Day of Week Short", FORMAT ( [Date], "ddd" ),
            "Day Sort", WEEKDAY ( [Date], 1 ),
            "Week of Year", WEEKNUM ( [Date] ),
            "Week Ending Saturday",
                VAR __DAY =
                    WEEKDAY ( [Date] ) - 7
                RETURN
                    [Date] - __DAY,
            "Start of Month", EOMONTH ( [Date], -1 ) + 1,
            "End of Month", EOMONTH ( [Date], 0 )
        ),
        "FY",
            IF ( [Month Number] >= 7, [Year] + 1, [Year] ),
        "Month FY", [Month Short],
        "Month Sort FY",
            IF ( [Month Number] > 6, [Month Number] - 6, [Month Number] + 6 ),
        "Week Num", WEEKNUM([Date]),
        "Day Number", DATEDIFF ( DATE ( YEAR ( [Date] ), 1, 1 ), [Date], DAY ) + 1,
        "Month and Day",  FORMAT([Date], "mm-dd"),
        "Week Ending Sunday",
            [Date]
                + MOD ( 1 - WEEKDAY ( [Date] ) + 7, 7 ),
        "Week Starting Monday", [Date] - WEEKDAY ( [Date], 3 ),
        "Year Rank", RANKX ( DISTINCT( __BASE ), YEAR ( [Date] ), , DESC, DENSE )
    )

RETURN
    __RESULT

 










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

2 REPLIES 2
danextian
Super User
Super User

Hi @ShivGC 

 

Matrix can be sorted only by the measure or row categories. Column categories are either sorted alphabetically or by using a custom column sort. You can rank the year in your dates table to get the reverse sort.

Year Sort = 
RANKX(ALL(Dates[Year]), Dates[Year],,DESC,Dense)

danextian_0-1738232625223.png

As the rank calc column is referencing the year column, year cannot be sorted directly by this column as this will cause circular dependency.

danextian_1-1738232681704.png

Your alternative is to create another calculated column referencing the original year column, custom sort that instead and then use it in your viz.

danextian_2-1738232764208.png

Alternatively and if you are using  DAX calendar, you can the sort column directly in to the table itself and not as a calc column. Here's a sample DAX calendar with a year sort/rank column built-in

Dates = 
VAR __MIN =
   DATE ( 2022, 1, 1 )  
VAR __MAX =
    TODAY()
VAR __BASE =
    CALENDAR ( __MIN, __MAX )
VAR __RESULT =
    ADDCOLUMNS (
        ADDCOLUMNS (
            __BASE,
            "Year", YEAR ( [Date] ),
            "Month Long", FORMAT ( [Date], "mmmm" ),
            "Month Short", FORMAT ( [Date], "mmm" ),
            "Month Number", MONTH ( [Date] ),
            "Month and Year", FORMAT ( [Date], "mmm-yy" ),
            "YYYYMM", FORMAT ( [Date], "YYYYMM" ),
            "Quarter", "Q" & QUARTER ( [Date] ),
            "Day of Week Long", FORMAT ( [Date], "dddd" ),
            "Day of Week Short", FORMAT ( [Date], "ddd" ),
            "Day Sort", WEEKDAY ( [Date], 1 ),
            "Week of Year", WEEKNUM ( [Date] ),
            "Week Ending Saturday",
                VAR __DAY =
                    WEEKDAY ( [Date] ) - 7
                RETURN
                    [Date] - __DAY,
            "Start of Month", EOMONTH ( [Date], -1 ) + 1,
            "End of Month", EOMONTH ( [Date], 0 )
        ),
        "FY",
            IF ( [Month Number] >= 7, [Year] + 1, [Year] ),
        "Month FY", [Month Short],
        "Month Sort FY",
            IF ( [Month Number] > 6, [Month Number] - 6, [Month Number] + 6 ),
        "Week Num", WEEKNUM([Date]),
        "Day Number", DATEDIFF ( DATE ( YEAR ( [Date] ), 1, 1 ), [Date], DAY ) + 1,
        "Month and Day",  FORMAT([Date], "mm-dd"),
        "Week Ending Sunday",
            [Date]
                + MOD ( 1 - WEEKDAY ( [Date] ) + 7, 7 ),
        "Week Starting Monday", [Date] - WEEKDAY ( [Date], 3 ),
        "Year Rank", RANKX ( DISTINCT( __BASE ), YEAR ( [Date] ), , DESC, DENSE )
    )

RETURN
    __RESULT

 










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.