Fix first-entry membership, define the return event and reserve zero for a mature period with no returning members.
Cohort retention heatmaps: keep immature cells blank
Anchor each member once
A cohort retention heatmap groups entities by a first qualifying period, then shows the share that performs a specified return action at each elapsed age. Rows are entry cohorts; columns are ages, not calendar dates. The entity might be a user, account or device, and those IDs are not interchangeable. Deduplicate signup records, resolve identity before grouping and keep each member in the original cohort. If a person returns after an inactive week, they still belong to their first cohort. Choose the return event precisely: opening the app, completing a project and making a payment yield different retention curves. Grain and denominator rules prevent an event count from masquerading as a user count. Funnels answer a related but different progression question.
Hold the original denominator fixed
In return-in-period retention, the numerator at age three is the number of original cohort members active in that period; the denominator is the size of that original eligible cohort. Do not divide by last week's active members unless the chart explicitly measures conditional continuation. Several return events by one member in the same period count once. An age-zero cell is one only if it represents enrollment by definition; it is not automatically one when age zero requires a distinct return action. State whether the chart means activity in that exact period, any activity up to that period, or continuous activity in every period. Those definitions can reverse the visual impression. Linked filters must recalculate cohort membership and denominator together rather than filtering only the numerator.
Separate zero from not yet observed
A young cohort has not reached later ages. Its future cells are undefined, not zero percent. A mature cohort with no returning member has a genuine zero. The current partial week is another state: until it closes, all cohorts' cells for that calendar week are provisional, because an apparent decline can simply be missing future activity. Keep a last-complete-period watermark, emit blank cells beyond it and label the watermark on the display. The triangular blank region should use a distinct missing pattern or text, not the low-retention color. Older data can still be incomplete because event ingestion is delayed; an as-of timestamp and an ingestion-lag audit should accompany published comparisons. Heatmap missingness and period completeness provide the display contract.
Compare equal ages and adequate support
Reading down one age column compares groups at the same lifecycle point; reading diagonally compares the same calendar period across groups of different ages. The latter can reveal a platform outage, but it is not a direct retention comparison. Show cohort sizes because 50 percent may represent one of two members or 5,000 of 10,000. Keep one zero-to-one color range across all rows and do not stretch each row independently. For small cohorts, add counts or uncertainty intervals and avoid strong claims from a bright cell. Segment-specific charts need an eligibility rule frozen at entry; defining a segment using future behavior leaks outcomes into cohort assignment. Small-sample display and calendar completeness help audit this comparison.
Worked contract
from collections import defaultdict
def retention_ledger(enrollments: list[tuple[str, int]],
return_events: list[tuple[str, int]],
last_complete_period: int, max_age: int):
if max_age < 0 or not isinstance(last_complete_period, int):
raise ValueError("age and complete-period watermark must be valid")
entry = {}
cohorts = defaultdict(set)
for member_id, period in enrollments:
if not member_id or member_id in entry or period > last_complete_period:
raise ValueError("members need one completed entry period")
entry[member_id] = period
cohorts[period].add(member_id)
if not entry:
raise ValueError("at least one enrolled member is required")
returned = defaultdict(set)
for member_id, period in return_events:
if (member_id not in entry or period < entry[member_id] or
period > last_complete_period):
raise ValueError("return event falls outside the observed member history")
returned[(entry[member_id], period - entry[member_id])].add(member_id)
rows = []
for cohort_period in sorted(cohorts):
size = len(cohorts[cohort_period])
cells = [1.0]
for age in range(1, max_age + 1):
cells.append(None if cohort_period + age > last_complete_period
else len(returned[(cohort_period, age)]) / size)
rows.append((cohort_period, size, cells))
return rows
members = [("member-47", 47), ("member-59", 47), ("member-83", 47),
("member-97", 48), ("member-131", 48), ("member-149", 49)]
returns = [("member-47", 48), ("member-47", 48), ("member-83", 48),
("member-47", 49), ("member-97", 49)]
heatmap = retention_ledger(members, returns, last_complete_period=49, max_age=2)
assert heatmap[0] == (47, 3, [1.0, 2 / 3, 1 / 3])
assert heatmap[1] == (48, 2, [1.0, 1 / 2, None])
assert heatmap[2] == (49, 1, [1.0, None, None])Performance and operating cost
For E enrollments, A return records, C cohorts and H requested ages, membership construction and event deduplication take O(E + A) expected time; sorting cohort keys takes O(C log C), and cell construction takes O(C·H). The entry and activity sets use O(E + A) space in the worst case, with O(C·H) output cells. Duplicate age-one activity from member-47 still counts once, while the newest cohort's future cells are None rather than zero. Period integers stand for consecutive, fully closed reporting periods; use a calendar-aware key upstream when weeks or months cross time-zone or fiscal boundaries.
Common Mistakes
- Reassigning a returning member to a new entry cohort.
- Dividing by the previous period's active members while labeling the result cohort retention.
- Painting future or partial-period cells as zero percent.
- Counting duplicate return events as multiple retained users.
Validation drill
Build weekly retention for 47 synthetic signup cohorts with a fixed last-complete-week watermark. Reconcile unique entry IDs, duplicate activity events and cohort sizes; publish age-one and age-four counts as well as rates, and leave all immature cells blank.
Read next
- Heatmaps: make missing cells, rates and color domains explicit
- Interactive dashboards: recompute cohorts after linked filters
- Time-series charts: distinguish missing days, zeros and provisional periods
- Funnel charts: hold the cohort still before calculating conversion
