Distinct Count ​
Count the number of unique values in a field using field.count.
How It Works ​
TPL has two forms of count:
| Syntax | Meaning | SQL Equivalent |
|---|---|---|
n or count | Row count | COUNT(*) |
field.count | Distinct count of field values | COUNT(DISTINCT field) |
When you bind count to a field (e.g., occupation.count), it counts the number of unique values of that field within each group. Standalone count or n counts the total number of rows.
Interactive Example ​
Try It
TABLE ROWS education * (n | occupation.count);
Dataset:
samplesoccupation, education, gender, incomeTry Variations
Query Breakdown ​
n- Row count: how many rows exist for each education leveloccupation.count- Distinct count: how many unique occupations appear in each education level- The row count will always be >= the distinct count
This is useful for understanding data cardinality, e.g.:
- How many unique users per region
- How many distinct products per category
- How many unique event types per session
Ordering by Distinct Count ​
You can order and limit rows by distinct count, just like any other aggregate:
sql
-- Top 5 education levels by number of distinct occupations
TABLE ROWS education[-5@occupation.count] * occupation.count;
-- All education levels ordered by distinct occupation count (descending)
TABLE ROWS education DESC@occupation.count * occupation.count;Distinct Count with Percentages ​
Combine distinct count with ACROSS to see each group's share of unique values:
sql
TABLE ROWS education * (occupation.count ACROSS);Related Examples ​
- Multiple Aggregates - Side-by-side aggregations
- Measure Binding - Bind multiple aggs to a measure
- Limit by Value - Top-N ordering