SQL for simplified MRR calculation

SQL for simplified MRR calculation

Required SQL knowledge: WITH clause, MIN and MAX functions, INNER JOINS, BigQuery arrays manipulation.

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

  1. Retrieve all subscriptions

  2. Calculate oldest and most recent subscriptions dates

  3. Generate a list of all the month between our oldest and most recent dates

  4. Join our months with our subscriptions

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

Retrieve all subscriptions

Let's start by querying all of our subscriptions (you may need to update the subscription table in the from command):

Run this query, you should see the content of your subscription table as the result.

Calculate oldest and most recent subscriptions dates

Then we will need to calculate our oldest start date and most recent end date. Let's do it using the min and max functions:

Run this query, you should see the min and max subscriptions dates.

Generate a list of all the month between our oldest and most recent dates

Now we will generate the list of months between our min_date and our max_date:

Run this query, you should see the list of months between our min_date and max_date.

Join our months with our subscriptions

Let's write the final step, which will use an inner join between our subscriptions and our months tables in order to generate one line per active subscription month.

The entire SQL query should now be as following:

Run the query: we now have one line per active subscription month πŸŽ‰

Save the query (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:

Simplified MRR dashboard

Last updated