How to make a model containing multiple fact and dimension tables incremental?

The problem I’m having

I want to make a model containing multiple fact and dimension tables incremental, so that only new or updated data is processed instead of running the entire query every time.

The context of why I’m trying to do this

I’m working on a Medallion architecture in Snowflake and want to optimize data processing and warehouse usage. The model combines multiple fact and dimension tables, and I want to understand how to handle incremental processing while maintaining data consistency.

What I’ve already tried

I tries dynamic table along with stream based task (have some limitations) but to make platform independence, wanted to use dbt.

Some example code or error messages

I can share the SQL query for the model, which includes multiple fact and dimension tables, to explore how it can be made incremental.

Model structure

The model combines data from three sources using UNION ALL:

  1. Online transactions: Joins a trip/waybill fact table, a ticket fact table, a passenger/ride fact table, and a transaction/payment fact table.

  2. Mobile transactions: Reads booking data from a separate source, including semi-structured JSON fields.

  3. Manual transactions: Joins a trip/waybill table with manual ticket reports and a denomination/reference table.

The combined data is aggregated by transaction_date, location_name, fare_category, and tax_rate.

The final output includes passenger counts, ticket counts, revenue, luggage revenue, toll revenue, and calculated surcharges.


### Simplified SQL structure

```sql
--CREATE OR REPLACE DYNAMIC TABLE target_schema.daily_sales_summary
--    TARGET_LAG = '1 day'
--    REFRESH_MODE = FULL
--   AS

WITH online_transactions AS (
    SELECT
        CAST(t.trip_timestamp AS DATE) AS transaction_date,
        t.location_id,
        t.trip_id,
        tk.ticket_id,
        r.passenger_category,
        r.passenger_count,
        r.paid_amount,
        p.tax_amount,
        d.fare_category,
        d.fare_value
    FROM source_schema.trip_fact t
    JOIN source_schema.ticket_fact tk
        ON t.trip_id = tk.trip_id
    JOIN source_schema.ride_fact r
        ON r.ticket_id = tk.ticket_id
    JOIN source_schema.payment_fact p
        ON p.ticket_id = tk.ticket_id
    LEFT JOIN source_schema.fare_dimension d
        ON tk.fare_id = d.fare_id
    WHERE t.status = 'COMPLETED'
),

online_aggregated AS (
    SELECT
        transaction_date,
        location_id,
        fare_category,
        SUM(passenger_count) AS passenger_count,
        SUM(paid_amount) AS revenue
    FROM online_transactions
    GROUP BY 1, 2, 3
),

mobile_transactions AS (
    SELECT
        CAST(booking_timestamp AS DATE) AS transaction_date,
        'MOBILE' AS location_name,
        booking_details:fare::NUMBER AS fare_category,
        booking_details:passenger_count::NUMBER AS passenger_count,
        booking_details:fare::NUMBER
            * booking_details:passenger_count::NUMBER AS revenue
    FROM mobile_source.booking_fact
    WHERE booking_status = 'SUCCESS'
),

manual_transactions AS (
    SELECT
        CAST(t.trip_timestamp AS DATE) AS transaction_date,
        t.location_id,
        d.fare_category,
        SUM(m.ticket_count) AS passenger_count,
        SUM(m.ticket_count * d.fare_value) AS revenue
    FROM source_schema.trip_fact t
    JOIN manual_source.ticket_report m
        ON t.trip_id = m.trip_id
    JOIN source_schema.fare_dimension d
        ON m.fare_name = d.fare_name
    WHERE t.status = 'COMPLETED'
      AND m.is_deleted = FALSE
    GROUP BY 1, 2, 3
),

combined_data AS (
    SELECT * FROM online_aggregated
    UNION ALL
    SELECT * FROM mobile_transactions
    UNION ALL
    SELECT * FROM manual_transactions
)

SELECT
    transaction_date,
    location_id,
    fare_category,
    SUM(passenger_count) AS total_passengers,
    SUM(revenue) AS total_revenue
FROM combined_data
GROUP BY 1, 2, 3;
```

So we have done this with dbt using a custom materialization to handle the Type 2 SCD and a stock standard dbt incremental model to handle the facts. The materialization takes a query that represents the model snapshot across all days and processes each day one by one. On the first day it inserts the records for that day, removes the date-time stamp column and adds:

  1. A hash of all the non-metadata columns using the dbt_utils hashing macro
  2. A surrogate key that it creates as a sequence in the same db and schema as the model.
  3. An eff_dttm and end_dttm, The eff_dttm is set to the date time column value for the snapshot. Initially the end_dttm is set to 9999-12-31.
  4. A meta_update_dttm that records when the record was inserted.

It then goes to the second date in order and processes it by comparing all of the records by unique key to the first date and if a record hash is different it:

  1. sets the end_dttm of the old record to the date time stamp of the second date less 1 millisecond.
  2. sets the meta_update_dttm to the current_timestamp of the old record
  3. Inserts a new record and sets the same metadata fields as before in the same way.

dbt’s built-ins for this really leave a lot to be desired so we went the route of rolling our own. There are other features like event-time-start based reloading. And it uses temporary tables during the process to speed up the overall runtime.

I believe the Kimball people have one in a dbt package somewhere, but it uses string keys. One advantage of our version is the integer key performance is way better. https://drogaieva.medium.com/enhancing-scd2-in-dbt-custom-materialization-for-creating-complex-slowly-changing-dimensions-type-2ef998bbc660

The subsequent runs just start from the high water mark of the largest eff_dttm so they are pretty efficient relative to the initial load. Initial load is always an expensive endeavor with these.