Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(data-modeling): drop null country codes in dim_country recipe (#103702)

posthog[bot]2b0e0bd51c49
diff --git a/products/data_modeling/skills/modeling-dimension-tables/SKILL.md b/products/data_modeling/skills/modeling-dimension-tables/SKILL.md
index 767cf8c97aed..c9f73d8acf81 100644
--- a/products/data_modeling/skills/modeling-dimension-tables/SKILL.md
+++ b/products/data_modeling/skills/modeling-dimension-tables/SKILL.md
@@ -47,12 +47,15 @@ in **dbt** (which has no equivalent), or if you need a rate provider PostHog doe
1. **One row per entity, unique key.** A dimension with duplicate keys silently fan-outs every fact it joins.
Test uniqueness (PostHog: verify in the shaping query; dbt: `unique` + `not_null`).
-2. **Alias to clean, stable names** — `country_code`, `region`, `plan_tier`. These names become the join
+2. **Drop NULL keys when you derive a dimension from events.** HogQL keeps a row whose property is
+ missing when the filter only compares it to `''`, so add `isNotNull(...)` as well. A NULL key becomes a
+ dimension row of its own and it matches nothing on the fact side.
+3. **Alias to clean, stable names** — `country_code`, `region`, `plan_tier`. These names become the join
surface everything else depends on.
-3. **Materialize static dimensions on a slow schedule**; don't leave a constantly-read lookup virtual.
-4. **Register and certify.** Annotate the dimension and, if it's load-bearing, certify it in the catalog
+4. **Materialize static dimensions on a slow schedule**; don't leave a constantly-read lookup virtual.
+5. **Register and certify.** Annotate the dimension and, if it's load-bearing, certify it in the catalog
(foundations `governance.md`) so other models discover it and don't build a rival copy.
-5. **Prefer built-in currency** (`convertCurrency`) over a hand-rolled FX table on PostHog.
+6. **Prefer built-in currency** (`convertCurrency`) over a hand-rolled FX table on PostHog.
## Build it
diff --git a/products/data_modeling/skills/modeling-dimension-tables/references/posthog/dim_country.sql b/products/data_modeling/skills/modeling-dimension-tables/references/posthog/dim_country.sql
index 97dc71200761..a6fbc33025ac 100644
--- a/products/data_modeling/skills/modeling-dimension-tables/references/posthog/dim_country.sql
+++ b/products/data_modeling/skills/modeling-dimension-tables/references/posthog/dim_country.sql
@@ -10,7 +10,10 @@ FROM (
SELECT
upper(properties.$geoip_country_code) AS country_code
FROM events
- WHERE properties.$geoip_country_code != ''
+ -- HogQL keeps a row whose property is missing when the filter only tests != '',
+ -- so the null check is what stops NULL from becoming a country_code of its own.
+ WHERE isNotNull(properties.$geoip_country_code)
+ AND properties.$geoip_country_code != ''
AND timestamp >= now() - INTERVAL 90 DAY
) AS seen
LEFT JOIN <country_region_lookup> AS lk ON seen.country_code = lk.country_code