KatInterview

Calculate Monthly Subscription Churn and Revenue Retention

SQL & DataAuto-graded · 18 test cases
MediumAuto-gradedSQL & Datadata-manipulationaggregationssaas-metrics

Last 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 (int or None): 1 if the user is active, 0 or None if inactive.
  • mrr (int, float, or None): The monthly recurring revenue associated with the snapshot. None should be treated as 0.0.
  • load_ts (str, optional): ISO timestamp of when the record was ingested into the system.

Metric Definitions

  1. Cohort: Users who were active on 2025-07-31 (i.e., is_active == 1).
  2. Logo Churn Count: Number of cohort users who are not active on 2025-08-31 (either is_active != 1 or missing from August snapshot).
  3. Logo Churn Rate: Logo Churn Count / Count of July active cohort users.
  4. Churn Loss: Sum of July MRR for all users in the July cohort who churned in August.
  5. Contraction: For July cohort users who remained active in August, the sum of max(0, July MRR - August MRR).
  6. Expansion: For July cohort users who remained active in August, the sum of max(0, August MRR - July MRR).
  7. Gross Revenue Churn: (Churn Loss + Contraction) / Total July Cohort MRR.
  8. 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 latest load_ts string. If load_ts is missing/None for some rows, a valid non-null timestamp takes precedence over a null timestamp.
  • Scope: Ignore snapshot dates other than 2025-07-31 and 2025-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.0 for the corresponding calculated rates.

Constraints

  • rows contains between 0 and 1,000 dictionaries.
  • Returned values should be floats matching the keys: logo_churn_count, logo_churn_rate, gross_revenue_churn, and net_revenue_retention. Note that logo_churn_count should be returned as a numeric int or float value 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.