I’ve covered how Sam demonstrates the top 50/200/rest products by sales using a supportive table.
In case a need to get the top 5%/10%/20% of products by sales, can the same technique be effective? If so, how would the measure look like?
Top 5 % Products =
VAR TopPerc = 0.05
VAR AllProducts =
ALL ( Products[ProductKey] )
VAR TotalProducts =
COUNTROWS ( ALL ( Products[ProductKey] ) )
VAR N =
ROUNDUP ( TotalProducts * TopPerc, 0 )
VAR TOPNProducts =
TOPN ( N, AllProducts, [Total Sales], DESC )
VAR Result =
CALCULATE ( [Total Sales], KEEPFILTERS ( TOPNProducts ) )
RETURN
Result
I use two measures:
In the first one we will perform the calculation of the TopN Product dynamically, using CALCULATETABLE to avoid problems with the RANKX function with negative values.
Product TopN Percent =
VAR _CountProduct = CALCULATE(DISTINCTCOUNT(Products[Index]),ALL(Products))
VAR _TopN = INT( DIVIDE(_CountProduct * [TopN Value],100))
VAR _RankProduct =
IF (
ISINSCOPE ( Products[Product Name] ) && HASONEVALUE ( Products[Product Name] ),
RANKX (
CALCULATETABLE ( VALUES ( Products[Product Name] ), ALLSELECTED ( Products[Product Name] ) ),
[Total Sales]
)
)
VAR Result =
SWITCH(
TRUE(),
_TopN = 0, [Total Sales],
_RankProduct <= _TopN, [Total Sales]
)
Return Result
And
The second part will allow us that the total only takes into account the TopN records.