SELECT
month,
new_users,
retained_users,
resurrected_users,
total_users,
-(LAG(total_users) OVER(ORDER BY month) - retained_users) AS churned_users
FROM(
SELECT
current_month AS month,
COUNT(DISTINCT(IF(min_month=current_month,fullVisitorID,NULL))) AS new_users,
COUNT(DISTINCT(IF(current_prev_diff=1,fullVisitorID,NULL))) AS retained_users,
COUNT(DISTINCT(IF(current_prev_diff>1,fullVisitorID,NULL))) AS resurrected_users,
COUNT(DISTINCT(fullVisitorId)) AS total_users
FROM(
SELECT
fullVisitorId,
DATE_TRUNC(PARSE_DATE("%Y%m%d",min_date),MONTH) AS min_month,
DATE_TRUNC(PARSE_DATE("%Y%m%d",date),MONTH) AS current_month,
DATE_TRUNC(PARSE_DATE("%Y%m%d",prev_date),MONTH) AS prev_month,
DATE_DIFF(DATE_TRUNC(PARSE_DATE("%Y%m%d",date),MONTH),DATE_TRUNC(PARSE_DATE("%Y%m%d",prev_date),MONTH),MONTH) AS current_prev_diff
FROM (
SELECT
date,
fullVisitorId,
MIN(date) OVER(PARTITION BY fullVisitorId) AS min_date,
LAG(date) OVER(PARTITION BY fullVisitorId ORDER BY date) as prev_date,
FROM
`bigquery-public-data.google_analytics_sample.ga_sessions_20*`
GROUP BY 1,2)
GROUP BY 1,2,3,4,5)
GROUP BY 1)
GROUP BY 1,2,3,4,5
ORDER BY 1
https://datastudio.google.com/embed/reporting/6a321ffc-1f61-4f7b-a321-d9d3467ecf28/page/PuXuB