Mastering Product Analytics with Mixpanel & GA4Lesson 27 of 32
Lesson 27 of 32

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.
Example

Real-World Example: Tracking School Graduation Classes Over 4 Years

PRACTICAL CONTEXT
  • 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.
Corporate / Tech Term Everyday / Practical Metaphor Operational Meaning
Cohort Birth Week The "Class of 2022" freshman year The calendar week the user first registered in the app.
Week Number ($0, 1, 2, \dots$) Freshman Year (0), Sophomore (1), Junior (2), Senior (3) Number of weeks elapsed since the user's birth week.
The Empty Cells (Triangle Shape) Future school years that haven't happened yet Recent cohorts haven't existed long enough to reach Week 12.
Cohort Retention % The percentage of classmates still attending school Active users in Week N divided by original Cohort Size.
Core Principle: Read cohort triangles horizontally to see retention decay over time; read them vertically to see if recent product updates improved retention for newer cohorts!

Visual Infographic: How a Cohort Triangle Table Forms in SQL

COHORT TRIANGLE INFOGRAPHIC
Stage 1
1. User Birth Date

Find user's first active date: `MIN(event_date) AS cohort_week`.

Stage 2
2. Subsequent Visits

Join user's future visits: `DATE_DIFF(visit_week, cohort_week, WEEK) AS week_num`.

Stage 3
3. Pivot into Triangle

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 CohortNew Users (W0)Week 1 Retained %Week 2 Retained %Week 3 Retained %Week 4 Retained %
Week 1 (Oct 06)50,00042.0%34.2%29.5%27.1%
Week 2 (Oct 13)54,00043.5%35.0%30.1%27.8%
Week 3 (Oct 20)58,00045.2% (+New UI)37.1%32.0%- (In Progress)
Week 4 (Oct 27)62,00046.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.

Retention

Rolling Retention & Cohort Triangle Heatmap Queries in SQL

Measuring percentage of acquired users returning on Day 1, Day 7, Day 14, and Day 30

RETENTION COHORTS
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%
-
-
-
Retention Plateau Sign: Notice Day 14 and Day 30 stabilize between 21% and 23%. A flat retention curve signifies true Product-Market Fit (PMF)!
BigQuery / Snowflake SQL Console

Step-by-Step UI Tutorial: Building a Cohort Triangle Query

Step 1
Step 1: Write Cohort Assignment CTE

`WITH cohort AS (SELECT user_id, DATE_TRUNC(MIN(event_date), WEEK) AS cohort_week FROM events GROUP BY 1)`.

Step 2
Step 2: Join Future Visits CTE

`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)`.

Step 3
Step 3: Pivot Table by Week Number

`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`.

Step 4
Step 4: Visualize Heatmap in Looker

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 Directionwhat 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

  1. Write the SQL formula to calculate `week_number` elapsed between a user's signup week and their current visit week.
  2. Explain why the bottom-right corner of a cohort retention matrix is always empty.