Forum Discussion

CSSR's avatar
CSSR
Frequent Visitor
5 years ago
Solved

RANKX understanding

Hi folks,

 

I want to know why when I use (1.) code/procedure for Ranking it does not work and when I use (2.) code/procedure it's working well. What's the reason/logic behind it?
(1.)

Rank Requestors (not working1) =
RANKX(ALL(DB_01[Requestor full name]),COUNTX(DB_01, DB_01[num])

Rank Requestors (not working2) =
RANKX(ALL(DB_01[Requestor full name]),COUNT(DB_01[num])


(2.) 
count of requests=COUNT(DB_01[num])
Rank Requestors (working)=RANKX(ALL(DB_01[Requestor full name]),[count of requests])

Rank Requestors (working1)=

RANKX(
ALL(DB_01[Requestor full name]),
CALCULATE(
COUNT(DB_01[num]),
ALLEXCEPT(DB_01,DB_01[Requestor full name])
            )
,
,DESC)

Thanks in advance for your comments ! I simply don't get why I need to create a measure to count requests and then use it in RANKX function instead of using COUNT function directly in it.

  • hi CSSR 

    The other common pitfall is using a DAX formula to aggregate rows without wrapping the expression in a CALCULATE function. In the previous examples, we always used the measure Sales Amount as the expression to use in the ranking. If you use an aggregation function such as SUMX, you should consider that the expression is evaluated for each row of the table passed as a first argument to RANKX. The row context defined in this iteration is not transformed into a filter context unless a context transition is invoked by CALCULATE, which is an implicit operation when you evaluate a measure. Thus, for each row the filter context is always the same (that is, the existing filter in the cell where RANKX is evaluated), and all the items have the same rank of 1 in this way. The correct formula simply wraps the expression in a CALCULATE function that performs the context transition for each row of the table iterated by RANKX.

     

    Regards,

    Lin

2 Replies