RANKX with Filters

Hello!

Relatively new to Power BI and DAX so I’m needing a little guidance. I’ve attempted to follow a few other forum posts and videos but have had no luck.

I have a set of data that consists of about 600 cities, I want to be able to select any city within that set and then based on the population, compare to the data I have in 200 of those cities.

I currently have a measure that will put a 1 or 0 based on the input from a slicer

Filtered Selection =
VAR SelectedPlan = SELECTEDVALUE( ‘City Names - Disconnected’[Name Only] )
VAR CurrentPlan = SELECTEDVALUE( Demographic[Name Only] )
VAR Active = SELECTEDVALUE( Demographic[Always Include?] )

RETURN
IF( CurrentPlan = SelectedPlan || Active = “Yes” , 1 , 0 )

I am wanting to use RANKX to rank (based on population) only the values that return a 1
(I have an overall ranking that works but is for all 600 cities)
First thought was
Filtered Rank = RANKX( FILTER( Demographic , [Filtered Selection] = 1 ) , [Population] , , DESC , Skip )
but that only returns a value of 1 for every rank, I have also attempted a virtual table that gives me the same result (Selection Table Measure)

My goal after getting those rankings is to show the details of the rankings 5 above and 5 below the selected value.

Population Compare - WC.pbix (91.9 KB)

I am no Guru
but with the RANK you need something like

[Rank Population] =

IF ( HASONEVALUE( ‘City’[City Name]),

RANKX(ALL(‘City’[City Name]), [Population])
)

Thats assumoing you are ranking by City Name. I made it ALL so that it RANKS ALL. Just replace that with the filtered version but make sure you get ALL of the filtered data

Hope that points you in the right direction

I have an overall rank that is working using

Overall Rank =
RANKX( ALL( Demographic ) , [Population] , , DESC , Skip )

this returns the rank of all cities

I can get the desired rank of what I’m looking for using

Filtered Rank =
RANKX( ALLSELECTED( Demographic ) , [Population] , , DESC , Skip )

Using a filter of [Filtered Selection] = 1

My problem is I want to add another filter (the 5 above and 5 below) using the rankings returned in the ALLSELECTED() measure above. once I start to work on that problem the rankings change when I apply new filters since I’m using the ALLSELECTED() function.

Actually I think I have it figured out using the following

Filtered Rank =
RANKX(
FILTER( ALL(Demographic) , [Filtered Selection] = 1 ) ,
[Population] , , DESC , Skip )

and then for the 5 high and 5 below

5 High 5 Low Display =
VAR SelectedPlan = SELECTEDVALUE( ‘City Names - Disconnected’[Name Only] )
VAR SelectedRank = CALCULATE( [Filtered Rank] , ALL( Demographic ) , FILTER( ALL( Demographic ) , Demographic[Name Only] = SelectedPlan ) )
VAR NormalRank = [Filtered Rank]

RETURN
IF( NormalRank >= SelectedRank - 5 && NormalRank <= SelectedRank +5,1,0)

then applying on my visuals a filter that is 5 high 5 low display = 1 & Filtered Selection = 1