Calculate Monthly Subscription Churn and Revenue Retention
SQL & DataAuto-graded · 18 test casesLast updated
Process monthly user subscription snapshots to compute customer churn rate, gross revenue churn, and net revenue retention metrics.
You are tasked with analyzing monthly recurring revenue (MRR) snapshot records to evaluate customer retention and revenue changes between July 2025 (2025-07-31) and August 2025 (2025-08-31).
You are given a list of dictionaries, where each dictionary represents an ingestion row of a user's subscription snapshot with the following keys:
user_id(str): Unique identifier for the subscriber.snapshot_date(str): Date string in ISO format (e.g.,'2025-07-31').is_active(intorNone):1if the user is active,0orNoneif inactive.mrr(int,float, orNone): The monthly recurring revenue associated with the snapshot.Noneshould be treated as0.0.load_ts(str, optional): ISO timestamp of when the record was ingested into the system.
Metric Definitions
- Cohort: Users who were active on
2025-07-31(i.e.,is_active == 1). - Logo Churn Count: Number of cohort users who are not active on
2025-08-31(eitheris_active != 1or missing from August snapshot). - Logo Churn Rate:
Logo Churn Count / Count of July active cohort users. - Churn Loss: Sum of July MRR for all users in the July cohort who churned in August.
- Contraction: For July cohort users who remained active in August, the sum of
max(0, July MRR - August MRR). - Expansion: For July cohort users who remained active in August, the sum of
max(0, August MRR - July MRR). - Gross Revenue Churn:
(Churn Loss + Contraction) / Total July Cohort MRR. - Net Revenue Retention (NRR):
(Total July Cohort MRR - Churn Loss - Contraction + Expansion) / Total July Cohort MRR.
Business Rules & Edge Cases
- Deduplication: If multiple records exist for the same
(user_id, snapshot_date)pair, select the entry with the strictly latestload_tsstring. Ifload_tsis missing/None for some rows, a valid non-null timestamp takes precedence over a null timestamp. - Scope: Ignore snapshot dates other than
2025-07-31and2025-08-31. - New Logos / Reactivations: Ignore users who were not active in July (even if active in August). They do not belong to the initial July cohort.
- Division by Zero: If the July cohort size or total July cohort MRR is zero, return
0.0for the corresponding calculated rates.
Constraints
rowscontains between 0 and 1,000 dictionaries.- Returned values should be floats matching the keys:
logo_churn_count,logo_churn_rate,gross_revenue_churn, andnet_revenue_retention. Note thatlogo_churn_countshould be returned as a numericintorfloatvalue representing the exact count.
Examples
Example 1
Input: compute_august_churn_metrics([{'user_id': 'u1', 'snapshot_date': '2025-07-31', 'is_active': 1, 'mrr': 20, 'load_ts': '2025-07-31T23:59:00'}, {'user_id': 'u1', 'snapshot_date': '2025-08-31', 'is_active': 0, 'mrr': 0, 'load_ts': '2025-08-31T23:59:00'}, {'user_id': 'u2', 'snapshot_date': '2025-07-31', 'is_active': 1, 'mrr': 50, 'load_ts': '2025-07-31T23:59:00'}, {'user_id': 'u2', 'snapshot_date': '2025-08-31', 'is_active': 1, 'mrr': 50, 'load_ts': '2025-08-31T23:59:00'}, {'user_id': 'u3', 'snapshot_date': '2025-07-31', 'is_active': 1, 'mrr': 30, 'load_ts': '2025-07-31T23:59:00'}, {'user_id': 'u3', 'snapshot_date': '2025-08-31', 'is_active': 1, 'mrr': 20, 'load_ts': '2025-08-31T23:59:00'}, {'user_id': 'u4', 'snapshot_date': '2025-07-31', 'is_active': 0, 'mrr': 0, 'load_ts': '2025-07-31T23:59:00'}, {'user_id': 'u4', 'snapshot_date': '2025-08-31', 'is_active': 1, 'mrr': 15, 'load_ts': '2025-08-31T23:59:00'}, {'user_id': 'u5', 'snapshot_date': '2025-07-31', 'is_active': 1, 'mrr': 15, 'load_ts': '2025-07-31T23:59:00'}, {'user_id': 'u5', 'snapshot_date': '2025-08-31', 'is_active': 0, 'mrr': 0, 'load_ts': '2025-08-31T23:59:00'}, {'user_id': 'u6', 'snapshot_date': '2025-07-31', 'is_active': 1, 'mrr': 25, 'load_ts': '2025-07-31T23:59:00'}, {'user_id': 'u6', 'snapshot_date': '2025-08-31', 'is_active': 1, 'mrr': 35, 'load_ts': '2025-08-31T23:59:00'}, {'user_id': 'u7', 'snapshot_date': '2025-07-31', 'is_active': 1, 'mrr': 40, 'load_ts': '2025-07-31T23:59:00'}, {'user_id': 'u7', 'snapshot_date': '2025-08-31', 'is_active': 0, 'mrr': 0, 'load_ts': '2025-08-31T23:59:00'}])
Output: {'logo_churn_count': 3, 'logo_churn_rate': 0.5, 'gross_revenue_churn': 0.4722222222222222, 'net_revenue_retention': 0.5833333333333334}
Hints
Hint 1
Start by deduplicating snapshot entries per (user_id, snapshot_date) using the latest load_ts.
Hint 2
Identify the July 31 active cohort first. Users outside this initial set should not be included in retention or churn calculations.
Hint 3
Track churned users separately from retained users to handle MRR changes, contraction, and expansion accurately.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
To compute retention metrics reliably from dirty snapshot logs, we structure our processing into four clear stages:
- Deduplication: Iterate through all snapshot rows and partition by
(user_id, snapshot_date). Keep only the row with the latestload_tsstring. Treat rows withload_ts = Noneas older than any entry with a non-null timestamp. - Snapshot Extraction: Map the deduplicated records into distinct dictionaries for July (
2025-07-31) and August (2025-08-31). Extractis_activestatus and convertmrrtofloat(defaultingNoneto0.0). - Cohort Identification:
- The base cohort consists only of users with
is_active == 1in July. - Users are classified as churned if they were in the July cohort but are not active in August (
is_active != 1or unlisted). - Remaining cohort members are retained.
- The base cohort consists only of users with
- Metric Calculations:
- Churn Loss: Sum of July MRR for churned users.
- Contraction: Sum of positive delta
(July MRR - August MRR)for retained users. - Expansion: Sum of positive delta
(August MRR - July MRR)for retained users. - Compute the rates using total July MRR and initial cohort count, guarding against division by zero.
Complexity
- Time Complexity: $\mathcal{O}(N)$ where $N$ is the number of rows in the input list. Deduplication and aggregations run in linear time.
- Space Complexity: $\mathcal{O}(U)$ where $U$ is the number of unique users in the snapshots, used for mapping states and values.
from typing import Dict, List
def compute_august_churn_metrics(rows: List[Dict]) -> Dict[str, float]:
latest = {}
for row in rows:
user_id = row["user_id"]
date = row["snapshot_date"]
key = (user_id, date)
load_ts = row.get("load_ts")
current = latest.get(key)
if current is None or (load_ts is not None and (current.get("load_ts") is None or load_ts > current["load_ts"])):
latest[key] = row
july = {}
august = {}
for (user_id, date), row in latest.items():
target = july if date == "2025-07-31" else august if date == "2025-08-31" else None
if target is not None:
target[user_id] = row
users = set(july) | set(august)
july_active = set()
august_active = set()
july_mrr = {}
august_mrr = {}
for user_id in users:
july_row = july.get(user_id, {})
august_row = august.get(user_id, {})
july_mrr[user_id] = float(july_row.get("mrr") or 0)
august_mrr[user_id] = float(august_row.get("mrr") or 0)
if july_row.get("is_active", 0) == 1:
july_active.add(user_id)
if august_row.get("is_active", 0) == 1:
august_active.add(user_id)
churned = july_active - august_active
churn_loss = sum(july_mrr[user_id] for user_id in churned)
retained = july_active - churned
contraction = sum(
max(0.0, july_mrr[user_id] - august_mrr[user_id])
for user_id in retained
)
expansion = sum(
max(0.0, august_mrr[user_id] - july_mrr[user_id])
for user_id in retained
)
total_july_mrr = sum(july_mrr[user_id] for user_id in july_active)
return {
"logo_churn_count": len(churned),
"logo_churn_rate": len(churned) / len(july_active) if july_active else 0.0,
"gross_revenue_churn": (churn_loss + contraction) / total_july_mrr if total_july_mrr else 0.0,
"net_revenue_retention": (total_july_mrr - churn_loss - contraction + expansion) / total_july_mrr if total_july_mrr else 0.0,
}