Situation: We need a PDT that precomputes 30‑day retention by signup cohort so dashboards are fast and consistent.Implementation (LookML):- Create a view backed by a persistent derived_table. Use a datagroup that fires daily (or on raw event update) so it rebuilds predictably. Add indexes and partitioning in the SQL if the DB supports it.sql
view: cohort_retention {
derived_table: {
datagroup_trigger: cohort_events_datagroup
sql:
WITH cohorts AS (
SELECT user_id, DATE_TRUNC('day', signup_at) AS signup_day
FROM ${TABLE}.users
WHERE signup_at IS NOT NULL
),
events AS (
SELECT user_id, DATE_TRUNC('day', event_at) AS event_day
FROM ${TABLE}.events
WHERE event_at >= (CURRENT_DATE - INTERVAL '60 day')
)
SELECT
c.signup_day,
datediff('day', c.signup_day, e.event_day) AS days_since_signup,
COUNT(DISTINCT c.user_id) AS cohort_size,
COUNT(DISTINCT CASE WHEN datediff('day', c.signup_day, e.event_day)=30 THEN e.user_id END) AS retained_day_30
FROM cohorts c
LEFT JOIN events e USING (user_id)
WHERE datediff('day', c.signup_day, e.event_day) BETWEEN 0 AND 30
GROUP BY 1,2
;;
}
persist_for: "24 hours" # safety expiry
}
Scheduling & refresh:- Use a datagroup (cohort_events_datagroup) that depends on a table or SQL that reflects source ingestion time (e.g., MAX(updated_at) from events). Datagroups trigger only when upstream data changes, avoiding unnecessary rebuilds.- For time-based needs, set persist_for (e.g., 24h) and schedule a nightly rebuild via Looker PDT rebuild or a separate orchestration job.Governance & stale-table handling:- Ownership: assign an owner and document purpose, query cost, and refresh cadence in a data catalogue.- Monitoring: create alerts using Looker System Activity explores to track PDT build failures, build time, and last_success_at. Integrate with Slack/email for failures.- Failover: have the Explore fallback to a non-PDT (ad-hoc SQL) or surface a warning if PDT is older than threshold (use sql_trigger_value or a “last_built” field) so consumers know data is stale.- Retries & backfills: implement automated retry logic in orchestration (Airflow) and provide a backfill job for missed builds.- Cost controls: limit PDT row size, use partitioning/clustering in warehouse, and review compute charges periodically.Why this approach:- Datagroups ensure rebuilds only when sources change; persist_for provides safety expiration; monitoring + ownership reduces unnoticed staleness and operational risk.