Advanced Top N in Power BI
Here we set up a slicer to define how many Top items we want to see, but importantly classify the rest as 'Others'
This allows us to see the whole picture and focus on the leading items
https://www.selectdistinct.co.uk/2023/07/27/advanced-top-n-filter-power-bi/
#PowerBI #TOPN #DataVisualisation
2. We showed how the standard filters in Power BI could be used to filter
the top or bottom rows
But, what if you also need to show the ‘others’ at the same time
This is a little more in depth, this guide shows you how
3. Advanced Top N
If you use the standard
TOP 5 filter, you ONLY
see the top 5
We will show you how
to add a slicer to select
which top rows to see
And also how to show
the balance as an
‘Others’ row
4. Step 1
Ensure there is a
measure value for the
ranking
You may have a
measure already, but if
not create one
DAX Code
Gross Sales New =
SUM(Financials[Gross Sales])
5. Step 2
Create a new table to determine the values for
‘Other’
Use this DAX code to create the new table
TopN Names =
UNION
(ALLNOBLANKROW( Financials[Country]),
{"Others"}
)
6. Step 3
Create a parameter to set the number of top
rows to show
ensure the parameter details are set as this
This creates a new parameter table and a slicer at the same time
7. Step 4
Add a new measure
This applies a ranking to all values in the rows,
including others, based on the measure.
Ranking =
VAR CatToRank = [TopN Value]
VAR Ranking =
RANKX (
ALLSELECTED('TopN Names'[Country]),
[Gross Sales New]
)
VAR IsOtherSelected =
SELECTEDVALUE('TopN Names'[Country]) = "Others"
VAR Result =
IF (
IsOtherSelected,
CatToRank + 1,
IF ( Ranking <= CatToRank , Ranking, ranking + 1)
)
Return
Result
8. Step 5
Make the rows visible if their rank is within the
Top N results
Create another measure with this code
RowVisible =
VAR Ranking = [Ranking]
VAR TopNValue = 'TopN'[TopN Value]
VAR Result = INT( Ranking <= TopNValue + 1)
Return
Result
9. Step 6
Compute a value for 'Others'
Gross Sales New Amount =
VAR NewMeasureAll =
CALCULATE( [Gross Sales New], REMOVEFILTERS('TopN Names'[Country]) )
RETURN
IF (
ISINSCOPE('TopN Names'[Country] ),
VAR TopNToRank = [TopN Value]
VAR IsOtherSelected = SELECTEDVALUE('TopN Names'[Country]) = "Others"
VAR TopNWithMeasure =
ADDCOLUMNS(
ALLSELECTED('TopN Names'[Country]),
"@amt" , [Gross Sales New]
)
VAR TopNCat = TOPN( TopNToRank, TopNWithMeasure, [@amt] )
VAR TotalTopN = SUMX( TopNCat,[@amt] )
VAR Result = IF ( IsOtherSelected, NewMeasureAll - TotalTopN , [Gross Sales New])
RETURN Result ,
NewMeasureAll
)
This creates a new measure which is used in place of the original. This is necessary as the total of “Other” results has to be recalculated when the
TopN value changes.
10. Create the relationship to the Top N
Enable “Other” values by Joining TopN
Names table to the Financials table.
Open the Model window using the
bottom icon in the left menu
Create a connection (Join) between the two Country objects
11. Filter to show Top N and Other
As it stands the Matrix is showing the original values, and all values.
The Row Visible measure can be used to filter as only TopN and
Other
Ensure the Matrix is selected.
Drag Row Visible item from Fields Pane to the Add Data field in the
Filter Pane
Change the Show Items drop down to ‘IS’
Add ‘1’ to the value box
Click Apply Filter
12. Include the new values in the matrix
Swap Gross Sales New
measure for the Gross Sales
New Amount in the
Visualisations Pane, Values
box
This changes the values in
the Matrix. Note ‘Other’ is
now shown.
By default this is sorting the values by country ascending, we want the
values to sort descending, but with the 'others' to always be last
We add the 'Ranking' measure into the matrix
Order the Ranking values Ascending. Click on the column header in the
matrix until the Arrow points up and the ranking is from 1 upwards.
13. Use in other visuals
Change the visual type to Pie
Chart.
This example clearly shows
the top 5 as representing
around a quarter of the
whole, with 'Others' showing
the majority
In this case we do not need
Ranking, it can be removed
14. See it in action
Users can use the slicer to alter the level of detail they need to see
15. This is a little more complicated than the normal Top N filter, but the
overall effect is worth the effort
Having the option to select the Top N value using a slicer in Power BI is a
great way to allow the user to interact with the data
16. For more Tips, Tricks and
Timesavers, visit our website
Business Analytics Blog – Select Distinct
Credit: simon.harrison@selectdistinct.co.uk