Fix a CAC payback waterfall report model using dbt + DuckDB, producing analytics.rpt_cac_payback_waterfall_fixed
harbor run -t snowflake-labs/dbt-fix-cac-payback-waterfallharbor download snowflake-labs/dbt-fix-cac-payback-waterfallYou're an analytics engineer rebuilding a CAC payback waterfall report. The warehouse already contains a reference dbt project at /app/dbt_transforms with various models materialized. Marketing spend data exists in the warehouse -- find the right source table by exploring available models.
Create a dbt project at /app/dbt_project and implement:
models/marts/marketing/rpt_cac_payback_waterfall_fixed.sqlThis task supports two database backends:
/app/database/retail.duckdb. The dbt project is at /app/dbt_models_duckdb/.SNOWFLAKE_ACCOUNT, SNOWFLAKE_USER, SNOWFLAKE_PASSWORD, SNOWFLAKE_DATABASE, SNOWFLAKE_SCHEMA, SNOWFLAKE_WAREHOUSE, SNOWFLAKE_ROLE). The dbt project is at /app/dbt_models_snowflake/.Check the DB_TYPE environment variable to determine which backend is active.
Create a profiles.yml with profile name retail_dw_master:
type: duckdb and path pointing to the database file.type: snowflake and use the environment variables for connection settings (password authentication). Set the profile's schema: to $SNOWFLAKE_SCHEMA — do NOT leave it blank (a blank schema makes Snowflake default to PUBLIC, so models land in the wrong schema and the verifier cannot find them).Materialize analytics.rpt_cac_payback_waterfall_fixed with columns:
campaign_id (STRING)channel_key (INTEGER)cohort_date (DATE) -- the first date we observe spend for the campaigndays_since_cohort (INTEGER) -- 0 for the cohort datedaily_spend (NUMERIC(12,2))daily_revenue_attributed (NUMERIC(12,2))cumulative_spend (NUMERIC(12,2))cumulative_revenue (NUMERIC(12,2))payback_ratio (NUMERIC(10,4)) -- compute a payback ratio showing how much of the investment has been recoupedis_paid_back (BOOLEAN) -- true when the campaign has recouped its investmentcampaign_id + days_since_cohort).channel_key in the output. If the source data has a campaign appearing across multiple channel_key values, pick the channel_key with the highest total spend_amount for that campaign. Aggregate all spend and revenue across channel keys into a single campaign row before building the waterfall.cumulative_spend and cumulative_revenue must be non-decreasing within each campaign.{% if target.type == 'snowflake' %}) for database-specific syntax/app/dbt_transforms.analytics.