Forum Discussion
Power BI RANKX Issue Summary - Get an average rank for decimal numbers
I am trying to calculate an average rank based on Views_Share_Avg, while considering both Channel and Daypart and excluding the selected date.
However, I am running into issues where:
- RANKX does not produce expected ranks (e.g., 1, 2, 3).
- Some ranks are duplicated (e.g., two "2"s).
Rank2 is a column created like this:Rank2 =VAR CurrentDaypart = MAX('techedge Dayparts for Top Ranked Days'[Daypart])VAR CurrentDate = MAX('techedge Dayparts for Top Ranked Days'[Date])VAR CurrentChannel = MAX('techedge Dayparts for Top Ranked Days'[Channel])RETURNRANKX(FILTER(ALLSELECTED('techedge Dayparts for Top Ranked Days'),'techedge Dayparts for Top Ranked Days'[Daypart] = CurrentDaypart &&'techedge Dayparts for Top Ranked Days'[Date] = CurrentDate &&'techedge Dayparts for Top Ranked Days'[Channel] = CurrentChannel),'techedge Dayparts for Top Ranked Days'[Views],,DESC,DENSE)I have a switchable measure:Views_Share_Avg =VAR AvgValue = AVERAGEX('techedge Dayparts for Top Ranked Days',[Views_Share])RETURNSWITCH(SELECTEDVALUE('Switch Table - Views/Share'[MetricSwitchName]), -- The slicer selection"Views", FORMAT(AvgValue, "#,##0"), -- Format as thousands"Share", FORMAT(AvgValue, "0.00%"), -- Format as percentageBLANK() -- Default case if nothing is selected)an average rank:Avg_Rank2 = AVERAGE('techedge Dayparts for Top Ranked Days'[Rank2])and a final rank and reads the average rank:Final_Rank =RANKX(SUMMARIZE(ALLSELECTED('techedge Dayparts for Top Ranked Days'),'techedge Dayparts for Top Ranked Days'[Channel],'techedge Dayparts for Top Ranked Days'[Daypart],'techedge Dayparts for Top Ranked Days'[Date],"@AvgRank", [Avg_Rank2]),[Avg_Rank2],,DESC,DENSE)
This is the result I get:
1) The ranks are reversed, so the lowest avg_rank2 should be final_rank 1.
Expected outcome below:
| Channel | Daypart | Views_Share_Avg | Avg_Rank2 | Final_Rank |
| BBC News | Breakfast | 44,457 | 2.00 | 3 |
| GB News | Breakfast | 85,533 | 1.76 | 1 |
| Sky News | Breakfast | 68,033 | 1.99 | 2 |
Here is a sample of the underlying table:
DateChannelDaypartRankViewsShareRank2
|
Any help would be most appreciated.
Thanks
RJ
Hello RedJedi_81,
Thank you for reaching out to the Microsoft Fabric Forum Community.
I’ve reproduced your scenario in Power BI using the sample data, and I achieved the expected output as per your requirements.
Output:For your reference, I’ve attached the .pbix file below so you can review or modify it as needed.
If this information is helpful, please “Accept as solution” and give a "kudos" to assist other community members in resolving similar issues more efficiently.
Thank you.
7 Replies
- bhanu_gautam
Super User
RedJedi_81 Try using
Ensure that the Rank2 calculation excludes the selected date and ranks the views correctly.
Rank2 =
VAR CurrentDaypart = MAX('techedge Dayparts for Top Ranked Days'[Daypart])
VAR CurrentChannel = MAX('techedge Dayparts for Top Ranked Days'[Channel])
RETURN
RANKX(
FILTER(
ALL('techedge Dayparts for Top Ranked Days'),
'techedge Dayparts for Top Ranked Days'[Daypart] = CurrentDaypart &&
'techedge Dayparts for Top Ranked Days'[Channel] = CurrentChannel
),
'techedge Dayparts for Top Ranked Days'[Views],
,
DESC,
DENSE
)Calculate the average rank for each combination of channel and daypart.
DAX
Avg_Rank2 =
AVERAGEX(
SUMMARIZE(
'techedge Dayparts for Top Ranked Days',
'techedge Dayparts for Top Ranked Days'[Channel],
'techedge Dayparts for Top Ranked Days'[Daypart],
"@Rank2", [Rank2]
),
[@Rank2]
)Rank the average ranks in ascending order.
DAX
Final_Rank =
RANKX(
SUMMARIZE(
ALLSELECTED('techedge Dayparts for Top Ranked Days'),
'techedge Dayparts for Top Ranked Days'[Channel],
'techedge Dayparts for Top Ranked Days'[Daypart],
"@AvgRank", [Avg_Rank2]
),
[@AvgRank],
,
ASC,
DENSE
) - Deku
Super User
Not sure I fully understand the average part. Check if this does what you want.
The way RANKX works is that you define a table, the expression is calculated per row and a ranking is performed. In the below we are calculating the number of views, per Day Part and Channel and produces the rank. This means the Channel-Daypart combo with the highest views will be ranked as 1
Rank = RANKX( ALLSELECTED( 'techedge Dayparts for Top Ranked Days'[Daypart], 'techedge Dayparts for Top Ranked Days'[Channel] ) CALCULATE( SUM( 'techedge Dayparts for Top Ranked Days'[Views] ), REMOVEFILTER( 'techedge Dayparts for Top Ranked Days'[Date] ), , DESC, DENSE ) - RedJedi_81Regular Visitor
Thank you for the quick reply.
I forgot to add Rank2 is a column. Does this need to be a measure?
- v-ssriganesh
Community Support
Hello RedJedi_81,
Thank you for reaching out to the Microsoft Fabric Forum Community.
I’ve reproduced your scenario in Power BI using the sample data, and I achieved the expected output as per your requirements.
Output:For your reference, I’ve attached the .pbix file below so you can review or modify it as needed.
If this information is helpful, please “Accept as solution” and give a "kudos" to assist other community members in resolving similar issues more efficiently.
Thank you.- v-ssriganesh
Community Support
Hi RedJedi_81,
I hope this information is helpful. Please let me know if you have any further questions or if you'd like to discuss this further. If this answers your question, please accept it as a solution and give it a 'Kudos' so other community members with similar problems can find a solution faster.
Thank you.
- v-ssriganesh
Community Support
Hi RedJedi_81,
May I ask if you have resolved this issue? If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.
Thank you.
- v-ssriganesh
Community Support
Hi RedJedi_81,
I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.
Thank you.