Forum Discussion
RANKX Calculated Column on a Related Tables
Sorry to have had to raise this request but I have looked far and wide for a resolution and tried perhaps 20 different DAX formulas for RANKX without the result I am looking for. I have used samples on RADCAD posts that just apply a 1 to all values, and have used samples on other requests in the community that havent provided a result or errored.
The best way I can illustrate the problem/request is using a sports model.
I have two tables, a Players Table and a Games table (Sample Data below)
The join fields between the two tables is on a concatenation of Player, Year and Team.
I want to add...
1. A calculated column on the Players table that ranks an all time score rank across all players and all teams
2. A calculated column on the Players table that ranks where the player come in scoring for any particular Year
3. A calculated column on the Players table thats ranks where the player come in scoring for the year within their Team
For 1. I have a calculated column not working that that is a rank of where the player ranked for total scoring in a single Year.
| Player | Team | Year |
| John Smith | Philli | 2022 |
| Julian Knight | Philli | 2022 |
| John Smith | New York | 2023 |
| Fred Krane | LA Lakers | 2022 |
| Andre Sport | Heat | 2022 |
| Xavier Fort | LA Lakers | 2022 |
| Fred Krane | Celtics | 2023 |
| Andre Sport | Heat | 2023 |
| Xavier Fort | Celtics | 2023 |
Games
| Player | Team | Year | Vs | Points |
| John Smith | Philli | 2022 | New York | 14 |
| John Smith | Philli | 2022 | LA Lakers | 15 |
| John Smith | Philli | 2022 | Heat | 23 |
| John Smith | Philli | 2022 | Celtics | 45 |
| Julian Knight | Philli | 2022 | New York | 2 |
| Julian Knight | Philli | 2022 | LA Lakers | 4 |
| Julian Knight | Philli | 2022 | Heat | 7 |
| Julian Knight | Philli | 2022 | Celtics | 8 |
| John Smith | New York | 2022 | Philli | 21 |
| John Smith | New York | 2022 | LA Lakers | 2 |
| John Smith | New York | 2022 | Heat | 7 |
| John Smith | New York | 2022 | Celtics | 9 |
| Fred Krane | LA Lakers | 2022 | New York | 12 |
| Fred Krane | LA Lakers | 2022 | Philli | 15 |
| Fred Krane | LA Lakers | 2022 | Heat | 11 |
| Fred Krane | LA Lakers | 2022 | Celtics | 10 |
| Andre Sport | Heat | 2022 | New York | 17 |
| Andre Sport | Heat | 2022 | Philli | 21 |
| Xavier Fort | LA Lakers | 2022 | New York | 25 |
| Xavier Fort | LA Lakers | 2022 | Philli | 13 |
| Xavier Fort | LA Lakers | 2022 | Heat | 11 |
| Xavier Fort | LA Lakers | 2022 | Celtics | 18 |
| Xavier Fort | LA Lakers | 2022 | Bulls | 14 |
| Fred Krane | Celtics | 2023 | New York | 12 |
| Fred Krane | Celtics | 2023 | Philli | 16 |
| Fred Krane | Celtics | 2023 | Heat | 18 |
| Andre Sport | Heat | 2023 | New York | 16 |
| Andre Sport | Heat | 2023 | Philli | 22 |
| Andre Sport | Heat | 2023 | Celtics | 13 |
| Andre Sport | Heat | 2023 | Bulls | 4 |
| Xavier Fort | Celtics | 2023 | Philli | 7 |
| Xavier Fort | Celtics | 2023 | Heat | 9 |
| Xavier Fort | Celtics | 2023 | New York | 32 |
| Xavier Fort | Celtics | 2023 | Bulls | 12 |
Hi,
We are a bit unclear about your requirement; if you can provide us the expected result, it will help us better to create DAX expressions.
However, as per our understanding, you want ranking based on players' scores across different criteria. Here’s how you can achieve this using DAX in:
Step 1: Create uniqueKey Keys
First, create a uniqueKey key in both the Players and Games tables to help with the relationships.
uniqueKey= [Player] & "-" & [Year] & "-" & [Team]
Step 2: Create Relationships
- Go to the Model View in Power BI.
- Create a relationship between the uniqueKey column in the Players table and the uniqueKey column in the Games table.
Step 3: Create Measures and Calculated Columns
Total Points Measure
Create a measure to calculate the total points for each player:
TotalPoints = SUM(Games[Points])
Player Total Points
Create a calculated column in the Players table to calculate the total points for each player:
PlayerTotalPoints = CALCULATE( [TotalPoints], FILTER( Games, Games[uniqueKey] = Players[uniqueKey] ))
All-Time Rank
Create a calculated column in the Players table for the all-time score rank:
AllTimeRank = RANKX( ALL(Players), [PlayerTotalPoints], , DESC, DENSE)
Rank Within Year
Create a calculated column in the Players table for the rank within a specific year:
RankWithinYear = RANKX( FILTER( ALL(Players), Players[Year] = EARLIER(Players[Year]) ), [PlayerTotalPoints], , DESC, DENSE)
Rank Within Year and Team
Create a calculated column in the Players table for the rank within a specific year and team:
RankWithinYearAndTeam = RANKX( FILTER( ALL(Players), Players[Year] = EARLIER(Players[Year]) && Players[Team] = EARLIER(Players[Team]) ), [PlayerTotalPoints], , DESC, DENSE)
Please refer to the screenshot below for the results,
Hope this helps.
Thanks!
2 Replies
- SamInogic
Super User
Hi,
We are a bit unclear about your requirement; if you can provide us the expected result, it will help us better to create DAX expressions.
However, as per our understanding, you want ranking based on players' scores across different criteria. Here’s how you can achieve this using DAX in:
Step 1: Create uniqueKey Keys
First, create a uniqueKey key in both the Players and Games tables to help with the relationships.
uniqueKey= [Player] & "-" & [Year] & "-" & [Team]
Step 2: Create Relationships
- Go to the Model View in Power BI.
- Create a relationship between the uniqueKey column in the Players table and the uniqueKey column in the Games table.
Step 3: Create Measures and Calculated Columns
Total Points Measure
Create a measure to calculate the total points for each player:
TotalPoints = SUM(Games[Points])
Player Total Points
Create a calculated column in the Players table to calculate the total points for each player:
PlayerTotalPoints = CALCULATE( [TotalPoints], FILTER( Games, Games[uniqueKey] = Players[uniqueKey] ))
All-Time Rank
Create a calculated column in the Players table for the all-time score rank:
AllTimeRank = RANKX( ALL(Players), [PlayerTotalPoints], , DESC, DENSE)
Rank Within Year
Create a calculated column in the Players table for the rank within a specific year:
RankWithinYear = RANKX( FILTER( ALL(Players), Players[Year] = EARLIER(Players[Year]) ), [PlayerTotalPoints], , DESC, DENSE)
Rank Within Year and Team
Create a calculated column in the Players table for the rank within a specific year and team:
RankWithinYearAndTeam = RANKX( FILTER( ALL(Players), Players[Year] = EARLIER(Players[Year]) && Players[Team] = EARLIER(Players[Team]) ), [PlayerTotalPoints], , DESC, DENSE)
Please refer to the screenshot below for the results,
Hope this helps.
Thanks!
- shaunwilks
Helper V
Whats an amazing response.
Thankyou very much and have this working now.
It would appear adding a column to the Players table in this instance that was the sumof points makes it easier to then base your rankings off the master table rather than keep going back to sum up the fact table.
I will need to look more into you use of the "EARLIER" function in the filter as I hadnt seen that used so much in this sort of way. I dont understand it yet but will edcuate myself knowing that it did the job and works.