Query editor
-- =====================================================-- CUMULATIVE TVL (STACKED AREA CHART)-- Calculates Absolute TVL from genesis to prevent drop-offs when filtering dates-- =====================================================WITH raw_history AS (-- 1. Get ALL data from genesis up to the End Date.-- DO NOT filter by Start Date yet, otherwise the running total resets to 0!SELECTday,blockchain,asset_type,net_tvl_usdFROM dune.igboweefe.result_private_activity_mvWHERE day < COALESCE(TRY_CAST('{{End Date}}' AS TIMESTAMP), CURRENT_TIMESTAMP)AND ('{{Blockchain}}' = 'All' OR blockchain = '{{Blockchain}}')),daily_deduped AS (-- 2. Deduplicate the operations overlap (Mint vs Send)SELECTday,blockchain,asset_type,MAX(net_tvl_usd) AS daily_net_flow_usdFROM raw_historyGROUP BY 1, 2, 3),daily_chain_totals AS (-- 3. Sum all assets together per day, per chainSELECTday,blockchain,SUM(daily_net_flow_usd) AS chain_daily_net_flowFROM daily_dedupedGROUP BY 1, 2),cumulative_tvl AS (-- 4. Calculate the true running bank balance using a Window FunctionSELECTday,blockchain,SUM(chain_daily_net_flow) OVER (PARTITION BY blockchain ORDER BY day ASC) AS absolute_tvl_usdFROM daily_chain_totals)-- 5. Finally, filter the visual output to match the dashboard's display windowSELECTCAST(day AS DATE) AS "DAY",blockchain AS "BLOCKCHAIN",absolute_tvl_usd AS "TVL (USD)"FROM cumulative_tvlWHERE day >= COALESCE(TRY_CAST('{{Start Date}}' AS TIMESTAMP), TIMESTAMP '2024-01-01 00:00:00')ORDER BY "DAY" DESC, "TVL (USD)" DESC;