Creating a Dynamic TOP N using a Dimension Parameter

by Kinley Ly

Creating a Dynamic TOP N using a Dimension Parameter is a relatively simple process since the Field Parameter feature we are using is quite similar to how parameters work in Tableau. However, the calculations on the other hand, is a whole different ballpark.

For this Demonstration, I will be using the Superstore Dataset which can be found here (Specifically the US version)

Firstly, let’s bring in the data into Power BI:


To load in our data, we want to select the “Get data” option on the “Home” Tab, choose “Excel Workbook" and open our dataset.
Next we want to only bring in our “Orders” data and select “Transform” in order for Power Bi to do initial formatting.
This will open up Power Query, but we don’t need to do any data cleaning in this dataset, so go ahead and select “Close and Apply”.

Now we want to start creating parameters:

Let's first create our dimension switch. Go to the “Modeling” Tab, select “New Parameter”, and choose “Fields”.
Rename the Parameter to anything you like and the fields we want to select are “Category”, “Sub-Category”, and “Product Name”. Make sure that “Add slicer to this page” is checked.

The next parameter we are creating is the TOP N parameter.
Go to the “Modeling” Tab, select “New Parameter”, and choose “Numeric Range”.
Rename the Parameter and configure the settings as follows:


After creating our two parameters, let's start creating our measures.


Our first measure will be for sales.


Sale = SUM(Orders[Sales])

We need to aggregate Sales first in order for our calculation to properly function.

The second measure will be our Rank measure. The formula is as follows:

Ranks =

SWITCH(

TRUE(),

SELECTEDVALUE(Categories[Categories Fields]) = "'Orders'[Category]",

RANKX(

ALLSELECTED(Orders[Category]),

[Sale],,

DESC

),

SELECTEDVALUE(Categories[Categories Fields]) = "'Orders'[Sub-Category]",

RANKX(

ALLSELECTED(Orders[Sub-Category]),

[Sale],,

DESC

),

SELECTEDVALUE(Categories[Categories Fields]) = "'Orders'[Product Name]",

RANKX(

ALLSELECTED(Orders[Product Name]),

[Sale],,

DESC

)

)


What this formula does is provide the rank of sales for the chosen category. You can always change the sum(sales) to suit your needs.

The next measure is as follows:

Dynamic Filter =

VAR SelectedFilter = SELECTEDVALUE('TOP N'[TOP N])

RETURN

IF(

ISFILTERED('TOP N'),

INT(

[Ranks] <= SelectedFilter)

,1)

This measure allows the chart to change according to the TOP N slicer.

Now we have our calculations, we can start putting everything together!


First, lets put down a chart to start, like a matrix chart.

Next, we will insert our "Categories" parameter as the rows to allow the first parameter to function as well as put down our "Sales" measure into "Values".

Afterwards, place our "Dynamic Filter" into our visual filter and configure it to show items when the value is 1.

That's it!!