Segmentation and Top-N

Segmentation groups the results of a query, like GROUP BY in SQL. Top-N keeps only the highest or lowest groups and can roll the rest into one "Other" row.

By attribute

{
  "metrics": [{ "property": "orders.revenue", "aggregation": "Sum" }],
  "segmentBy": ["orders.country"]
}

One result per country. List several properties for one result per combination:

{
  "segmentBy": ["orders.country", "orders.category"]
}

In the app the same request can show as two columns or, with Segment Also By, as one combined breakdown (Czechia, Electronics) — see Segments.

A property of a linked dataset works the same way — "segmentBy": ["customers.country"] on an orders query. See Cross-Dataset Analysis.

By time period

timeSegments groups by a time property by a calendar period:

{
  "metrics": [{ "property": "orders.revenue", "aggregation": "Sum" }],
  "timeSegments": [{ "property": "orders.orderedAt", "grouping": "MONTH", "timezone": "Europe/Prague" }]
}
Grouping Kind
MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR Continuous — one group per real period, so two years by MONTH give 24 groups in order
HOUR_OF_DAY, DAY_OF_WEEK, WEEK_OF_YEAR, MONTH_OF_YEAR, QUARTER_OF_YEAR Recurring — every year folds onto the same few groups, for seasonality

timezone is optional (UTC by default). Time groups come after the segmentBy groups in each result key.

Long ranges at a fine grouping

A year by minute is over half a million groups — more than any chart can draw. A request that would produce that many time groups is refused with a message naming the three ways forward:

Narrow the time range, choose a coarser grouping (minute → hour → day → month), or add a Top-N limit.

The grouping is never changed for you: a chart drawn by hour when you asked for minutes looks just like the one you asked for, so you could not tell.

By numeric range

segmentBuckets groups a measure into ranges of a fixed size:

{
  "metrics": [{ "property": "orders.orderId", "aggregation": "Count" }],
  "segmentBy": ["orders.revenue"],
  "segmentBuckets": { "orders.revenue": 10000 }
}

This counts orders per revenue range of 10,000 (0–9,999, 10,000–19,999, …), in the measure's stored units.

Rows whose measure has no value fall into one extra group, Unknown, whose key is 9007199254740991 — the same key a row with no time gets under a time grouping. Like any group, it is left out when every measure in it is empty. Add a Measure Is Not Null condition to leave it out, or Measure Is Null to query it alone.

Top-N

Rank groups by a measure and keep the first few:

{
  "metrics": [
    { "property": "orders.revenue", "aggregation": "Sum" },
    { "property": "orders.orderId", "aggregation": "Count" }
  ],
  "segmentBy": ["orders.category"],
  "orderBy": { "metricIndex": 0, "direction": "Descending" },
  "segmentLimit": 10
}
Field Type Description
orderBy.metricIndex Number Zero-based position of the ranking measure in metrics
orderBy.direction String Ascending or Descending
segmentLimit Number How many groups to return

The top 10 categories by revenue, highest first. To rank by an attribute's value instead, see Ordering segments by value; to rank by a computed measure, Ranking by a computed measure.

The "Other" row

Add includeOther to get one more row holding everything outside the top N:

{
  "metrics": [{ "property": "orders.revenue", "aggregation": "Sum" }],
  "segmentBy": ["orders.category"],
  "orderBy": { "metricIndex": 0, "direction": "Descending" },
  "segmentLimit": 5,
  "includeOther": true
}

"Other" is exact for every aggregation, averages, distinct counts and percentiles included. With a Count, Median or Percentile measure over an extreme number of groups, the request may be refused rather than answered with an approximate "Other".

Ranking one property of several

With more than one property in segmentBy, Top-N ranks their combinations. orderBy.rankBy ranks the properties you list instead — each value by the measure over every value of the others — and keeps every row of the top N:

{
  "metrics": [{ "property": "orders.revenue", "aggregation": "Median" }],
  "segmentBy": ["orders.category", "orders.channel"],
  "orderBy": { "metricIndex": 0, "direction": "Descending", "rankBy": ["orders.category"] },
  "segmentLimit": 3,
  "includeOther": true
}

The three categories with the highest median revenue across all channels, each with every channel it sells through. includeOther then adds one exact "Other" row per channel: its key holds the "Other" value at the category position and the channel's own value beside it. A Radar chart with a Color By ranks its spokes this way.

rankBy lists segmentBy properties (not time segments) and needs a measure ranking. It is refused with 400 when a measure reads a different dataset from the rest, or when a multi-value property is left out of the list — in both a row would be counted twice.

Ordering segments by a measure

orderBy ranks the groups by one of the request's measures, named by its position:

"orderBy": { "metricIndex": 1, "direction": "Descending" }
Field Type Description
metricIndex Number Zero-based position of the ranking measure in metrics. A computed measure is named with computedMetric instead — see Ranking segments by a computed measure
metric, aggregation String The ranking measure by name — its property and aggregation. Optional beside metricIndex (both must then describe the same measure). On their own, they rank by that aggregate even when it is not among the measures
direction String Ascending or Descending

Prefer metricIndex. A name cannot tell apart two measures over the same property and aggregation — one with its own conditions and one without — so such a request without metricIndex is refused with 400. A number in metric is refused too.

Ordering segments by value

orderBy can rank groups by an attribute's value instead — "the first 10 categories alphabetically":

{
  "metrics": [{ "property": "orders.revenue", "aggregation": "Sum" }],
  "segmentBy": ["orders.category"],
  "orderBy": { "property": "orders.category", "direction": "Ascending" },
  "segmentLimit": 10,
  "includeOther": true
}
  • property must be one of the request's segmentBy properties, an attribute of the dataset the query starts from. direction is required: Ascending is A→Z, Descending Z→A.
  • The order is the value's sort key when one was uploaded, else its display name, else the value. A missing value sorts last in both directions.
  • With several segmentBy properties, groups that share the ordered value follow the remaining properties, ascending.
  • Without segmentLimit every group comes back, in value order.
  • includeOther adds one "Other" row with the exact aggregate of every row outside the kept groups — averages, distinct counts and percentiles included.
  • Value ordering cannot be combined with a measure ranking in the same orderBy, with properties of linked datasets in segments or measures, with per-measure filters, or with sub-aggregations — such requests are refused with 400 naming the conflict.

The first or last N periods

property may also be the property of one of the request's timeSegments — the groups are then ordered by period. "The last 12 months, and one row for everything older":

{
  "metrics": [{ "property": "orders.revenue", "aggregation": "Sum" }],
  "timeSegments": [{ "property": "orders.orderedAt", "grouping": "MONTH" }],
  "orderBy": { "property": "orders.orderedAt", "grouping": "MONTH", "direction": "Descending" },
  "segmentLimit": 12,
  "includeOther": true
}
  • Ascending is oldest first, Descending newest first. A recurring grouping (DAY_OF_WEEK, HOUR_OF_DAY, MONTH_OF_YEAR…) is ordered by its own number — Monday = 1.
  • grouping names which time segment to order by, spelled as in timeSegments. It can be left out when only one time segment is on that property; with two it is required (400 otherwise).
  • The group of rows with no time sorts last in both directions, like a missing attribute value.
  • Ties on the period (with an attribute in segmentBy as well) follow the remaining properties, ascending, and the "Other" row is exact, as above. The same refusals apply.

Combined example

Top 10 countries by number of orders in March 2024, with revenue and average order value, plus "Other":

{
  "conditions": [
    { "type": "Time In Range", "property": "orders.orderedAt", "minimum": 1709251200000, "maximum": 1711929600000 }
  ],
  "metrics": [
    { "property": "orders.revenue", "aggregation": "Sum" },
    { "property": "orders.orderId", "aggregation": "Count" },
    { "property": "orders.revenue", "aggregation": "Average" }
  ],
  "segmentBy": ["orders.country"],
  "orderBy": { "metricIndex": 1, "direction": "Descending" },
  "segmentLimit": 10,
  "includeOther": true
}

Limits in the app

A table in Explore or on a dashboard with no Top-N caps itself when you group by something with very many values (a customer id, an order id). It shows the first rows ranked by its first measure and says so:

Showing first 5,000 rows — result is larger. Add a Top-N to this visualization to control what is shown.

Add a Top-N and the automatic limit steps aside. Charts never draw a partial picture — they show a message instead and ask for a Top-N or a narrower filter.

← FilteringComputed Measures →