Fix division by zero error in customer metrics model and add engagement, churn, and peer comparison metrics
harbor run -t snowflake-labs/dbt-fix-division-by-zeroharbor download snowflake-labs/dbt-fix-division-by-zeroFix a dbt model with data quality issues in customer metrics and add new analytical columns.
The rpt_customer_metrics model has bugs causing invalid values (infinity, NaN). Additionally, we need new metrics for customer segmentation, churn prediction, and peer comparison.
Fix the rpt_customer_metrics.sql model to produce correct output with all required new columns.
/app/dbt_models_duckdb/models/marts/customer/rpt_customer_metrics.sql/app/dbt_models_snowflake/models/marts/customer/rpt_customer_metrics.sqlThis 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:
schema: to $SNOWFLAKE_SCHEMA — do NOT leave it blank. A blank or omitted schema makes Snowflake silently default to PUBLIC, so your models get built in the wrong schema and the verifier cannot find them.The model uses the staging table:
The existing model has problems that need to be resolved:
inf and nan values in calculated columnsThe model produces inf and nan values. Fix all division-by-zero issues using NULLIF or similar patterns.
Also exclude records where customer_id is NULL from the source query.
Add a new column customer_engagement_score (0-100) that combines multiple factors:
| Factor | Weight | Calculation |
|---|---|---|
| Recency | 30% | min(1.0, max(0, (365 - days_since_last_order) / 365)) |
| Frequency | 35% | min(1.0, max(0, orders_per_month / 5.0)) |
| Monetary | 35% | Use PERCENT_RANK() on total_revenue |
Formula: (recency_factor * 0.30 + frequency_factor * 0.35 + monetary_factor * 0.35) * 100
Ensure the score is bounded between 0 and 100. For engagement score inputs, treat NULL values as 0, except days_since_last_order where NULL means no recent order — treat as 365 days (so recency factor = 0). Use COALESCE(days_since_last_order, 365) in the recency calculation.
Add a new column ltv_tier that categorizes customers using waterfall logic (check in order):
| Tier | Criteria |
|---|---|
| vip | revenue_percentile >= 0.95 AND orders_last_90d > 0 AND total_orders >= 10 |
| platinum | revenue_percentile >= 0.90 AND orders_last_180d > 0 |
| gold | revenue_percentile >= 0.75 OR avg_order_value > 500 |
| silver | total_orders >= 3 AND total_revenue > 100 |
| bronze | total_orders >= 1 AND total_revenue > 50 |
| at_risk | total_orders > 0 AND days_since_last_order > 365 |
| inactive | All others |
Use revenue_percentile calculated with PERCENT_RANK() on total_revenue. Treat NULL percentiles as 0.
Add a new column churn_risk_score (0-100) that predicts customer churn:
| Factor | Weight | Calculation |
|---|---|---|
| Days inactive | 40% | min(1.0, max(0, COALESCE(days_since_last_order, 365) / 365)) |
| Declining orders | 30% | 1.0 - min(1.0, max(0, order_frequency_trend)) |
| Declining revenue | 30% | 1.0 - min(1.0, max(0, revenue_velocity_ratio)) |
Formula: (inactive_factor * 0.40 + declining_orders_factor * 0.30 + declining_revenue_factor * 0.30) * 100
Higher score = higher churn risk. Bound between 0-100. Note: NULL handling for churn risk differs from engagement score — customers with unknown days_since_last_order should be treated as maximally inactive: use COALESCE(days_since_last_order, 365) so NULL yields inactive_factor = 1.0 (not 0).
Add columns that compare each customer to their peers:
| Column | Description |
|---|---|
| customer_value_rank | Rank by total_revenue (highest = 1) using DENSE_RANK |
| customer_value_percentile | PERCENT_RANK by total_revenue (higher revenue = higher percentile) |
| above_median_revenue | 1 if total_revenue > median, else 0 |
| Column | Type | Description |
|---|---|---|
| customer_id | varchar | Unique customer identifier |
| total_orders | integer | Total number of orders placed by the customer |
| total_revenue | decimal | Total revenue generated by the customer |
| first_order_date | date | Date of the customer's first order |
| last_order_date | date | Date of the customer's most recent order |
| days_since_last_order | integer | Days since most recent order (relative to the most recent order date in the data) |
| orders_last_30d | integer | Number of orders in the last 30 days (relative to the most recent order date in the data) |
| orders_last_90d | integer | Number of orders in the last 90 days (relative to the most recent order date in the data) |
| orders_last_180d | integer | Number of orders in the last 180 days (relative to the most recent order date in the data) |
| revenue_last_30d | decimal | Revenue generated in the last 30 days (relative to the most recent order date in the data) |
| revenue_last_90d | decimal | Revenue generated in the last 90 days (relative to the most recent order date in the data) |
| avg_order_value | decimal | Average revenue per order |
| orders_per_month | decimal | Average number of orders per month (relative to the most recent order date in the data) |
| revenue_velocity_ratio | decimal | Recent revenue vs historical revenue ratio |
| order_frequency_trend | decimal | Trend metric indicating increase/decrease in order frequency |
| customer_engagement_score | decimal | Customer engagement composite score (0-100) |
| churn_risk_score | decimal | Churn risk prediction score (0-100) |
| customer_value_rank | integer | Rank by total revenue (1 = highest) |
| customer_value_percentile | decimal | Percentile by total revenue |
| above_median_revenue | integer | 1 if above median revenue, 0 otherwise |
| ltv_tier | varchar | Customer lifetime value tier classification |
{% if target.type == 'duckdb' %}...{% else %}...{% endif %}) for syntax that differs between backendsdate_diff() and INTERVAL '30' day; Snowflake uses DATEDIFF() and DATEADD()PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY col) OVER () works in DuckDB but NOT in Snowflake; use a subquery or MEDIAN() for SnowflakeMAX(ordered_at) from the source data as the reference date for all time-based calculations (do NOT use CURRENT_DATE — the data may not extend to the present day)