Rolling Retention & Cohort Triangle Heatmap Queries in SQL
Overview & Learning Objectives
Have you ever seen those beautiful triangular heatmap tables where the rows show user signup weeks (e.g. "Week of Jan 1", "Week of Jan 8") and the columns show retention percentages decaying across Week 0, Week 1, Week 2, through Week 12? That is the Cohort Retention Triangle! It is the holy grail of product retention analysis. In this lesson, you will master the SQL architecture behind cohort heatmaps: `DATE_TRUNC()`, acquisition cohort tagging, calculating week intervals, and pivoting data into triangle tables.
- Define user acquisition cohorts using `DATE_TRUNC(first_seen_timestamp, WEEK)`.
- Calculate activity intervals using `DATE_DIFF(event_date, cohort_date, WEEK)`.
- Pivot cohort results into an executive Retention Triangle Matrix with color-coded decay percentages.
Real-World Example: Tracking School Graduation Classes Over 4 Years
- Scenario: Tracking high school graduation classes year by year to see alumni college enrollment rates.
- Retention Matrix: Groups users by signup week on the Y-axis and elapsed weeks active on the X-axis.
- SQL Output: Builds the classic triangular retention heatmap to detect Product-Market Fit plateaus.
Visual Infographic: How a Cohort Triangle Table Forms in SQL
COHORT TRIANGLE INFOGRAPHICFind user's first active date: `MIN(event_date) AS cohort_week`.
Join user's future visits: `DATE_DIFF(visit_week, cohort_week, WEEK) AS week_num`.
Count unique users per `cohort_week` across `week_0, week_1, week_2, week_3`.
Industry Standard & Framework
Standard: SaaS Cohort Triangle Analytics & BigQuery Pivot Standards.
Real-World Business Application: Blinkit Weekly Grocery Customer Retention Triangle Matrix
Blinkit data team queries BigQuery to analyze weekly repeat grocery ordering retention across acquisition cohorts.
| Acquisition Cohort | New Users (W0) | Week 1 Retained % | Week 2 Retained % | Week 3 Retained % | Week 4 Retained % |
|---|---|---|---|---|---|
| Week 1 (Oct 06) | 50,000 | 42.0% | 34.2% | 29.5% | 27.1% |
| Week 2 (Oct 13) | 54,000 | 43.5% | 35.0% | 30.1% | 27.8% |
| Week 3 (Oct 20) | 58,000 | 45.2% (+New UI) | 37.1% | 32.0% | - (In Progress) |
| Week 4 (Oct 27) | 62,000 | 46.8% (+Diwali) | 38.5% | - (In Progress) | - (In Progress) |
Step-by-Step UI Tutorial & Visual Workflow Guide
Below is the practical, non-technical walkthrough showing how Product Analysts design, review, and execute this capability inside real-world tools like Mixpanel, Google Analytics 4, BigQuery, and Google Tag Manager.
Rolling Retention & Cohort Triangle Heatmap Queries in SQL
Measuring percentage of acquired users returning on Day 1, Day 7, Day 14, and Day 30
| Acquisition Cohort | Size | Day 0 | Day 1 | Day 7 | Day 14 | Day 30 |
|---|---|---|---|---|---|---|
| Oct 01 – Oct 07 | 12,450 |
100%
|
48.2%
|
32.1%
|
26.4%
|
21.8%
|
| Oct 08 – Oct 14 | 14,200 |
100%
|
51.4%
|
35.6%
|
28.9%
|
23.2%
|
| Oct 15 – Oct 21 | 13,800 |
100%
|
53.0%
|
38.2%
|
31.0%
|
-
|
| Oct 22 – Oct 28 | 15,600 |
100%
|
56.8%
|
41.5%
|
-
|
-
|
| Oct 29 – Nov 04 | 16,100 |
100%
|
58.2%
|
-
|
-
|
-
|
Step-by-Step UI Tutorial: Building a Cohort Triangle Query
`WITH cohort AS (SELECT user_id, DATE_TRUNC(MIN(event_date), WEEK) AS cohort_week FROM events GROUP BY 1)`.
`WITH activity AS (SELECT e.user_id, c.cohort_week, DATE_DIFF(DATE_TRUNC(e.event_date, WEEK), c.cohort_week, WEEK) AS week_num FROM events e JOIN cohort c ON e.user_id = c.user_id GROUP BY 1, 2, 3)`.
`SELECT cohort_week, COUNT(DISTINCT CASE WHEN week_num = 0 THEN user_id END) AS w0, COUNT(DISTINCT CASE WHEN week_num = 1 THEN user_id END) AS w1 FROM activity GROUP BY 1 ORDER BY 1`.
Export table to Looker Studio and apply color scale formatting (dark blue = 100%, light blue = 20%).
How to Read a Cohort Triangle Matrix
TRIANGLE DECODER| reading Direction | what It Reveals |
|---|---|
| Horizontal (Row by Row) | Retention Decay: How fast does a single cohort forget about the app over 12 weeks? |
| Vertical (Column by Column) | Product Evolution: Are Week 1 retention numbers getting better over calendar time? |
| Diagonal (Bottom-Left to Top-Right) | Calendar Calendar Effect: Shows if an external holiday (Diwali / New Year) boosted all cohorts simultaneously |
Implementation Guidelines for Product Analysts
- Always use `DATE_TRUNC()` to standardize timestamps to the start of the week (Monday or Sunday) to align cohorts.
- Divide by `NULLIF(week_0_users, 0)` to prevent divide-by-zero database errors.
- Exclude the current active week from final reporting because incomplete weeks artificially depress retention numbers.
Practical Hands-On Exercises
- Write the SQL formula to calculate `week_number` elapsed between a user's signup week and their current visit week.
- Explain why the bottom-right corner of a cohort retention matrix is always empty.