I'm creating a dashboard that shows some demographic information for a population. However, in cases where less than 25 individuals show a certain demographic trait, I need to be able to re-categorize that trait as "REDACTED" to protect privacy.
A pseudo-code version might be:
CASE
WHEN COUNT(gender) < 25 THEN "REDACTED"
ELSE gender
END
Unfortunately, this mixes aggregation and non-aggregation and isn't workable in a calculated field.
I'm hoping for some suggestions on how to solve preferably without aggregating first in the data source --I'd like to be for my data and blends to exist at the individual grain with aggregation happening within Google Data Studio.
Publicly-Editable Example Google Data Studio report
Data source (INPUT) is the source, also available here.
Expected output:
gender | count |
---|---|
female | 222 |
male | 223 |
REDACTED | 55 |
The below looks at:
A system error is displayed:
Sorry, calculated fields can't mix metrics (aggregated values) and dimensions (non-aggregated values). Please check the aggregation types of the fields used in this formula. Learn more.
when using the calculated field in the question:
CASE
WHEN COUNT(gender) < 25 THEN "REDACTED"
ELSE gender
END
This ("aggregated values") refers to the 2nd line in the CASE
, specifically the aggregated field:
COUNT
(gender)
One way is to use reaggregation with a blend, by performing a self blend (where the data source of both tables are the same) and creating a dedicated COUNT(gender)
field (titled gender_count
below):
Table 1 | Table 2 |
---|---|
Dimension #1: gender |
Dimension #1: gender |
Dimension #2: person_id |
|
Dimension #3: dept |
|
Metric: Record Count |
Metric: gender Aggregation: COUNT Rename: gender_count |
![]() |
![]() |
Join Description | Table 1 🔗 Table 2 |
---|---|
Join Operator | Left Outer |
Join Condition | gender 🔗 gender |
Image | ![]() |
In summary, the result of the blend will look like:
CASE
WHEN gender_count < 25 THEN "REDACTED"
ELSE gender
END
Record Count
Publicly editable Google Data Studio report (embedded Google Sheets data source) and a GIF to elaborate: