21. Dynamic Charts: Field Parameters, What-if and Dynamic Top N
21.5 Dynamic Top N with a Parameter
Start with a problem
Shraddha wants a chart of the Top N stores where she picks N (3, 4 or 5) herself.
Explain simply
Combine a numeric what-if parameter Top N with a measure that ranks stores and shows sales only for ranks ≤ N.
Show the data
| Store | City | Total Sales (₹) |
|---|---|---|
| Kothrud | Pune | 650 |
| College Road | Nashik | 500 |
| Rajarampuri | Kolhapur | 450 |
| Gangapur | Nashik | 300 |
| Baner | Pune | 200 |
About this data: This table uses columns: Store, City, Total Sales (₹). This table uses columns: Rank, Store, Top N Sales (N = 3). This table uses columns: Store, Total Sales, Rank. This table uses columns: Store, Total Sales. Values shown are the lesson examples already on this page (not a new dataset).
Fictional sample data. Practice file: visuals_orders_apr_may_2026.csv (source: src/practice/).
Demonstrate
- New parameter › Numeric range
Top N: 1 to 5, step 1, default 3. - Create
Top N Sales = VAR n = [Top N Value] VAR r = RANKX(ALLSELECTED(Orders[Store]), [Total Sales]) RETURN IF(r <= n, [Total Sales]). - Bar chart: Y-axis
Store, X-axisTop N Sales; sort descending. - Set N = 3.
Show the result
Your result should look like this (fictional sample):
| Rank | Store | Top N Sales (N = 3) |
|---|---|---|
| 1 | Kothrud | ₹650 |
| 2 | College Road | ₹500 |
| 3 | Rajarampuri | ₹450 |
| — | Gangapur, Baner | blank (hidden) |
Explain what happened
RANKX ranks each store among the stores in the visual; IF returns blank above N, and visuals hide blank rows.
Explore further
- An Others bar (Gangapur + Baner = ₹500) needs an extra table with an Others row.
Detailed reference for this lesson:
Dynamic Top N with a what-if parameter in Power BI with example — in 30 seconds
What: A what-if parameter gives the user a number slider. A Top N measure uses that number to keep only the top stores.
Why: The ops head sometimes wants the top 3 stores, sometimes the top 5, in the same visual.
Tiny example: Slider = 2 → only Andheri East (₹1,100) and Kothrud (₹600) stay in the chart.
Dynamic Top N with a what-if parameter in Power BI with example — 30 सेकंदात
काय: What-if parameter user ला number चा slider देतो. Top N measure त्या number ने फक्त top stores ठेवतो.
का: Ops head ना कधी top 3 stores हवेत, कधी top 5, त्याच visual मध्ये.
छोटं उदाहरण: Slider = 2 → chart मध्ये फक्त Andheri East (₹1,100) आणि Kothrud (₹600) राहतात.
Dynamic Top N with a what-if parameter in Power BI with example — 30 सेकंड में
क्या: What-if parameter user को number का slider देता है. Top N measure उस number से सिर्फ़ top stores रखता है.
क्यों: Ops head को कभी top 3 stores चाहिए, कभी top 5, उसी visual में.
छोटा उदाहरण: Slider = 2 → chart में सिर्फ़ Andheri East (₹1,100) और Kothrud (₹600) रहते हैं.
First look at the data (before → after):
Same before / after tables as text
Before — Sales by store (fictional)
| Store | Total Sales | Rank |
|---|---|---|
| Andheri East | ₹1,100 | 1 |
| Kothrud | ₹600 | 2 |
| College Road | ₹300 | 3 |
| Baner | ₹200 | 4 |
Formula / click path
Modeling › New parameter › Numeric range: Top N = 1 to 10, default 3
Show Store = IF(RANKX(ALLSELECTED(DarkStore[Store Name]), [Total Sales]) <= [Top N Value], 1, 0)
Filters on this visual: Show Store is 1
After — Slider = 2
| Store | Total Sales |
|---|---|
| Andheri East | ₹1,100 |
| Kothrud | ₹600 |
Do it step by step:
- Click Modeling › New parameter › Numeric range.
- Name it Top N. Minimum 1, Maximum 10, Increment 1, Default 3. Click Create. A slider appears.
- Click New measure. Type the Show Store formula.
- Build a bar chart: Store Name and Total Sales.
- Drag Show Store into Filters on this visual. Set it to is 1. Click Apply filter.
- Move the slider to 2. Check: only Andheri East and Kothrud.
एक-एक step करून बघा:
- Modeling › New parameter › Numeric range वर click करा.
- नाव Top N. Minimum 1, Maximum 10, Increment 1, Default 3. Create वर click करा. एक slider दिसतो.
- New measure वर click करा. Show Store formula type करा.
- Bar chart बनवा: Store Name आणि Total Sales.
- Show Store ओढून Filters on this visual मध्ये टाका. is 1 set करा. Apply filter वर click करा.
- Slider 2 वर न्या. तपासा: फक्त Andheri East आणि Kothrud.
एक-एक step करके देखिए:
- Modeling › New parameter › Numeric range पर click कीजिए.
- नाम Top N. Minimum 1, Maximum 10, Increment 1, Default 3. Create पर click कीजिए. एक slider दिखता है.
- New measure पर click कीजिए. Show Store formula type कीजिए.
- Bar chart बनाइए: Store Name और Total Sales.
- Show Store को खींचकर Filters on this visual में डालिए. is 1 set कीजिए. Apply filter पर click कीजिए.
- Slider को 2 पर ले जाइए. जाँचिए: सिर्फ़ Andheri East और Kothrud.
The Filters pane Top N (Module 22.4) uses a fixed N. To let users choose N with a slider:
Steps in Power BI
- Modeling › New parameter › Numeric range › Name
Top N, Whole number, 1 to 20, increment 1, default 5 › Create. - Create the measure below.
- Bar chart: Y-axis = Product Name, X-axis =
[Top N Sales]. Products outside the top N return BLANK and disappear from the chart. - Sort by Top N Sales descending. Add a dynamic title
"Top " & [Top N Value] & " products by sales".
Top N Value = SELECTEDVALUE('Top N'[Top N], 5)
Top N Sales =
VAR n = [Top N Value]
VAR rnk = RANKX(ALLSELECTED(Product[Product Name]), [Total Sales], , DESC, Dense)
RETURN IF(HASONEVALUE(Product[Product Name]) && rnk <= n, [Total Sales])
Because of ALLSELECTED, the ranking respects slicers. For example, the top 5 in Nagpur on Amazon Now.
Ties
With Dense ranking, two products with equal sales share a rank, so you may see more than N bars. That is correct. Explain it in the subtitle, or add a tie-breaker.
Dynamic Top N comes up a lot as an interview scenario. Write this measure yourself.
Dynamic Top N interview मध्ये scenario म्हणून खूप येतो. हा measure स्वतः लिहून बघा.
Dynamic Top N interview में scenario के रूप में बहुत आता है. यह measure ख़ुद लिखकर देखिए.
Ravindra Bagale's Tip
Friends, many students build Top N with RANKX but forget ties, so "Top 5" shows 6 products. Decide how ties should behave (show them, or break them with a second measure), and tell users what happens. Test with a City filter to make sure the ranking respects slicers. Practise, and it will feel very easy.
Ravindra Bagale's Tip – मराठी
मित्रांनो, बरेच students RANKX ने Top N बनवतात पण ties विसरतात, त्यामुळे "Top 5" मध्ये 6 products दिसतात. Ties कसे हाताळायचे ते ठरवा (दाखवायचे, की दुसऱ्या measure ने तोडायचे), आणि काय होतं ते users ना सांगा. Ranking slicers पाळतं ना याची खात्री करण्यासाठी City filter लावून test करा. Practice करा, मग एकदम सोपं वाटेल.
Ravindra Bagale's Tip – हिंदी
दोस्तों, बहुत से students RANKX से Top N बनाते हैं पर ties भूल जाते हैं, जिससे "Top 5" में 6 products दिखते हैं. तय करो कि ties का क्या करना है (दिखाना है, या दूसरे measure से तोड़ना है), और users को बताओ कि क्या होता है. Ranking slicers को मानती है यह पक्का करने के लिए City filter लगाकर test करो. Practice करो, फिर बहुत आसान लगेगा.
Fix common mistakes
| Wrong result | Reason | Correction |
|---|---|---|
| All ranks = 1 | RANKX over a column not in the visual | Rank over ALLSELECTED of the axis column |
| Ties make 4 bars for N = 3 | Equal values share a rank | Add a tie-breaker |
| Top N ignores slicers | Used ALL instead of ALLSELECTED | Use ALLSELECTED |
Practise independently
Practice task
Set N = 4. Which store is added?
Hints
- Next value after 450.
Solution
Gangapur ₹300.
Think like a professional
Dynamic Top N is a classic interview scenario. Explain ALLSELECTED vs ALL in your answer.
Loading DAX practice tasks...
- Product rank (Easy): Shown per Product Name. Measure: Product Rank = rank of [Total Sales] among all product names.
- City rank (Easy): Shown per Store City. Measure: City Rank = rank of [Total Sales] among all cities.
- Platform rank by orders (Easy): Shown per Platform. Measure: App Rank = rank of [Total Orders] among all platforms.
- Top 3 products only (Medium): Shown per Product Name. Measure: Top 3 Sales = [Total Sales] when the product rank is 3 or better, else blank.
- Top 2 cities only (Medium): Shown per Store City. Measure: Top 2 Sales = [Total Sales] when the city rank is 2 or better, else blank.
- City rank by units (Medium): Shown per Store City. Measure: Qty Rank = rank of [Total Qty] among all cities.
- Top 5 or others (Medium): Shown per Customer Name. Measure: Group = "Top 5" when the customer rank by [Total Sales] is 5 or better, else "Others".
- Sales of the top 3 products (Hard): Measure: Top 3 Total = [Total Sales] of the 3 best product names.
- Top 2 store share (Hard): Measure: Top 2 Share = sales of the 2 best store names ÷ sales of all stores.
- Dense rank of categories (Hard): Shown per Category. Measure: Cat Rank = dense rank of [Total Qty] among all categories, highest first.
Revision card