Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions dbt/models/marts/facts/_fact_payments.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
version: 2

models:
- name: fact_payments
description: >
Payments fact table.
Grain: 1 row per payment_key (surrogate key of order_id + payment_sequential).
order_purchase_date is sourced via LEFT JOIN with int_orders_enriched on order_id.
Tradeoff: using int_orders_enriched avoids a mart-to-mart dependency
that would arise from joining fact_orders directly.
config:
tags: ['marts', 'fact', 'payments']
columns:
- name: payment_key
description: Surrogate key — grain of this model (order_id + payment_sequential)
tests:
- not_null
- unique

- name: order_id
description: FK to orders
tests:
- not_null
- relationships:
to: ref('int_orders_enriched')
field: order_id
config:
severity: warn

- name: payment_sequential
description: Payment sequence number within the order
tests:
- not_null

- name: payment_type
description: Raw payment method
tests:
- not_null

- name: payment_type_clean
description: Cleaned payment method (lowercase, trimmed)

- name: payment_installments
description: Number of installments
tests:
- not_null

- name: payment_value
description: Payment amount in BRL
tests:
- not_null

- name: order_purchase_date
description: >
Purchase date from int_orders_enriched via LEFT JOIN on order_id.
Can be null if the order has no matching record in the intermediate layer.

- name: is_negative_value
description: Flag for payments with value < 0

- name: is_invalid_installments
description: Flag for installments < 1

- name: is_excessive_installments
description: Flag for card payments with installments > 24
37 changes: 37 additions & 0 deletions dbt/models/marts/facts/fact_payments.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{{
config(
materialized='table',
tags=['marts', 'fact', 'payments']
)
}}

with payments as (
select * from {{ ref('stg_payments') }}
),

orders as (
select
order_id,
cast(purchased_at as date) as order_purchase_date
from {{ ref('int_orders_enriched') }}
),

final as (
select
p.payment_key,
p.order_id,
p.payment_sequential,
p.payment_type,
p.payment_type_clean,
p.payment_installments,
p.payment_value,
p.is_negative_value,
p.is_invalid_installments,
p.is_excessive_installments,
o.order_purchase_date
from payments p
left join orders o
on p.order_id = o.order_id
)

select * from final
Loading