SQL for advanced MRR calculation

SQL for advanced MRR calculation

Required SQL knowledge: WITH clause, MIN, MAX LEAST and COALESCE functions, Window functions, INNER JOINS, CASE statements, BigQuery arrays manipulation, subqueries.

The main differences with the simplified version are:

  • Adding inactive months for a customers, between his active subscriptions

  • Identifying MRR change for a customer

  • Categorising our MRR change between new, renewal, upgrade, downgrade and churn

In order to model our revenue, we will write a query which will execute the following step:

  1. Retrieve all subscriptions and create a list of all the month between our oldest and most recent subscription dates

  2. Calculate our customer revenue by month

  3. Create our customer churn months

In the end, our modeled dataset will look like this:

Retrieve all subscriptions and create a list of all the month between our oldest and most recent subscription dates

This step is the beginning of our SQL for simplified MRR calculate article, which is:

Calculate our customer revenue by month

Now for each of our customers we will:

  1. Calculate for each customer is first and last active months.

  2. Create one row per month between his first and last month.

  3. Join our subscriptions with our customer months and set the MRR to 0 for months when a customer subscription is inactive.

  4. Identify for each customer the first_active_month, last_active_month, and for each customer month we add a flag is_first_month and is_last_month.

Create our customer churn months

We add a new month after our customer last month in order to create one churn month per customer

Calculate MRR change

Now we will:

  1. Merge our customer_revenue_by_month table with our customer_churn_month table

  2. Get our customer prior month MRR and calculate our current month MRR change

Classify our MRR change into categories

Now we have all the data we need to categorize our MRR change. We can easily identify :

  • New MRR

  • Churned MRR

  • Upgrade MRR

  • Downgrade MRR

  • Reactivation MRR

Assembling the full query

Now we can assemble all the blocks into our full query:

Run the query and save it (you can use a combination of month and customer_id as primary key).

Build the exploration and the dashboard

By creating an exploration on top on this dataset, we will be able to build the following dashboard:

Last updated