Build a customer risk scoring model based on payment behavior, returns, and chargebacks
harbor run -t snowflake-labs/dbt-customer-risk-scoringharbor download snowflake-labs/dbt-customer-risk-scoringThe risk management team needs a customer risk scoring model to identify potentially risky customers based on their order behavior. Your goal is to build a dbt model that scores every customer by analyzing patterns in their order history.
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.
/app/dbt_project2024-12-01 as the reference daterisk_analyticsmainYou must configure dbt to connect to the database:
profiles.yml with profile name dbt_projecttype: duckdb and the database pathrisk_analytics/app/dbt_models_snowflake as the baseln -sfn /app/dbt_models_snowflake /app/dbt_projectprofiles.yml in the project directory with profile name retail_dw_mastertype: snowflake using password authentication:
mainBuild from customer order history available in the warehouse. You will need to explore the available tables and their schemas to understand the data model. For DuckDB, look in the main schema. For Snowflake, explore the available schemas to find the relevant order and customer tables.
Pay attention to column types, as flag columns (e.g., chargeback_flag) may be stored differently across backends.
Customers accumulate risk points based on the following criteria:
payment_status = 'UNPAID'status = 'RETURNED' (count from the orders table, not a separate returns table)chargeback_flag = TRUEThe total risk score is calculated as:
risk_score = (late_payment_count * 10) + (return_count * 5) + (chargeback_count * 20)
Assign risk tiers based on the score:
'LOW': risk_score < 20'MEDIUM': risk_score >= 20 AND risk_score < 50'HIGH': risk_score >= 50 AND risk_score < 100'CRITICAL': risk_score >= 100Create staging models to clean and prepare the source data:
stg_orders_risk - Orders staging model with cleaned/trimmed string fieldsstg_customers_risk - Customers staging model with trimmed name fieldsDefine appropriate sources for each schema.
customer_risk_scores.sql - Final risk scoring model with columns:
customer_idcustomer_name (concatenate first_name and last_name)late_payment_count - Number of unpaid ordersreturn_count - Number of returned orderschargeback_count - Number of orders with chargebacksrisk_score - Total risk points (see formula above)risk_tier - Category based on score boundaries (see tiers above)trim(), coalesce(), cast() which work on both backendsUPPER(CAST(flag AS VARCHAR)) IN ('TRUE', '1', 'T', 'Y', 'YES') for Snowflake compatibility)--select specifying model names explicitly