What if your reports could fix themselves when data arrives late?
Why Handling late-arriving data in dbt? - Purpose & Use Cases
Imagine you run a store and track daily sales in a spreadsheet. Sometimes, sales data from a few days ago arrives late. You try to update your reports manually every time new data comes in.
Manually updating reports is slow and confusing. You might miss late data or overwrite correct numbers. This causes errors and wastes time, making your reports unreliable.
Handling late-arriving data in dbt lets you automatically update your models when new data arrives. It keeps your reports accurate without extra manual work, saving time and avoiding mistakes.
UPDATE sales SET amount = new_amount WHERE date = '2024-04-01';SELECT * FROM sales WHERE date >= '2024-04-01' -- dbt model handles late dataYou can trust your data reports to always reflect the latest information, even when data arrives late.
A marketing team updates campaign results daily. Sometimes data from past days arrives late. With dbt handling late-arriving data, their dashboards always show the true performance without manual fixes.
Manual updates for late data are slow and error-prone.
dbt automates updates to keep data fresh and accurate.
This saves time and builds trust in your reports.
Practice
late-arriving data mean in data processing?Solution
Step 1: Understand the timing of data arrival
Late-arriving data refers to data that comes in after the time it was expected to be available for processing.Step 2: Differentiate from other data issues
It is not about corruption, duplication, or source system differences, but purely about timing.Final Answer:
Data that arrives after the expected time window -> Option BQuick Check:
Late-arriving data = late arrival [OK]
- Confusing late data with corrupted data
- Thinking late data means duplicate records
- Assuming late data is from a different system
Solution
Step 1: Understand filtering late data by date
Late-arriving data is usually older than the current date minus a buffer (like 1 day), so we filter for dates less than that.Step 2: Check each option's logic
WHERE event_date < current_date - interval '1 day' filters for event_date before yesterday, which captures late data. WHERE event_date > current_date + interval '1 day' filters future dates, which is incorrect. WHERE event_date = current_date filters only today's data, ignoring late data. WHERE event_date IS NULL filters nulls, unrelated to late data.Final Answer:
WHERE event_date < current_date - interval '1 day' -> Option AQuick Check:
Filter dates before yesterday = WHERE event_date < current_date - interval '1 day' [OK]
- Filtering future dates instead of past dates
- Filtering only current date data
- Filtering null dates instead of late data
SELECT
id,
event_date,
data_value
FROM source_table
WHERE event_date >= (SELECT MAX(event_date) FROM {{ this }})What is the main purpose of this WHERE clause?
Solution
Step 1: Analyze the WHERE clause logic
The clause filters rows where event_date is greater or equal to the max event_date already in the target table, meaning it picks new or updated data since last run.Step 2: Understand incremental model behavior
This is a common pattern in dbt incremental models to avoid reprocessing old data and only add new or changed rows.Final Answer:
To select only new or updated data since last run -> Option AQuick Check:
Incremental filter = new or updated data [OK]
- Thinking it excludes late-arriving data
- Assuming it selects duplicates
- Confusing null filtering with this clause
WHERE event_date > (SELECT MAX(event_date) FROM {{ this }})But your model misses some late-arriving data. What is the likely problem?
Solution
Step 1: Understand the difference between > and >=
Using > excludes rows where event_date equals the max date already processed, which can miss late-arriving data with that date.Step 2: Correct the filter to include equal dates
Changing to >= includes those rows, capturing late-arriving data that arrived late but has the same event_date.Final Answer:
Using > instead of >= excludes data with event_date equal to max date -> Option CQuick Check:
Use >= to include late data on max date [OK]
- Using > excludes late data with max date
- Thinking subquery is invalid in dbt
- Filtering with < instead of >
- Assuming late data has null dates
Solution
Step 1: Understand the goal of including late-arriving data
Late data can have event_dates already processed, so we must update or insert without duplicates.Step 2: Use merge strategy with unique keys and proper filtering
Using a merge (upsert) with unique keys and filtering event_date >= max date ensures late data is added or updated correctly.Step 3: Evaluate other options
Deleting all data every run is costly. Ignoring late data loses accuracy. Filtering only current_date misses late data.Final Answer:
Use a merge strategy with unique keys and filter event_date >= max date -> Option DQuick Check:
Merge + unique keys + >= max date = best late data handling [OK]
- Reloading full data every run is inefficient
- Ignoring late data causes incomplete analysis
- Filtering only current date misses late data
Ready to prove it?
Pass the test and this pattern moves unknown
