Skip to content
Advertisement

How to count monthly retention user in bigquery?

I have raw data as below. With each line is the record of an transaction of user, and the month when they made the transaction
enter image description here


What I want is to calculate the number of user who made order in a month and the number of repeated user (RETENTION) from last month, then I can know how many % of user is repeated user.

The desired result should look like this enter image description here

How can I do it in bigquery?

Advertisement

Answer

One way to do it is to do it is through a self-join with the same table and a 1-month delay. That way, we match user&month combinations with user&previous-month to see if it’s a returning user. For example, using the 2M row public table bigquery-public-data.hacker_news.stories and a particular user:

enter image description here

Note that prev_month is null (we used LEFT OUTER JOIN) for 2014-02-01 as the user was not active during 2014-01-01. We are joining on author and lagged months with:

Then we count a user as repeating if the previous month was not null:

Note that we do not use DISTINCT here as we already grouped by author and month combinations when defining orders as CTE. You might need to take this into account for other examples.

Full query:

and results snippet:

enter image description here

which are correct results, i.e. for 2007-03-01:

enter image description here

Performance is not too fancy but in this case we are selecting only the fields needed for the aggregated data so scanned data is low and execution time not too high (~5s).

An alternative is to use EXISTS() inside COUNTIF() instead of the join but it takes longer for me (~7s). Query

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement