Skip to content

Top-N roll-up

A top-N roll-up keeps only the highest-ranking N children at every level (by a chosen measure) and, optionally, folds the remainder into a single “Others” node that sums their measures. It’s the standard “top 10 products, everything else combined” view — the engine does the selection and the summation, so the totals stay correct.

This is best seen in a running app (the tree reshapes as “Others” absorbs the tail). The declarative config:

from dash_tensor_grid import TensorGrid, build_grid
payload = build_grid(sales, {
"rows": ["region", "country"],
"measures": {"revenue": "sum"},
"formats": {"revenue": "currency:USD"},
"top_n": {"by": "revenue", "n": 5, "others": True, "others_label": "Others"},
})
TensorGrid(id="grid", row_data=payload["row_data"], column_defs=payload["column_defs"])
  • by — the measure to rank siblings by; n — how many to keep per group.
  • others — when true, the rest roll into one bucket that sums the measures (a synthetic node, so it is excluded from selection / export); others_label / othersLabel names it (default “Others”). Omit others to simply drop the tail.
  • descending (config, default true) keeps the highest values; set it false to keep the lowest N.
  • Top-N applies to the row tree only — it is refused on a pivot config. It runs after filtering and before sort, and composes cleanly with window columns (rank the kept siblings, cap the rest).