Course outline

Tableau / Intermediate: calculation and interaction scope / Lesson 14

Top N Filters and Context

Explain why a top-N result changes when a regional filter is applied before or after the ranking population.

About 3 min readContent reviewed
On this page

Lesson goals

  • Define the population used for a top-N calculation.
  • Compare ordinary and context filters on a small fixture.
  • Check table-calculation partition and sort separately.

Frame the ranking question

“Top product overall, then show East” and “top product within East” are different questions. In a bounded Tableau workbook lab, create product A with East sales 10 and West sales 90, and product B with East sales 40 and West sales 0. Overall A leads with 100; within East B leads with 40.

The formulas below require this synthetic workbook and are illustrative; the public reader does not execute them. Keep one row per product and region, and confirm the source totals before applying filters. The exact UI and supported features depend on the installed Tableau environment.

Show the unfiltered totals

Use the aggregate below in a product crosstab and confirm A is 100 and B is 40. A top-one product filter over all regions selects A. If an ordinary East dimension filter is applied after that top filter in the documented order, the resulting view shows East sales for A, not necessarily the East leader.

tableau Aggregate sales by product
SUM([Sales])

The aggregate summarizes the records that remain in the calculation’s view context.

Define the ranking population

Make the East filter a context filter when the intended top-one selection must be computed within East, then rebuild or review the top filter. In the stated fixture the eligible East totals are A 10 and B 40, so B is first. Confirm the observed view and filter order in the actual workbook rather than relying on color alone.

Inspect a table calculation separately

A table calculation such as the following ranks marks according to its partition and addressing settings. It is not automatically equivalent to a top-N dimension filter. In the lab, set the calculation direction and sort explicitly, inspect the visible marks, and verify how any filters affect the marks before interpreting the rank.

tableau Rank the visible sales marks
RANK_DENSE(SUM([Sales]), "desc")

RANK_DENSE ranks the aggregate according to the table calculation’s configured scope and ordering.

Reconcile the result

Test no region filter, ordinary East filter, and East as context using the same fixture. Record the filter types, ranking rule, tie policy, and expected product for each case. Compare the workbook with a hand calculation and retest after adding dimensions; an altered view can change table-calculation partitions.

Common errors and fixes

The overall leader appears instead of the East leader.

Why it happens
The regional filter was applied after the top-N population was chosen.
How to fix it
Use the documented context order when the question requires ranking within East.
Prevent it next time
State the ranking population before building filters.

A table rank changes when a dimension is added.

Why it happens
The partition or addressing changed with the view.
How to fix it
Configure and inspect the table calculation scope.
Prevent it next time
Retest representative view changes.

A tie yields an unexpected number of marks.

Why it happens
The ranking and tie policy were not specified.
How to fix it
Choose the intended rule and inspect tied values.
Prevent it next time
Include a tie fixture in acceptance checks.

2 practice exercises

1. With A East 10 and West 90, and B East 40 and West 0, who leads overall and in East?

Show worked solution

A leads overall with 100 versus 40. B leads East with 40 versus 10. The top-N filter must use the population matching the question.

2. Why can a table rank change after adding a dimension?

Show worked solution

The new dimension can change the marks, partition, or addressing of the table calculation. Inspect the configured scope and recalculate a small example.

Key takeaways

  • Define the ranking population before applying top-N.
  • Context can change which rows a top filter considers.
  • Table calculation scope needs separate validation.

Official references