Skip to content
Advertisement

Count distinct id between months previous year and same months current year Bigquery

I have a dataset in bigquery which contains order_date: DATE and customer_id.

How can I count distinct customer_id between the months of the previous year and the same months of the current year? For example, from 2020-01-01 to 2021-01-01, then from 2020-02-01 to 2021-01-01, and so on until the current date and should be grouped by the latest date. The output looks like

and the next periods shouldn’t include the previous.

Thanks in advance.

Advertisement

Answer

If you want just a count for each month you can expand the data and aggregate:

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