Create a running balance model using window functions to track customer balances over time
harbor run -t snowflake-labs/dbt-calculate-running-balanceharbor download snowflake-labs/dbt-calculate-running-balanceFinance needs a comprehensive account ledger with advanced risk assessment. Track every transaction, analyze payment patterns, calculate risk metrics, and identify problematic accounts.
/app/dbt_project (create project here)/app/dbt_project (create project here)finance_analyticsThis 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:
/app/dbt_projectprofiles.yml to connect to the database with schema finance_analytics+schema in dbt_project.yml (only set schema in profiles.yml)Orders: Customer purchases
main.ordersORDERS.ORDERSstatus NOT IN ('CANCELLED', 'FAILED')order_id, customer_id, ordered_at, grand_total, statusPayments: Customer payments
main.CUSTOMER_PAYMENTSFINANCE.CUSTOMER_PAYMENTSstatus = 'POSTED'payment_id, customer_id, payment_date, amount, status| Column | Description |
|---|---|
transaction_id |
Order or payment ID |
customer_id |
Customer identifier |
transaction_type |
'ORDER' or 'PAYMENT' |
transaction_date |
Date of transaction |
transaction_amount |
Positive for orders, negative for payments |
previous_balance |
Balance before transaction (0 for first) |
running_balance |
Balance after transaction |
transaction_sequence |
Sequential number per customer |
balance_status |
'CREDIT' if < 0, 'ZERO' if = 0, 'DEBIT' if > 0 |
days_since_last_payment |
Days since most recent prior payment (NULL if none) |
is_high_balance |
TRUE/1 if running_balance > 500 |
balance_change_direction |
'INCREASE', 'DECREASE', or 'NO_CHANGE' |
cumulative_orders |
Running count of orders |
cumulative_payments |
Running count of payments |
is_first_transaction |
TRUE/1 if transaction_sequence = 1 |
balance_trend |
'IMPROVING' if balance decreased, 'WORSENING' if increased, 'STABLE' |
consecutive_orders |
Count of consecutive ORDER transactions ending at this row (resets on PAYMENT) |
consecutive_payments |
Count of consecutive PAYMENT transactions ending at this row (resets on ORDER) |
days_since_first_transaction |
Days between this transaction and customer's first transaction |
| Column | Description |
|---|---|
customer_id |
Customer identifier |
total_orders |
Count of ORDER transactions |
total_payments |
Count of PAYMENT transactions |
total_order_amount |
Sum of order amounts |
total_payment_amount |
Sum of payment amounts (absolute value) |
final_balance |
Customer's current balance |
final_status |
Balance status of final transaction |
avg_days_between_payments |
Average days between consecutive payments (NULL if < 2 payments) |
max_balance_reached |
Highest running_balance recorded |
account_health_score |
1-5: 5 if balance <= 0, 4 if <= 100, 3 if <= 300, 2 if <= 500, 1 if > 500 |
longest_order_streak |
Maximum consecutive orders without payment |
account_age_days |
Days between first and last transaction |
| Column | Description |
|---|---|
customer_id |
Customer identifier |
final_balance |
Current balance |
payment_frequency |
'HIGH' if avg_days_between_payments < 15, 'MEDIUM' if < 45, 'LOW' if >= 45 or NULL |
balance_volatility |
'HIGH' if max_balance_reached > 3 * final_balance, 'MEDIUM' if > 1.5x, 'LOW' otherwise |
streak_risk |
'HIGH' if longest_order_streak >= 5, 'MEDIUM' if >= 3, 'LOW' otherwise |
overall_risk_score |
1-10: sum of (payment_freq: HIGH=3/MED=2/LOW=1) + (volatility: HIGH=4/MED=2/LOW=1) + (streak: HIGH=3/MED=2/LOW=1) |
risk_category |
'CRITICAL' if overall >= 9, 'HIGH' if >= 7, 'MEDIUM' if >= 5, 'LOW' otherwise |
DATEDIFF('day', start, end) for Snowflake and date subtraction for DuckDB when calculating day differences