Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.
I have a Matrix with 2 column headers:
Country and
Year
In values I have 3 rows:
Sum of Sales (Ths.),
Year on Year % Change (+/- % PY) and
Country Share % (Horiz. %)
I am trying to create a Rank of Country by the Measure, so that I can sort my Country Headers in order of Country Share Greatest to Least, Like this.
I have tried
Solved! Go to Solution.
Hi,
The issue where your RANKX measure returns 1 for every country is likely due to the context in which your Horiz. % measure is being evaluated within the matrix. Power BI can sometimes struggle to maintain the correct context when dealing with multi-level columns or matrices.
Here are the steps to correct this:
When using a Matrix with multi-level column headers, you need to ensure that the ranking measure is calculating in the correct context of the country, ignoring the year but respecting any filters on the matrix.
Modify your Rank measure like this:
DAX
Copy code
Rank =
RANKX (
ALLSELECTED('Table'[Country]), // Ensure that the ranking applies to all selected countries
CALCULATE([Horiz. %]), // Calculate the measure in the correct context
, // Optional: if you want to include a tie-breaking expression, add it here
DESC, // Sort in descending order
DENSE // Dense ranking, meaning no gaps in rank numbers
)
If you're ranking based on a specific year's Horiz. %, you may need to adjust your rank measure to focus on the selected year:
DAX
Copy code
Rank =
RANKX (
ALLSELECTED('Table'[Country]),
CALCULATE([Horiz. %], 'Table'[Year] = SELECTEDVALUE('Table'[Year])),
,
DESC,
DENSE
)
This ensures the ranking is based on the selected year's Horiz. %, not the overall total.
Once the rank measure is correctly calculating, apply sorting by Rank in the matrix by doing the following:
If you want to rank countries only for a specific year (e.g., 2024), consider adding a filter or slicer for the year in the Rank calculation. You can add a slicer for Year in your Power BI report or modify the Rank formula further:
DAXCopy codeRank = RANKX( ALL('Table'[Country]), CALCULATE([Horiz. %], 'Table'[Year] = SELECTEDVALUE('Table'[Year])), , DESC, DENSE)
This should fix the issue, and the countries will be sorted by their Horiz. % share in descending order. If you are still seeing 1 for all ranks, double-check the context of your Horiz. % measure and ensure it's calculating correctly for each country.
Hope this helps.
Hi @SamInogic
After completely niss seeing what you so cometantly explained, it suddenly dawned on me what you were doing.
The Rank measure is required in the Matrix and then sort Country by the Row Rank measure
yes I see that works now and I can hide this row in a variaty of ways.
many thanks, I learnt a lot.
Hi,
The issue where your RANKX measure returns 1 for every country is likely due to the context in which your Horiz. % measure is being evaluated within the matrix. Power BI can sometimes struggle to maintain the correct context when dealing with multi-level columns or matrices.
Here are the steps to correct this:
When using a Matrix with multi-level column headers, you need to ensure that the ranking measure is calculating in the correct context of the country, ignoring the year but respecting any filters on the matrix.
Modify your Rank measure like this:
DAX
Copy code
Rank =
RANKX (
ALLSELECTED('Table'[Country]), // Ensure that the ranking applies to all selected countries
CALCULATE([Horiz. %]), // Calculate the measure in the correct context
, // Optional: if you want to include a tie-breaking expression, add it here
DESC, // Sort in descending order
DENSE // Dense ranking, meaning no gaps in rank numbers
)
If you're ranking based on a specific year's Horiz. %, you may need to adjust your rank measure to focus on the selected year:
DAX
Copy code
Rank =
RANKX (
ALLSELECTED('Table'[Country]),
CALCULATE([Horiz. %], 'Table'[Year] = SELECTEDVALUE('Table'[Year])),
,
DESC,
DENSE
)
This ensures the ranking is based on the selected year's Horiz. %, not the overall total.
Once the rank measure is correctly calculating, apply sorting by Rank in the matrix by doing the following:
If you want to rank countries only for a specific year (e.g., 2024), consider adding a filter or slicer for the year in the Rank calculation. You can add a slicer for Year in your Power BI report or modify the Rank formula further:
DAXCopy codeRank = RANKX( ALL('Table'[Country]), CALCULATE([Horiz. %], 'Table'[Year] = SELECTEDVALUE('Table'[Year])), , DESC, DENSE)
This should fix the issue, and the countries will be sorted by their Horiz. % share in descending order. If you are still seeing 1 for all ranks, double-check the context of your Horiz. % measure and ensure it's calculating correctly for each country.
Hope this helps.