Bird
Raised Fist0
dbtdata~10 mins

Handling late-arriving data in dbt - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Handling late-arriving data
Data arrives
↓
Check if data is late?
No→Process normally
Yes↓
Apply late data handling logic
↓
Update existing records or append
↓
Recalculate aggregates if needed
↓
Final clean dataset ready
Data arrives and is checked if late. If late, special logic updates or appends data, then aggregates are recalculated.
Execution Sample
dbt
with base as (
  select * from source_table
),
late_data as (
  select * from base where date < current_date - interval '7' day
),
final as (
  select * from base
  union all
  select * from late_data
)
select * from final
This dbt SQL snippet identifies late data older than 7 days and combines it with the base data for final processing.
Execution Table
StepActionData SampleResulting Data State
1Load source_table[{id:1, date:2024-06-01}, {id:2, date:2024-06-10}]All data loaded
2Filter late data (date < current_date - 7)[{id:1, date:2024-06-01}]Late data isolated
3Combine base and late_data[{id:1, date:2024-06-01}, {id:2, date:2024-06-10}, {id:1, date:2024-06-01}]Duplicates possible, data unioned
4Process final datasetDuplicates handled or aggregates recalculatedClean final dataset ready
5End-Processing complete
💡 All data processed including late arrivals, final dataset ready for analysis
Variable Tracker
VariableStartAfter Step 2After Step 3Final
baseempty[{id:1, date:2024-06-01}, {id:2, date:2024-06-10}][{id:1, date:2024-06-01}, {id:2, date:2024-06-10}][{id:1, date:2024-06-01}, {id:2, date:2024-06-10}]
late_dataempty[{id:1, date:2024-06-01}][{id:1, date:2024-06-01}][{id:1, date:2024-06-01}]
finalemptyempty[{id:1, date:2024-06-01}, {id:2, date:2024-06-10}, {id:1, date:2024-06-01}][Cleaned dataset without duplicates]
Key Moments - 2 Insights
Why do we check if data is late before processing?
Because late data can change past results, so we handle it differently to update or append existing records as shown in step 2 of the execution_table.
What happens if we just append late data without cleaning duplicates?
Duplicates can cause wrong analysis results. Step 3 shows unioning data which may create duplicates, so step 4 cleans or recalculates aggregates.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what data does 'late_data' contain after step 2?
AAll records from source_table
BRecords with event_date within last 7 days
CRecords with event_date older than 7 days
DEmpty dataset
💡 Hint
Check the 'Data Sample' column in step 2 of execution_table
At which step does the dataset potentially contain duplicates?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Resulting Data State' in step 3 mentioning duplicates
If we skip late data handling, what is the likely impact on the final dataset?
AFinal dataset will be missing late-arriving records
BFinal dataset will have duplicates
CFinal dataset will be empty
DNo impact, data is always complete
💡 Hint
Refer to the concept_flow where late data is checked and handled specially
Concept Snapshot
Handling late-arriving data in dbt:
- Identify late data by comparing event dates
- Separate late data for special processing
- Combine base and late data carefully
- Clean duplicates or recalc aggregates
- Ensures accurate, up-to-date datasets
Full Transcript
This visual execution shows how to handle late-arriving data in dbt. First, data is loaded from the source. Then, late data is identified by filtering records older than a threshold (7 days). This late data is combined with the base data, which may cause duplicates. Finally, duplicates are cleaned or aggregates recalculated to produce a clean final dataset. Handling late data ensures that delayed records update past results correctly, avoiding errors in analysis.

Practice

(1/5)
1. What does late-arriving data mean in data processing?
easy
A. Data that is generated by a different system
B. Data that arrives after the expected time window
C. Data that is duplicated in the dataset
D. Data that is corrupted or incomplete

Solution

  1. 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.
  2. Step 2: Differentiate from other data issues

    It is not about corruption, duplication, or source system differences, but purely about timing.
  3. Final Answer:

    Data that arrives after the expected time window -> Option B
  4. Quick Check:

    Late-arriving data = late arrival [OK]
Hint: Late-arriving means data comes late, not corrupted or duplicated [OK]
Common Mistakes:
  • Confusing late data with corrupted data
  • Thinking late data means duplicate records
  • Assuming late data is from a different system
2. Which of the following is the correct way to filter late-arriving data in a dbt SQL model?
easy
A. WHERE event_date < current_date - interval '1 day'
B. WHERE event_date > current_date + interval '1 day'
C. WHERE event_date = current_date
D. WHERE event_date IS NULL

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    WHERE event_date < current_date - interval '1 day' -> Option A
  4. Quick Check:

    Filter dates before yesterday = WHERE event_date < current_date - interval '1 day' [OK]
Hint: Late data usually has older dates than today minus buffer [OK]
Common Mistakes:
  • Filtering future dates instead of past dates
  • Filtering only current date data
  • Filtering null dates instead of late data
3. Given this dbt incremental model SQL snippet:
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?
medium
A. To select only new or updated data since last run
B. To select all data including duplicates
C. To exclude all late-arriving data
D. To select data with null event_date

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    To select only new or updated data since last run -> Option A
  4. Quick Check:

    Incremental filter = new or updated data [OK]
Hint: Incremental models filter data newer than max existing date [OK]
Common Mistakes:
  • Thinking it excludes late-arriving data
  • Assuming it selects duplicates
  • Confusing null filtering with this clause
4. You wrote this dbt incremental model filter:
WHERE event_date > (SELECT MAX(event_date) FROM {{ this }})

But your model misses some late-arriving data. What is the likely problem?
medium
A. The subquery syntax is invalid in dbt
B. You should filter with event_date < instead
C. Using > instead of >= excludes data with event_date equal to max date
D. Late-arriving data always has null event_date

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Using > instead of >= excludes data with event_date equal to max date -> Option C
  4. Quick Check:

    Use >= to include late data on max date [OK]
Hint: Use >= not > to include late data on max date [OK]
Common Mistakes:
  • Using > excludes late data with max date
  • Thinking subquery is invalid in dbt
  • Filtering with < instead of >
  • Assuming late data has null dates
5. You want to handle late-arriving data in a dbt incremental model. Which approach best ensures late data is included without duplicating existing rows?
hard
A. Filter only event_date = current_date to avoid duplicates
B. Delete all data and reload full dataset every run
C. Ignore late-arriving data to keep model simple
D. Use a merge strategy with unique keys and filter event_date >= max date

Solution

  1. 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.
  2. 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.
  3. Step 3: Evaluate other options

    Deleting all data every run is costly. Ignoring late data loses accuracy. Filtering only current_date misses late data.
  4. Final Answer:

    Use a merge strategy with unique keys and filter event_date >= max date -> Option D
  5. Quick Check:

    Merge + unique keys + >= max date = best late data handling [OK]
Hint: Merge with unique keys and >= max date handles late data well [OK]
Common Mistakes:
  • Reloading full data every run is inefficient
  • Ignoring late data causes incomplete analysis
  • Filtering only current date misses late data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard