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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
ns29
Helper I
Helper I

Sort date field in Matrix column header descending

I have already seen the following:

 

https://www.excelguru.ca/blog/2018/06/14/ranking-method-choices-in-power-query/

https://stackoverflow.com/questions/49685042/powerbi-sort-columns-in-matrix-visual

 

I am having a hard time understanding the solution. Honestly, I'm just looking for a pivot table kind of solution. Please attached screenshot. 

 

Thanks!

 

sample1.PNGsample2.PNG

1 ACCEPTED SOLUTION

Hi @ns29 ,

 

We can use the following steps to meet your requirement:

 

1. create a calculated table using following DAX:

 

ColumnHeader = 
ADDCOLUMNS (
    CROSSJOIN (
        DATATABLE (
            "ValueHeader", STRING,"ValueRank",INTEGER,
            {
                { "%_of_sample",2 },
                { "metric_value_1" ,1},
                { "metric_value_2",3 }
            }
        ),
        DISTINCT ( 'Table1'[metric_month] )
    ),
    "Rank", RANKX (
        DISTINCT ( 'Table1'[metric_month] ),
        [metric_month],
        ,
        DESC,
        DENSE
    )
)

 

2. Make metric_month column sort by Rank column, then make ValueHeader sort by Rank column.

1.jpg

2.jpg

 

3. create relationship between two tables based on the ColumnHeader column

3.jpg

 

4. create a measure used in the value field of matrix visual:

 

ValueInMatrix = SWITCH(SELECTEDVALUE('ColumnHeader'[ValueHeader]),"metric_value_1",SUM('Table1'[metric_value_1])&"","metric_value_2",SUM('Table1'[metric_value_2])&"","%_of_sample",FORMAT([%_of_sample],"Percent"))

 

4.jpg

 


Best regards,

 

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

8 REPLIES 8
v-lid-msft
Community Support
Community Support

Hi @ns29 ,

 

We can create two calculated column, then use ShowDate Column to sort by rank column and use ShowDate Column as column header to meet your requirement:

 

Calculated Columns:

ShowDate = [Date]
Rank = RANKX('Table',[Date],,,Dense)

 

Sort by:

5.jpg

 

Matrix visual:6.jpg

 


Best regards,

 

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@v-lid-msft thanks for your response. Seems like the proposed solution is similar to other solutions I've seen. Baffles me that something so apparently simple requires such a complicated workaround. But there are probably good reasons why the product is designed this way which my simple mind may not be able to comprehend. I'll give it a shot and report back.

 

PS: I get the Rank field but what is the point of the ShowDate field since it appears to be identical to the Date field. I'm trying to understand the mechanics rather than blindly apply the solution. Thanks again!

amitchandak
Super User
Super User

Can you share sample data and sample output. If possible please share a sample pbix file after removing sensitive information.Thanks.
Proud to be a Datanaut My Recent Blog -
https://community.powerbi.com/t5/Community-Blog/HR-Analytics-Active-Employee-Hire-and-Termination-tr...

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

@amitchandak  Thank you for the quick response! Happy to send over samples but have a super rookie question - how do I attach anything other than images on here? So embarrased to ask...

You can upload to dropbox, onedrive and share

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

@amitchandak thanks, see link below for samples.

 

I have uploaded both an Excel file and a pbix file. Basically, the Excel has a pivot table that I'm trying to mimic in Power BI. Here are the things I'm trying to do and not finding a solution or at least a simple way akin to Excel pivot tables - the first is about the original post but hoping to get a few more answers while I'm at it. 

  • Sort date type column header in descending order
  • I have 3 different values in the pivot table, I'd like to see them values first, date next in the pivot table. Meaning, I want to see all the numbers for my first value with corresponding dates, then the next value and so on. In the pivot table, it's a simple drag and drop, I have been totally unable to do that in Power BI. It forces me to see all 3 values for the first date, then the next date and so on.
  • Please see measure %_of_sample in the pbix. In the pivot table, it's a simple step of adding the value a second time to the pivot and right clicking and choosing % of etc. I researched it and came up with this measure but was wondering if there was a simpler solution. 

That's all I can think of for the moment, thank you!

 

https://drive.google.com/drive/folders/1VY2yV7MKE-DHRpQcgl9e2U9Xd4YbYTxV?usp=sharing

Hi @ns29 ,

 

We can use the following steps to meet your requirement:

 

1. create a calculated table using following DAX:

 

ColumnHeader = 
ADDCOLUMNS (
    CROSSJOIN (
        DATATABLE (
            "ValueHeader", STRING,"ValueRank",INTEGER,
            {
                { "%_of_sample",2 },
                { "metric_value_1" ,1},
                { "metric_value_2",3 }
            }
        ),
        DISTINCT ( 'Table1'[metric_month] )
    ),
    "Rank", RANKX (
        DISTINCT ( 'Table1'[metric_month] ),
        [metric_month],
        ,
        DESC,
        DENSE
    )
)

 

2. Make metric_month column sort by Rank column, then make ValueHeader sort by Rank column.

1.jpg

2.jpg

 

3. create relationship between two tables based on the ColumnHeader column

3.jpg

 

4. create a measure used in the value field of matrix visual:

 

ValueInMatrix = SWITCH(SELECTEDVALUE('ColumnHeader'[ValueHeader]),"metric_value_1",SUM('Table1'[metric_value_1])&"","metric_value_2",SUM('Table1'[metric_value_2])&"","%_of_sample",FORMAT([%_of_sample],"Percent"))

 

4.jpg

 


Best regards,

 

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@v-lid-msft Thank you for the detailed response and the effort you put into providing the sample output. I'll play with it but it does seem like a lot of hoops to jump through to get to some seemingly simple results. I'll play with this and also evaluate against Tableau. But, definitely marking this as solved.

 

Thank you again!

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.

Top Solution Authors