Build comprehensive cohort retention matrix with revenue tracking using dbt
harbor run -t snowflake-labs/cohort-retention-matrixharbor download snowflake-labs/cohort-retention-matrixThe Marketing team needs a customer cohort retention analysis to understand how customers from different acquisition channels behave over time.
Create a dbt model at models/marts/marketing/cohort_retention_matrix.sql. Configure it to use schema marketing_analytics.
Note: The dbt project prefixes custom schemas with main_, so marketing_analytics becomes main_marketing_analytics in the database.
/app/dbt_models_duckdb/models/marts/marketing/cohort_retention_matrix.sql/app/dbt_models_snowflake/models/marts/marketing/cohort_retention_matrix.sqlRun dbt deps before running models.
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:
The model must produce exactly these columns:
| Column | Description |
|---|---|
| cohort_month | Signup month in YYYY-MM format |
| acquisition_source | Marketing channel, 'UNKNOWN' for missing values |
| cohort_size | Count of customers who signed up (including those who never ordered) |
| never_ordered_count | Count of customers who never placed any valid order |
| m0_retained | Count of customers who ordered in month 0 (same calendar month as signup) |
| m0_revenue | Total revenue from month 0 orders |
| m0_rate | Percentage of cohort retained in month 0, rounded to 2 decimals (e.g., 45.23) |
| m1_retained | Count of customers who ordered in month 1 |
| m1_revenue | Total revenue from month 1 orders |
| m1_rate | Percentage of cohort retained in month 1, rounded to 2 decimals |
| m1_cumulative | Count of customers who ordered in month 0 OR month 1 (not double-counted) |
| m2_retained | Count of customers who ordered in month 2 |
| m2_revenue | Total revenue from month 2 orders |
| m2_rate | Percentage of cohort retained in month 2, rounded to 2 decimals |
| m2_cumulative | Count of customers who ordered in month 0, 1, OR 2 (not double-counted) |
| m3_retained | Count of customers who ordered in month 3 |
| m3_revenue | Total revenue from month 3 orders |
| m3_rate | Percentage of cohort retained in month 3, rounded to 2 decimals |
| m3_cumulative | Count of customers who ordered in months 0-3 (not double-counted) |
| m6_retained | Count of customers who ordered in month 6 |
| m6_revenue | Total revenue from month 6 orders |
| m6_rate | Percentage of cohort retained in month 6, rounded to 2 decimals |
| m6_cumulative | Count of customers who ordered in months 0-6 (not double-counted) |
| m12_retained | Count of customers who ordered in month 12 |
| m12_revenue | Total revenue from month 12 orders |
| m12_rate | Percentage of cohort retained in month 12, rounded to 2 decimals |
| m12_cumulative | Count of customers who ordered in months 0-12 (not double-counted) |
| early_churned_count | Count of customers who ordered in month 0 but never ordered again after month 0 |
| cohort_ltv | Total lifetime revenue divided by cohort_size, rounded to 2 decimals |
| avg_days_to_second_purchase | Average days between 1st and 2nd order for customers with 2+ orders, NULL if none in cohort |
Cohort definition: Group customers by their account creation month (from CUSTOMER.CUSTOMERS). This is NOT based on first order date.
Cohort size: Includes ALL customers who signed up in that month, even if they never placed an order.
Month calculation: Month N means exactly N calendar months after the signup month. A customer who signed up January 15th and ordered February 3rd counts as month 1 (not month 0).
Valid orders: Only count orders with STATUS = 'COMPLETED'. Exclude orders where TEST_ORDER_FLAG, SAMPLE_ORDER_FLAG, or INTERNAL_ORDER_FLAG is true.
Channel filtering: Exclude any acquisition_source that has fewer than 10 total customers across all cohorts.
Revenue column: Use GRAND_TOTAL from the orders table. Default to 0 when no orders exist.
Retained count: Count distinct customers with at least one valid order in that specific month offset.
Retention rate: Calculate as (retained_count / cohort_size) * 100, rounded to 2 decimal places. Return 0.00 if cohort_size is 0.
Cumulative retention: Count distinct customers who ordered in ANY month from 0 up to and including that month. A customer ordering in multiple months counts once.
Never ordered: Count customers in the cohort who have zero valid orders ever.
Early churned: Count customers who placed at least one order in month 0 but have no orders in any month after month 0.
Sorting: Order by cohort_month ascending, then acquisition_source ascending.
Find the appropriate dbt source references by examining the existing source definitions in the project.