The COUNT_DISTINCT
function counts the number of unique items in a field.
Syntax
COUNT_DISTINCT(value)
Parameters
value
- a field or expression that contains the items to be counted.
How the COUNT_DISTINCT function works
The COUNT_DISTINCT
function takes 1 parameter, which can be the name of a metric, dimension, or expression of any type. COUNT_DISTINCT
returns the total number of unique items in that field or expression.
To count all items, including duplicates, use COUNT
.
There are 2 other ways to apply this function:
- In a data source, change a field's Aggregation type to
Count Distinct
. - in a report, edit the field's aggregation in a chart.
COUNT_DISTINCT Example
COUNT_DISTINCT(
Page) - counts the number of non-unique values in the Page dimension.
Limits of COUNT_DISTINCT
You can't apply this function to a pre-aggregated field (Aggregation type of Auto
), or to an expression which is the result of another aggregation function. For example, a formula such as COUNT_DISTINCT(Sessions)
in a Google Analytics data source will produce an error.
To avoid possibly incurring higher query costs when using BigQuery data sources, consider using APPROX_COUNT_DISTINCT.