Situation: Our analytics team needed to know why weekly active users (WAU) dropped by ~12% in the last month to decide whether to throttle an ad campaign.Schema:- events(user_id, event_time, event_type, product_id, platform)- users(user_id, created_at, country)- ad_impressions(user_id, impression_time, campaign_id)Task: Produce WAU by week, detect cohorts with biggest drop, and attribute to platform/campaign.Action — query I wrote (BigQuery / Postgres SQL style):sql
WITH user_events AS (
SELECT
user_id,
DATE_TRUNC(event_time, WEEK) AS week,
platform
FROM events
WHERE event_time BETWEEN '2025-09-01' AND '2025-10-31'
AND event_type = 'open'
),
weekly_active AS (
SELECT
week,
platform,
COUNT(DISTINCT user_id) AS wau
FROM user_events
GROUP BY week, platform
),
week_delta AS (
SELECT
week,
platform,
wau,
LAG(wau) OVER (PARTITION BY platform ORDER BY week) AS prev_wau,
SAFE_DIVIDE(wau - LAG(wau) OVER (PARTITION BY platform ORDER BY week),
LAG(wau) OVER (PARTITION BY platform ORDER BY week)) AS pct_change
FROM weekly_active
)
SELECT * FROM week_delta ORDER BY week, platform;
Key points:- Used COUNT(DISTINCT user_id) and LAG() window to compute week-over-week percent change per platform.- Joined to ad_impressions when attributing to campaigns (not shown) to compute impression rates for users who dropped.Validation:- Cross-checked totals against a simple aggregation: SELECT COUNT(DISTINCT user_id) FROM events WHERE DATE_TRUNC(event_time,WEEK)=X.- Spot-checked raw user_ids for weeks with biggest delta to ensure not double-counting (e.g., timezone issues).- Verified no future timestamps, handled NULLs from LAG, and ensured event_time timezone consistency.- Benchmarked query runtime; added partition pruning on event_time and created composite index (event_time, event_type) to speed up scans.Result & Action:- Insight: iOS platform showed a 22% drop in WAU in week-of-2025-10-12; Android was flat. Correlating with ad_impressions revealed a spike in a specific campaign that targeted iOS users with a redirect causing app-launch failures.- Action: Disabled that campaign within 24 hours and pushed a quick fix to the ad redirect logic. WAU recovered to baseline in two weeks. We also added an automated weekly WAU alert and a small integration test that simulates ad click-to-open flow to prevent recurrence.This approach balances correctness (distinct counts, timezone handling), explainability (per-platform windowed deltas), and performance (partitioning/indexing) for operational decision-making.