Skip to content

Commit

Permalink
fix: dim busting spree
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastien Verreault committed Jan 8, 2025
1 parent ba5d5e4 commit fd6e250
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ WITH terms_and_disbursal AS (
, de.disbursal_concluded_event_recorded_at AS disbursal_start_date
FROM ${ref("fct_credit_facility_events")} cfe
RIGHT JOIN ${ref("fct_credit_facility_disbursal_events")} de USING (event_key)
LEFT JOIN ${ref("dim_credit_facility_events_terms_annual_rate")} ar USING (terms_annual_rate_key)
LEFT JOIN ${ref("dim_credit_facility_events_terms_accrual_interval_type")} it USING (terms_accrual_interval_type_key)
LEFT JOIN ${ref("dim_credit_facility_events_terms_duration_type")} dt USING (terms_duration_type_key)
LEFT JOIN ${ref("dim_credit_facility_events_terms_duration_value")} dv USING (terms_duration_value_key)
-- LEFT JOIN ${ref("dim_credit_facility_events_terms_annual_rate")} ar USING (terms_annual_rate_key)
-- LEFT JOIN ${ref("dim_credit_facility_events_terms_accrual_interval_type")} it USING (terms_accrual_interval_type_key)
-- LEFT JOIN ${ref("dim_credit_facility_events_terms_duration_type")} dt USING (terms_duration_type_key)
-- LEFT JOIN ${ref("dim_credit_facility_events_terms_duration_value")} dv USING (terms_duration_value_key)
WHERE disbursal_concluded_event_recorded_at_date_key != 19000101
AND terms_accrual_interval_type = 'end_of_month'
), projections AS (
Expand Down Expand Up @@ -112,7 +112,7 @@ WITH terms_and_disbursal AS (
FROM projected_payment_data
), projected_payment AS (
SELECT
customer_id_key
customer_id
, event_key
, idx AS disbursal_idx
, credit_facility_start_date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ WITH terms_and_disbursal AS (
, de.disbursal_concluded_event_recorded_at AS disbursal_start_date
FROM ${ref("fct_credit_facility_events")} cfe
RIGHT JOIN ${ref("fct_credit_facility_disbursal_events")} de USING (event_key)
LEFT JOIN ${ref("dim_credit_facility_events_terms_annual_rate")} ar USING (terms_annual_rate_key)
LEFT JOIN ${ref("dim_credit_facility_events_terms_accrual_interval_type")} it USING (terms_accrual_interval_type_key)
LEFT JOIN ${ref("dim_credit_facility_events_terms_duration_type")} dt USING (terms_duration_type_key)
LEFT JOIN ${ref("dim_credit_facility_events_terms_duration_value")} dv USING (terms_duration_value_key)
WHERE disbursal_concluded_event_recorded_at_date_key != 19000101
AND terms_accrual_interval_type = 'end_of_month'
), projections AS (
Expand Down Expand Up @@ -105,7 +101,7 @@ WITH terms_and_disbursal AS (
FROM projected_payment_data
), projected_payment AS (
SELECT
customer_id_key
customer_id
, event_key
, idx AS disbursal_idx
, credit_facility_start_date
Expand All @@ -130,7 +126,7 @@ WITH terms_and_disbursal AS (
WHERE days_from_now >= 0
), grouped AS (
SELECT
customer_id_key
customer_id
, event_key
, disbursal_idx
, credit_facility_start_date
Expand All @@ -148,7 +144,7 @@ WITH terms_and_disbursal AS (
, SUM(projected_amount_in_cents) AS projected_amount_in_cents
FROM projected_payment
GROUP BY
customer_id_key
customer_id
, event_key
, disbursal_idx
, credit_facility_start_date
Expand All @@ -165,7 +161,7 @@ WITH terms_and_disbursal AS (
ORDER BY days_from_now
), arrayed AS (
SELECT
customer_id_key
customer_id
, event_key
, disbursal_idx
, credit_facility_start_date
Expand All @@ -183,7 +179,7 @@ WITH terms_and_disbursal AS (
, ARRAY_AGG(projected_amount_in_cents) AS cash_flows
FROM grouped
GROUP BY
customer_id_key
customer_id
, event_key
, disbursal_idx
, credit_facility_start_date
Expand Down Expand Up @@ -229,9 +225,9 @@ WITH terms_and_disbursal AS (

-- ORDER BY order_by

select * from with_risk order by customer_id_key, event_key, disbursal_idx
-- select * from arrayed order by customer_id_key, event_key, disbursal_idx
-- select * from grouped order by customer_id_key, event_key, disbursal_idx
select * from with_risk order by customer_id, event_key, disbursal_idx
-- select * from arrayed order by customer_id, event_key, disbursal_idx
-- select * from grouped order by customer_id, event_key, disbursal_idx

-- TODO: need to add disbursal cashflows: projected_disbursal
-- TODO: fix float64, numeric, bignumeric
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ WITH value_approved_cf AS (
SELECT
cfe.event_key
, 5.53 AS bench_mark -- TODO get from proper source
, ar.terms_annual_rate
, cfe.terms_annual_rate
, COALESCE(de.amount, 0) AS disbursal_amount_in_cents
, facility AS credit_facility_limit_in_cents
FROM ${ref("fct_credit_facility_events")} cfe
LEFT JOIN ${ref("fct_credit_facility_disbursal_events")} de USING (event_key)
LEFT JOIN ${ref("dim_credit_facility_events_terms_annual_rate")} ar USING (terms_annual_rate_key)
WHERE approval_process_concluded_approved
AND facility > 0
), breakeven_by_cf AS (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,21 @@ config {
WITH total_customers AS (
SELECT
COUNT(DISTINCT customer_id) AS value
FROM ${ref("dim_credit_facility_events_customer_id")}
FROM ${ref("fct_credit_facility_events")}
), approved_cf AS (
SELECT
COUNT(DISTINCT customer_id) AS value
FROM ${ref("fct_credit_facility_events")}
LEFT JOIN ${ref("dim_credit_facility_events_customer_id")} USING (customer_id_key)
WHERE approval_process_concluded_approved
), activated_cf AS (
SELECT
COUNT(DISTINCT customer_id) AS value
FROM ${ref("fct_credit_facility_events")}
LEFT JOIN ${ref("dim_credit_facility_events_customer_id")} USING (customer_id_key)
WHERE activated_recorded_at_date_key != 19000101
), disbursed_cf AS (
SELECT
COUNT(DISTINCT customer_id) AS value
FROM ${ref("fct_credit_facility_events")}
LEFT JOIN ${ref("dim_credit_facility_events_customer_id")} USING (customer_id_key)
RIGHT JOIN ${ref("fct_credit_facility_disbursal_events")} USING (event_key)
WHERE disbursal_concluded_event_recorded_at_date_key != 19000101
)
Expand Down

0 comments on commit fd6e250

Please sign in to comment.