Cohort retention analysis: SQL query, pandas heatmap and reading guide

by fidji Claude English ~612 tokens
Public excerpt Locked
Classic, unbounded or bracket retention? Chosen for your business, then: cohort CTE, activity CTE, period index, censored cells set to NULL, pandas pivot + heatmap with cohort sizes, and how to read rows, columns and diagonals…

About this prompt

From your orders or events table, builds a proper cohort retention analysis: the right retention definition for your business, a SQL query that masks incomplete periods, Python code for the triangle heatmap and curves, and a guide to reading it (flattening point, vertical vs diagonal patterns, small-cohort noise). Ends with sanity checks so the triangle can be trusted.

Example output

Provided by the author
STEP 1 — Definition Repeat-purchase e-commerce → classic monthly retention on paid orders. The current month is censored (NULL). Orders with status 'refunded' and users flagged is_test are excluded. STEP 2 — SQL (PostgreSQL) ```sql WITH first_orders AS ( SELECT user_id, DATE_TRUNC('month', MIN(created_at AT TIME ZONE 'Europe/Paris')) AS cohort FROM orders WHERE status = 'paid' GROUP BY user_id ), activity AS ( SELECT DISTINCT user_id, DATE_TRUNC('month', created_at AT TIME ZONE 'Europe/Paris') AS active_month FROM orders WHERE status = 'paid' ) SELECT f.cohort, (EXTRACT(YEAR FROM age(a.active_month, f.cohort)) * 12 + EXTRACT(MONTH FROM age(a.active_month, f.cohort)))::int AS period_index, COUNT(DISTINCT a.user_id) AS retained_users … ``` STEP 4 — Reading Read down column 3: if newer cohorts sit consistently higher, the onboarding change is a candidate explanation, not a proof…

Details

Model
Claude
Prompt language
English
Who it's for
Data analysts, growth and product teams in SaaS, apps and e-commerce
Length
~612 tokens (estimate)
Delivered as
Prompt text, shown online after purchase

Reviews

Buyers only

No reviews yet. Only buyers of this prompt can leave one.

Report this prompt