24. Monthly active users are not the sum of the days

SQLMediumGrain trap

distinct count · active users · grain · aggregation

How the Colab notebook works

The notebook has the data, an answer cell and a check() function. You need a Google account; nothing is installed on your machine. Free Colab runs only two or three notebooks at a time, so end the session of a finished problem (Runtime > Disconnect and delete runtime) before opening the next one. "Mark as solved" is remembered in this browser only.

The situation

The growth dashboard shows monthly active users (MAU) per platform. A daily active users table already existed, so the first version of the dashboard added the daily numbers up to get the month.

Every monthly number came out more than twice too high. A user who logs in on twenty days is twenty daily active users and one monthly active user.

Input tables

logins

One row per login. A user can log in several times a day, and on more than one platform.

Column Type Description
user_id BIGINT The user.
platform STRING web, android or ios.
login_ts TIMESTAMP_NTZ When the login happened.

Requirement

Return one row per month and platform that has logins.

  • login_month is the first day of the calendar month of the login. A login at 23:59:59 on 31 January belongs to January; one second later belongs to February.
  • monthly_active_users is the number of different users with at least one login on that platform in that month.
  • total_logins is the number of logins on that platform in that month.
  • peak_daily_active_users is the highest number of different users on that platform on any single day of that month.

Expected output

One row per month and platform.

Column Type Description
login_month DATE First day of the month.
platform STRING
monthly_active_users BIGINT Distinct users in the month.
total_logins BIGINT
peak_daily_active_users BIGINT Highest distinct users on one day.

Solve it in the browser

This editor runs DuckDB inside your browser. Nothing is installed and nothing is sent to a server. DuckDB's SQL is close to Spark SQL but not identical. Spark's TRUNC(login_ts, 'MM') does not exist in DuckDB. DATE_TRUNC('month', login_ts) works in both; cast its result to DATE. The Colab notebook runs real Spark.

query.sql DuckDB, in your browser
Ctrl + Enter runs the query

Your result appears here. Your draft is kept in this browser.

Explanation

Show explanationGives the answer away. Try the problem first.

The approach

Some numbers can be added up from smaller pieces and some cannot.

  • Logins can. The logins of a month are the sum of the logins of its days.
  • Distinct users cannot. A user who logs in on 1 March and on 2 March is one daily active user on each day and one monthly active user, not two.

So every distinct count has to be computed at the level it is reported at, straight from the detail rows:

COUNT(DISTINCT user_id)   -- grouped by month and platform, over the logins

The three output numbers live at two different levels, which is why the reference solution has two branches:

  1. monthly_active_users and total_logins: group the logins by month and platform.
  2. peak_daily_active_users: first count distinct users per day and platform, then take the MAX of those daily numbers per month.

The two branches are joined on month and platform.

DATE_TRUNC('month', login_ts) gives the first moment of the month as a timestamp, so it is cast to DATE.

There are 530 logins by 55 users over three months.

Common wrong answers

Adding up daily active users. The mistake in the story. For web in January it gives 50 instead of 15, and every one of the nine rows is more than twice too high. Every monthly_active_users value is wrong. The login totals and the peaks from the same query are right, which is what makes it look trustworthy.

COUNT(user_id) without DISTINCT. Counts logins. monthly_active_users comes out equal to total_logins in every row.

The peak as the busiest day by logins. MAX of a daily COUNT(*) counts a user who logs in twice on a day twice. Wrong in all nine rows.

MAX(COUNT(DISTINCT user_id)) in one query. Spark refuses it: [NESTED_AGGREGATE_FUNCTION] It is not allowed to use an aggregate function in the argument of another aggregate function. The daily count needs its own subquery.

Leaving login_month as a timestamp. The values look right; the check reports the column type. TRUNC(login_ts, 'MM') returns a DATE directly in Spark, but it does not exist in DuckDB.

The same trap one level up

The result has one row per platform. It is tempting to add the three rows of a month to get "monthly active users, all platforms". That is the same mistake again. In January the three platform numbers add up to 44, but only 39 different users logged in: 4 of them used more than one platform and are counted once per platform.

A distinct count is tied to its grain. Change the grain, in time or in any other dimension, and you have to count again from the detail rows. This is also why a pre-aggregated table of distinct counts cannot be rolled up. If the detail is too large to recount, an approximate sketch (approx_count_distinct, HyperLogLog) is the usual way out, because sketches can be merged and plain counts cannot.

Edge cases in the data

Case Where What it catches
A user active on several days of a month most users Summing daily numbers.
Several logins by one user on one day common COUNT(user_id); peak by logins.
Users on more than one platform in a month 4 in January, 5 in February, 5 in March Adding the platform rows together.
Login at 2025-01-31 23:59:59 and at 2025-02-01 00:00:00 user 1, web The month boundary: the first is January, the second February.
Login at 2025-03-31 23:59:59 user 2, ios The last second of the data still belongs to March.