Forum Discussion
RANKX Calculated Column on a Related Tables
- 2 years ago
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!
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!
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.