Implement RFM (Recency, Frequency, Monetary) customer segmentation using enterprise retail data
harbor run -t snowflake-labs/dbt-rfm-customer-segmentationharbor download snowflake-labs/dbt-rfm-customer-segmentationBuild a dbt project that implements RFM (Recency, Frequency, Monetary) customer segmentation using the enterprise retail data warehouse.
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. Reference dbt projects exist at /app/dbt_models_duckdb/ and /app/dbt_models_snowflake/ for inspection only. Write your dbt project at /app/dbt_project — outputs in the reference directories are not graded.
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 dbt_projectDBT_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:
2024-12-01 as the reference date for recency calculations2023-01-01 to 2024-11-30 (23 months)/app/dbt_projectrfm_analyticsConfiguration Note: Set the schema only in profiles.yml. Do not add +schema in dbt_project.yml (dbt concatenates them, causing schema name issues).
Reference the raw tables in the database:
Raw orders table in schema main: main.orders
order_id, customer_id, ordered_at, grand_total, statusRaw customers table in schema main: main.customers
customer_id, customer_number, first_name, last_name, statusCreate sources that point to these raw tables in the main schema, and build the following staging models (use these exact names):
stg_orders__ordersstg_customer__customersmodels/staging/)Create source definitions to access the existing tables in the database.
models/intermediate/)int_rfm_metrics.sql - Calculate RFM metrics per customer:
status NOT IN ('CANCELLED', 'RETURNED', 'FAILED')recency_days: Days between analysis date (2024-12-01) and customer's most recent orderfrequency: Count of orders in the analysis periodmonetary: Total grand_total in the analysis periodmodels/marts/)rfm_scores.sql - Assign RFM scores using NTILE:
NTILE(5) to assign scores 1-5 for each dimensioncustomer_id) in all NTILE windows to ensure reproducible tie-breaking.rfm_segments.sql - Final segmentation model with columns:
customer_idcustomer_name (concatenate first_name and last_name from customers)recency_daysfrequencymonetaryr_score (1-5)f_score (1-5)m_score (1-5)rfm_score (concatenated string like "555", "321", etc.)rfm_segment - Assign segment based on RFM score combination:
The rfm_segments model must:
When multiple segment conditions match, use this priority order (first match wins):