Scenario:
Suppose I have a sales table including different categories with hierarchy, now I want to rank the sales and profits based on each hierarchy, how will I achieve this requirement?
This Blog is based on Function RANKX.
Table used:
Shop B and Shop C in the sample table are similar with Shop A, so I won’t list them all here.
Guide Line:
Detailed Steps:
Rank Class =
RANKX (
ALLSELECTED ( 'Table'[Class] ),
CALCULATE ( SUM ( 'Table'[CategorySales] ) ),
,
DESC,
DENSE
)
Card Display Class =
CALCULATE (
MAX ( 'Table'[Class] ),
FILTER ( ALLSELECTED ( 'Table'[Class] ), [Rank Class] = 1 )
)
Rank Product =
VAR a =
CALCULATETABLE (
FILTER ( DISTINCT ( 'Table'[Class] ), [Rank Class] = 1 ),
ALLSELECTED ()
)
RETURN
IF (
CALCULATE ( COUNTROWS ( 'Table' ), 'Table'[Class] IN a ) = 0,
BLANK (),
RANKX (
GROUPBY (
FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Class] IN a ),
'Table'[Product]
),
CALCULATE ( SUM ( 'Table'[ProductProfit] ), 'Table'[Class] IN a ),
,
DESC,
DENSE
)
)
Card Display Product =
CALCULATE (
MAX ( 'Table'[Product] ),
FILTER ( ALLSELECTED ( 'Table'[Product] ), [Rank Product] = 1 )
)
This time when we use the shop slicer to select shop A, it will give us the following result:
Requirement: Since the first rank of Product is PS4, we need to rank their categories.
Create the similar measure like step2:
Rank Category =
VAR a =
CALCULATETABLE (
FILTER ( DISTINCT ( 'Table'[Product] ), [Rank Product] = 1 ),
ALLSELECTED ()
)
RETURN
IF (
CALCULATE ( COUNTROWS ( 'Table' ), 'Table'[Product] IN a ) = 0,
BLANK (),
RANKX (
GROUPBY (
FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Product] IN a ),
'Table'[Category]
),
CALCULATE ( SUM ( 'Table'[CategorySales] ), 'Table'[Product] IN a ),
,
DESC,
DENSE
)
)
Card Display Category =
CALCULATE (
MAX ( 'Table'[Category] ),
FILTER ( ALLSELECTED ( 'Table'[Category] ), [Rank Category] = 1 )
)
After these steps, all the requirements are achieved, when we use the shop slicer to select Shop A, the whole result will be like this:
This is how can we create hierarchy ranks in power bi by Dax. Hope this article helps everyone with similar questions here.
Author: Yingjie Li
Reviewer: Ula Huang, Kerry Wang
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.