Query editor
with trades as (selectblock_date as report_date,trader, lag(block_date, 1) over(partition by trader order by block_date) last_trade_datefrom query_3004403 -- Trades.group by 1,2 -- No duplicates.), trader_first_login as(selecttrader, min(report_date) first_trade_datefrom tradesgroup by 1), date_generate as(selectt.first_trade_date, d. report_date, t.traderfromtrader_first_login tjoin(unnest(sequence(date('2023-08-10'), current_date, interval '1' day)) as d(report_date))on true), date_join_user_trade as (selectg.report_date, g.first_trade_date, if(d.trader is not null, true, false) as is_active, count(d.trader) over(partition by g.trader order by to_unixtime(g.report_date)/86400 ASC RANGE BETWEEN 6 PRECEDING AND CURRENT ROW) is_active_last_7d, count(d.trader) over(partition by g.trader order by to_unixtime(g.report_date)/86400 ASC RANGE BETWEEN 29 PRECEDING AND CURRENT ROW) is_active_last_30d, count_if(d.last_trade_date IS NULL) OVER (PARTITION BY g.trader ORDER BY to_unixtime(g.report_date)/86400 ASC RANGE BETWEEN 2 PRECEDING AND CURRENT ROW) is_inactive_last_3d, count_if(d.last_trade_date IS NULL) OVER (PARTITION BY g.trader ORDER BY to_unixtime(g.report_date)/86400 ASC RANGE BETWEEN 6 PRECEDING AND CURRENT ROW) is_inactive_last_7d, count_if(d.last_trade_date IS NULL) OVER (PARTITION BY g.trader ORDER BY to_unixtime(g.report_date)/86400 ASC RANGE BETWEEN 29 PRECEDING AND CURRENT ROW) is_inactive_last_30dfrom date_generate gleft join trades d on g.report_date = d.report_date and g.trader = d.trader), daily_overview_report as (selectreport_date, count_if(report_date = first_trade_date) as USER_NRU, count_if(is_active = true) as USER_DAU, count_if(is_active_last_7d > 0) USER_WAU, count_if(is_active_last_30d > 0) USER_MAU, count_if(DATE_DIFF('day', first_trade_date, report_date) = 1 AND is_active = true) USER_return_1d, count_if(DATE_DIFF('day', first_trade_date, report_date) = 3 AND is_active = true) USER_return_3d, count_if(DATE_DIFF('day', first_trade_date, report_date) = 7 AND is_active = true) USER_return_7d, count_if(DATE_DIFF('day', first_trade_date, report_date) = 30 AND is_active = true) USER_return_30d, count_if(is_inactive_last_3d > 2) USER_inactive_3d, count_if(is_inactive_last_7d > 6) USER_inactive_7d, count_if(is_inactive_last_30d > 29) USER_inactive_30dfrom date_join_user_tradegroup by report_date)SELECT*,LEAD(USER_return_1d, 1) OVER (ORDER BY report_date ASC) * 1.00 / NULLIF(USER_NRU, 0) as USER_R1,LEAD(USER_return_3d, 3) OVER (ORDER BY report_date ASC) * 1.00 / NULLIF(USER_NRU, 0) as USER_R3,LEAD(USER_return_7d, 7) OVER (ORDER BY report_date ASC) * 1.00 / NULLIF(USER_NRU, 0) as USER_R7,LEAD(USER_return_30d, 30) OVER (ORDER BY report_date ASC) * 1.00 / NULLIF(USER_NRU, 0) as USER_R30,USER_DAU - USER_NRU as OLD_USER