You own the April billing health dashboard for a SaaS product. Finance reviewed the latest run and flagged two separate problems:
- MRR is overstated — accounts they know cancelled or fell past due during April are still being counted as paying.
- Some workspaces appear more than once, and their net MRR looks doubled.
The report is built from three SQL files that run in order:
queries/01_current_subscription_state.sql derives each workspace's current subscription from the event log.
queries/02_april_credit_ledger.sql rolls April credits up to one row per workspace.
queries/03_billing_health_rollup.sql is the final dashboard query and depends on the two helper views above.
Read all three before you change anything — the two symptoms have two different causes, and they are not in the same file.
Business rules for April 2026:
- The reporting cutoff is
2026-04-30 23:59:59.
subscription_events is an append-only log. A workspace's current subscription is its most recent event (by occurred_at) at or before the cutoff; if two events share the same occurred_at, the one with the higher id is current.
- Include only external, non-deleted workspaces.
- Include only workspaces whose current status is
active.
- Include only current
pro and enterprise plans.
- Net MRR starts from the current subscription event's
mrr_cents.
- Subtract the total of the workspace's April
billing_adjustments rows where kind = 'credit'.
- Ignore
writeoff and manual_charge adjustments.