Handling duplicate, missing and late-arriving data

By Hatnest · Reviewed by Saurav Raj Joshi · Updated Nov 30, 2026

Real data is rarely clean. The same record arrives twice, a column is empty, or a record shows up long after the report that needed it has run. Each problem has a standard remedy, and the right one depends on whether you are processing a batch or a stream.

Duplicates

First decide what makes two rows "the same": a business key (such as an order ID), or the whole row. Then decide which copy wins, usually the latest.

PySpark

exact = df.dropDuplicates()                          # identical rows
by_key = df.dropDuplicates(["order_id"])             # one row per key, arbitrary survivor

dropDuplicates keeps an unspecified row per key. When the newest must win, rank rows with a window instead:

from pyspark.sql import Window, functions as F
w = Window.partitionBy("order_id").orderBy(F.col("updated_at").desc())
latest = df.withColumn("rn", F.row_number().over(w)).filter("rn = 1").drop("rn")

T-SQL

ROW_NUMBER() numbers rows within each partition, so keep number 1. A CTE keeps it readable.

WITH ranked AS (
  SELECT *, ROW_NUMBER() OVER (PARTITION BY OrderID ORDER BY UpdatedAt DESC) AS rn
  FROM stg.Orders)
SELECT * FROM ranked WHERE rn = 1;

For loading, MERGE matches on a stable key to insert or update, but the source must return at most one row per target key, so deduplicate first.

KQL

distinct removes duplicate combinations of columns. summarize arg_max(UpdatedAt, *) by OrderId returns the newest row per key. An eventhouse can also keep that result current with a materialized view using arg_max.

Streaming

Sources that retry can resend events. Structured streaming remembers keys in state: dropDuplicates with the watermark column included in the key, or dropDuplicatesWithinWatermark when the ID alone defines a duplicate. Without a watermark, state is kept for the life of the query. Choose the key carefully: too broad deletes distinct events, too narrow lets duplicates through.

Missing data

Decide per column whether a missing value should be dropped, filled, flagged, or left as NULL.

  • PySpark: dropna(how="any", subset=[...]) removes rows with nulls in the listed columns; fillna(value, subset=[...]) replaces nulls.
  • T-SQL: COALESCE or ISNULL substitute defaults; IS NULL filters. Remember that COUNT(column) ignores NULLs.
  • KQL: coalesce() returns the first non-null (or non-empty string) argument; isnull(), isempty() and isnotempty() test values.
  • In the warehouse model: a failed dimension lookup should not drop the fact. Store a special member such as Unknown, then fix it when the real key is known. Soft-delete rather than physically delete missing dimension members.
  • In incremental copies: rows with a NULL watermark are skipped after the initial load, so a watermark column must be populated.

Late-arriving data

Late data means a record arrives after the point where it would normally have been processed.

  • Late facts in a stream: use event time and a watermark. The watermark says how late a record may be and still update a window. Data later than that may be dropped, and long watermarks cost state.
  • Late dimension details: a fact can arrive before its dimension member. The fact load inserts an inferred member (only the natural key is known, attributes are Unknown, and an inferred flag is set). When the real details arrive, update the row in place and clear the flag; this is a correction, not a Type 2 history change.
  • Late updates and corrections: prefer an incremental design that can reprocess a recent window (for example, always re-read the last few days) combined with an idempotent write such as MERGE, so reruns do not duplicate.
  • Reload safely: if a Copy job is reset, an Append destination will duplicate rows. Clear the destination first or use merge or overwrite semantics.

Common mistakes

  • Using dropDuplicates on a key when the latest version must survive.
  • Running MERGE with duplicate source keys.
  • Setting a streaming watermark so short that valid late events are lost.
  • Replacing nulls with zeros and then averaging, which skews results.
  • Treating an inferred dimension member's later update as SCD Type 2.

For the exam

  • Latest-wins deduplication: ROW_NUMBER in T-SQL and Spark, arg_max in KQL.
  • Streaming deduplication needs a watermark to bound state.
  • Late events: event time plus watermark; late dimension rows: inferred members.
  • Missing keys at fact load map to an Unknown member, not a dropped row.
  • NULL watermark values are skipped by incremental Copy job runs.

Sources