Forum Discussion

Ania26's avatar
Ania26
Helper IV
1 year ago
Solved

Sorting by the last column

Hello,

 

CountryValueYear

USA12023
France32023
Canada12023
USA22024
France12024
Canada62024

 

For the data above , I have amtrix table:

How can I sort by the last column? 2024. descending? 

  • Hey there!

     

    You could try creating a Sort Column in Power Query

    • Since the matrix does not allow direct sorting by specific columns, create a calculated column:
    • In Power Query, add a new column:
      SortColumn = 'YourTable'[2024] // Ensure this refers to the 2024 column
    • Sort the Matrix Table Using This Column

      • Go to the Fields pane in Power BI.
      • Click on the Country column.
      • Select Sort by Column β†’ Choose SortColumn.
    • Ensure Sorting Order

      • If the sort is incorrect, adjust sorting in Visual Level Filters:
        • Click on the matrix table.
        • Under Sorting & Filtering options, select 2024 column β†’ Sort Descending.

    If sorting directly is not working, create a DAX measure to rank countries based on the 2024 values:

    Rank2024 = RANKX(ALL('YourTable'), SUM('YourTable'[2024]), , DESC, DENSE)

    Then, sort the matrix by Rank2024 instead.

     

    Hope this helps!

    😁😁

     

6 Replies

  • freginier's avatar
    freginier
    Solution Sage

    Hey there!

     

    You could try creating a Sort Column in Power Query

    • Since the matrix does not allow direct sorting by specific columns, create a calculated column:
    • In Power Query, add a new column:
      SortColumn = 'YourTable'[2024] // Ensure this refers to the 2024 column
    • Sort the Matrix Table Using This Column

      • Go to the Fields pane in Power BI.
      • Click on the Country column.
      • Select Sort by Column β†’ Choose SortColumn.
    • Ensure Sorting Order

      • If the sort is incorrect, adjust sorting in Visual Level Filters:
        • Click on the matrix table.
        • Under Sorting & Filtering options, select 2024 column β†’ Sort Descending.

    If sorting directly is not working, create a DAX measure to rank countries based on the 2024 values:

    Rank2024 = RANKX(ALL('YourTable'), SUM('YourTable'[2024]), , DESC, DENSE)

    Then, sort the matrix by Rank2024 instead.

     

    Hope this helps!

    😁😁

     

  • Hello, Thank you for this but this will not work when someone will filter other than 2024 year. 

  • v-echaithra's avatar
    v-echaithra
    Community Support

    Hi Ania26 ,

    Thank you for reaching out to Microsoft Community.

    Try to make sorting dynamic, based on the latest visible year in the matrix. So you can dynamically pick the latest visible year (e.g., 2024 now, maybe 2025 later), sort countries by their value in that latest year.

     

    Create a measure to identify the latest visible year:

    LatestVisibleYear =

    MAX('Table'[Year])

     

    and now create a measure to return value for the latest visible year to calculate the sum of values only for the latest visible year, dynamically.

     

    ValueforlatestYear =

    VAR LatestYear = CALCULATE(MAX('Table'[Year]), ALLSELECTED('Table'))

    RETURN

    CALCULATE(

    SUM('Table'[Value]),

    'Table'[Year] = LatestYear

    )

     

    Use this measure to sort your matrix, sort the matrix by ValueforlatestYear descending.

    If this issue has been resolved, consider accepting helpful replies as solutions.

    Regards,
    Chaithra E.


  • v-echaithra's avatar
    v-echaithra
    Community Support

    Hi Ania26 ,

    We wanted to kindly follow up to check if the solution provided for the issue worked? or Let us know if you need any further assistance?
    If our response addressed, please mark it as Accept as solution and click Yes if you found it helpful.

     

    Regards,
    Chaithra.

  • v-echaithra's avatar
    v-echaithra
    Community Support

    Hi Ania26 ,

    As we haven’t heard back from you, we wanted to kindly follow up to check if the solution provided for the issue worked? or Let us know if you need any further assistance?
    If our response addressed, please mark it as Accept as solution and click Yes if you found it helpful.

     

    Regards,
    Chaithra E

  • v-echaithra's avatar
    v-echaithra
    Community Support

    Hi Ania26 ,

    We wanted to kindly follow up to check if the solution provided for the issue worked? or Let us know if you need any further assistance?
    If our response addressed, please mark it as Accept as solution and click Yes if you found it helpful.

     

    Regards,
    Chaithra.