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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
rpinxt
Solution Sage
Solution Sage

Sorting on columns in a matrix

Pic will make it clear :

rpinxt_1-1736425943130.png

 

What I wanted is Year-week in a descending order.

At leat putting Year-Month above is will get me the descending part because YM is sorted by YM- :

Dim_Date =
ADDCOLUMNS(
    CALENDARAUTO(),
    "YM", FORMAT([Date], "YYYY-MM"),
    "YM-", FORMAT([Date], "YYYY-MM") * -1

)

 

But for the weeks it is not fully working (but now at least 2025 comes first because of YM)

 

multiply by -1 is not an option I think  because I use this to get the week numbers:

"Year - Week (ISO)", YEAR([Date]) &"-"& WEEKNUM([Date], 21),
 
Anybody an idea how I can get the year - weeks to me descending?

 

1 ACCEPTED SOLUTION
danextian
Super User
Super User

Hi @rpinxt 

 

The matrix visual does not offer an option to sort columns in descending or ascending order directly. Columns are sorted either alphabetically or based on custom sorting in ascending order. To achieve your desired sorting, you will need to create a custom sort column. Please refer to the example below:

DatesTable = 
VAR _base =
    ADDCOLUMNS (
        CALENDAR ( DATE ( 2024, 1, 1 ), TODAY () ),
        "YYYY-MM", FORMAT ( [Date], "YYYY-MM" ),
        "Year Week",
            YEAR ( [Date] ) & "-"
                & FORMAT ( WEEKNUM ( [Date] ), "00" )
    )
RETURN
    ADDCOLUMNS (
        _base,
        "Year-Month Sort", RANKX ( _base, [YYYY-MM],, DESC, DENSE ),
        "Year-Week Sort", RANKX ( _base, [Year Week],, DESC, DENSE )
    )

danextian_0-1736428050301.png

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

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


"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

5 REPLIES 5
rpinxt
Solution Sage
Solution Sage

@danextian ah took only a tiny adjustment to make it all work as expected 😄

rpinxt_1-1736431537668.png

 

Now all is perfect. Thanks!

rpinxt_2-1736431575722.png

 

 

rpinxt
Solution Sage
Solution Sage

I think I understand a bit @danextian 

My original Dim_Date table was connect to a table on rundate then from that connection it makes:

Dim_Date =
ADDCOLUMNS(
    CALENDARAUTO(),
    "Year", YEAR([Date]),
    "Quarter", FORMAT([Date],"\QQ"),
    "Quarter Nr", QUARTER([Date]),
    "Month", FORMAT([Date], "mmmm" ),
    "Mth", FORMAT([Date], "mmm" ),
    "Month Number", MONTH([Date]),
    "Week Number", WEEKNUM([Date]),
    "Week Number Txt", "Week " & WEEKNUM([Date]),
    "Week Nr (ISO)", WEEKNUM([Date], 21),
    "Week Nr (ISO) Desc", WEEKNUM([Date], 21),
    "Year - Week (ISO)", YEAR([Date]) &"-"& WEEKNUM([Date], 21),
    "Year - Week (ISO)2", FORMAT([Date], "-YYYY-WW"),
    "ISO Week Desc", WEEKNUM([Date], 21) * -1,
    "Week Day", FORMAT([Date], "DDD"),
    "Week Day Nr", WEEKDAY([Date], 2),
    "YM", FORMAT([Date], "YYYY-MM"),
    "YM-", FORMAT([Date], "YYYY-MM") * -1,
    "Period", FORMAT([Date], "MM-YYYY")
)
 
Now for the matrix I made a new date table names Dim_Date_Matrix and also connected to the Rundate.
This looks like :
Dim_Date_Matrix =
VAR _base =
    ADDCOLUMNS (
        CALENDAR ( DATE ( 2021, 1, 1 ), TODAY () ),
        "YYYY-MM", FORMAT ( [Date], "YYYY-MM" ),
        "Year Week",
            YEAR ( [Date] ) & "-"
                & FORMAT ( WEEKNUM ( [Date] ), "00" )
    )
RETURN
    ADDCOLUMNS (
        _base,
        "Year-Month Sort", RANKX ( _base, [YYYY-MM],, DESC, DENSE ),
        "Year-Week Sort", RANKX ( _base, [Year Week],, DESC, DENSE )
    )
 
 
Still don't fully understand but the Today() probably makes for suddenly week 2 is showing up.
The max for rundate at this time is 5th of Jan.
So in the original Dim_Date that leeds to Week 1.

CALENDARAUTO relies on the existing dates in your model. I don't know what dates you have there.  So when you use TODAY() in CALENDAR(), you're seeing week 2 since we already are in the second week of January.  Instead of today, use MAX(data[rundate])





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

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


"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.
rpinxt
Solution Sage
Solution Sage

Thanks @danextian 

Looks to be sorting but 2 major things:

rpinxt_0-1736429000539.png

Its january so it should be 2025-01 and not 02??

And were is suddenly week 50 and 47?? (and I have week 53 now)

danextian
Super User
Super User

Hi @rpinxt 

 

The matrix visual does not offer an option to sort columns in descending or ascending order directly. Columns are sorted either alphabetically or based on custom sorting in ascending order. To achieve your desired sorting, you will need to create a custom sort column. Please refer to the example below:

DatesTable = 
VAR _base =
    ADDCOLUMNS (
        CALENDAR ( DATE ( 2024, 1, 1 ), TODAY () ),
        "YYYY-MM", FORMAT ( [Date], "YYYY-MM" ),
        "Year Week",
            YEAR ( [Date] ) & "-"
                & FORMAT ( WEEKNUM ( [Date] ), "00" )
    )
RETURN
    ADDCOLUMNS (
        _base,
        "Year-Month Sort", RANKX ( _base, [YYYY-MM],, DESC, DENSE ),
        "Year-Week Sort", RANKX ( _base, [Year Week],, DESC, DENSE )
    )

danextian_0-1736428050301.png

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

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


"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
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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