Marketing Mix Modeling - Media effectiveness, saturation curves, and budget optimization
harbor run -t snowflake-labs/dbt-harbor-marketing-mix-modelharbor download snowflake-labs/dbt-harbor-marketing-mix-modelBuild a Marketing Mix Model (MMM) using dbt for a D2C e-commerce company spending $500K/month across multiple marketing channels. The model must:
This task supports both DuckDB and Snowflake. Run echo $DB_TYPE in a shell BEFORE you write any code or spawn any subagents — the live env determines which backend the verifier runs against. Do NOT assume a default from this prose. Both /app/dbt_models_duckdb/ and /app/dbt_models_snowflake/ exist on disk; the verifier only checks the project matching the live $DB_TYPE.
DB_TYPE=duckdb$DUCKDB_PATH (default: /app/database/retail.duckdb)DB_TYPE=snowflakeSNOWFLAKE_ACCOUNTSNOWFLAKE_USERSNOWFLAKE_PASSWORDSNOWFLAKE_DATABASE - The clone database to useSNOWFLAKE_SCHEMASNOWFLAKE_WAREHOUSESNOWFLAKE_ROLE (optional)Note: For Snowflake, the entrypoint automatically creates a clone database and sets SNOWFLAKE_DATABASE. The clone is destroyed when the task completes.
You must configure dbt to connect to the database:
profiles.yml in the dbt project directory with profile name retail_dw_masterDBT_PROFILES_DIR environment variable to the directory containing profiles.ymlConfigure with type: duckdb and the database path from $DUCKDB_PATH.
Configure with type: snowflake using password authentication:
CRITICAL: Use the existing dbt project based on the database backend:
/app/dbt_models_duckdb//app/dbt_models_snowflake/dbt_project.ymldbt deps --profiles-dir .dbt run --profiles-dir . --select model_nameIMPORTANT for Snowflake: Always use --select with specific model names. Never run bare dbt run as the pre-built project has thousands of models.
Unlike attribution models (which track individual customer touchpoints), MMM uses econometric modeling to understand:
1. Baseline Sales - What you'd sell organically without any marketing
2. Incremental Sales - Additional sales driven by marketing spend
3. Saturation Effects - Diminishing returns as spend increases
4. Adstock/Carryover Effects - Marketing impact persists over time
5. Cross-Channel Effects - Channels interact with each other
The database contains enterprise retail data. Explore it to find the data you need.
Exploration Commands:
-- List all tables
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_schema != 'information_schema';
-- Examine table structure
DESCRIBE schema_name.table_name;
-- Preview data
SELECT * FROM table_name LIMIT 10;
What you need to discover:
Separate organic sales from marketing-driven sales:
Requirements:
Implementation Approach:
baseline_revenue = base_demand * baseline_trend * day_factor * month_factor * holiday_factorAdstock Model:
Adstock_t = Spend_t + (decay_rate * Adstock_t-1)Saturation Function (Hill Curve):
Saturated_Impact = Spend^alpha / (Spend^alpha + halfpoint^alpha)
Channel Saturation Parameters:
Requirements:
halfpoint_daily = halfpoint_monthly / 30saturated_spend = Saturated_Impact * adstock_spendsaturation_efficiency_pct = 100 * saturated_spend / NULLIF(adstock_spend, 0) (cap 0-100)Channel Response Coefficients (Revenue per $1 saturated spend):
Classification Rules:
Constraints:
Break down daily sales into components:
Actual_Sales = Baseline + Email_Incremental + Paid_Search_Incremental +
Social_Incremental + Display_Incremental + TV_Incremental + Residual
Create the following dbt models organized by layer:
main_staging)Create staging models in models/staging/mmm/:
1. stg_mmm__daily_sales
metric_date, channel_id, channel_name, total_revenue, order_count, units_sold2. stg_mmm__marketing_spend
metric_date, channel_id, channel_name, spend_amount, impressions, clicks3. stg_mmm__calendar
metric_date, day_of_week, day_name, month, quarter, is_weekend, is_holiday, holiday_name4. stg_mmm__channel_mapping
channel_id, channel_name, channel_type, decay_rate, saturation_halfpoint, saturation_shape, saturation_alphaChannel Mapping Rule (Required): Normalize all channel names to exactly 5 canonical values: Email, Paid Search, Social Media, Display Ads, TV/Video. Use case-insensitive matching.
main_intermediate)Create intermediate models in models/intermediate/mmm/:
5. int_mmm__baseline_sales
metric_date, baseline_revenue, baseline_trend, day_of_week_factor, month_factor, holiday_lift_factor, is_weekend, is_holiday6. int_mmm__adstock_transformed
metric_date, channel_id, channel_name, raw_spend, decay_rate, adstock_spend, cumulative_adstock_7d, cumulative_adstock_30d7. int_mmm__saturation_curves
metric_date, channel_id, channel_name, adstock_spend, saturation_halfpoint, saturation_alpha, saturated_spend, saturation_efficiency_pct, saturation_status8. int_mmm__incremental_revenue
metric_date, channel_id, channel_name, saturated_spend, channel_coefficient, incremental_revenue, raw_roas, effective_roas9. int_mmm__daily_decomposition
metric_date, actual_sales, baseline_sales, email_incremental, paid_search_incremental, social_incremental, display_incremental, tv_incremental, total_incremental, model_predicted_sales, residual, residual_pctmain_marts)Create mart models in models/marts/mmm/:
10. fct_mmm_performance
metric_date, channel_id, channel_name, raw_spend, adstock_spend, saturated_spend, incremental_revenue, raw_roas, effective_roas, saturation_efficiency_pct, contribution_to_total_sales_pct11. rpt_mmm_channel_effectiveness
channel_id, channel_name, channel_type, total_raw_spend, total_incremental_revenue, avg_saturation_efficiency_pct, raw_roas, marginal_roas, saturation_status, rank_by_effectiveness, recommended_action12. rpt_mmm_budget_optimization
channel_id, channel_name, current_budget, current_budget_pct, current_marginal_roas, recommended_budget, recommended_budget_pct, budget_change, budget_change_pct, expected_revenue_change13. rpt_mmm_decomposition
metric_date, actual_sales, baseline_sales, baseline_pct, email_incremental, email_pct, paid_search_incremental, paid_search_pct, social_incremental, social_pct, display_incremental, display_pct, tv_incremental, tv_pct, total_marketing_incremental, marketing_pct, residual, residual_pctPercentage column formulas (all on a 0–100 scale; this rpt_mmm_decomposition table only):
baseline_pct = 100.0 * baseline_sales / NULLIF(actual_sales, 0)email_pct = 100.0 * email_incremental / NULLIF(actual_sales, 0)paid_search_pct = 100.0 * paid_search_incremental / NULLIF(actual_sales, 0)social_pct = 100.0 * social_incremental / NULLIF(actual_sales, 0)display_pct = 100.0 * display_incremental / NULLIF(actual_sales, 0)tv_pct = 100.0 * tv_incremental / NULLIF(actual_sales, 0)marketing_pct = 100.0 * total_marketing_incremental / NULLIF(actual_sales, 0)residual_pct = 100.0 * residual / NULLIF(actual_sales, 0)baseline_pct + marketing_pct + residual_pct should sum to ~100 per row.
14. rpt_mmm_saturation_analysis
channel_id, channel_name, current_spend_level, saturation_halfpoint, spend_vs_halfpoint_ratio, current_efficiency_pct, optimal_spend_level, spend_gap, is_oversaturated15. rpt_mmm_summary
time_period, total_sales, baseline_sales, baseline_pct, total_marketing_incremental, marketing_pct, total_marketing_spend, overall_marketing_roas, avg_saturation_efficiency_pct, top_performing_channel, most_saturated_channelBaseline Calculation:
Adstock Formula: Adstock(t) = Spend(t) + decay_rate * Adstock(t-1)
Saturation Formula (Hill Curve):
Saturated = (Adstock^alpha) / (Adstock^alpha + halfpoint^alpha)
Saturated_Spend = Saturated * Adstock
Incremental Revenue: Incremental(channel, date) = channel_coefficient * saturated_spend(channel, date)
-- For staging models:
{{ config(materialized='view', schema='staging') }}
-- For intermediate models:
{{ config(materialized='view', schema='intermediate') }}
-- For marts models:
{{ config(materialized='table', schema='marts') }}
The dbt project has target_schema='main'. When you set schema='staging', dbt creates main_staging.
{{ source() }} for raw tables and {{ ref() }} for model dependencies