DataEng Hub
Data modelingadvancedArticle · 8 min read

Handling Late-Arriving Dimensions and Facts in a Lakehouse

Facts that show up before their customer exists, and facts that show up weeks after the event. Inferred members, point-in-time joins and re-keying jobs on Delta Lake, with the trade-offs of each.

Balaji S.
Lead Data Engineer

TL;DR

  • Late-arriving dimension (also called an early-arriving fact): a sale references customer C-7781, but the CRM hasn't sent C-7781 yet.
  • The best default fix is an inferred member: insert a placeholder dimension row with the natural key and a flag, give the fact its real surrogate key, and fill in the attributes when the dimension arrives.
  • Late-arriving fact: a sale from 3 October arrives on 12 November. Join it to the dimension version that was valid on 3 October, not the current one.
  • Mapping to an "unknown" key (-1) is simpler but loses the link unless you run a re-keying job later.
  • Backdated dimension changes are the hardest case. They mean splitting history and re-keying facts. Avoid it unless the business genuinely needs it.

The problem

NorthBridge runs a loyalty app. A customer signs up in the app at the till and gets scanned on their first purchase within seconds. The POS system sends the sale immediately. The CRM sends the new customer in its nightly batch.

So every day, roughly 3,000 sales arrive for customers the warehouse has never heard of. The original fact load did an inner join to dim_customer, and those sales just disappeared from gold. Store managers noticed first-day spend for new members was always zero.

The first fix was a left join with coalesce(customer_key, -1). Sales stopped disappearing, but the loyalty team's "first purchase value" metric was now permanently attached to "Unknown customer". The link was gone, even after the CRM sent the customer the next morning.

There's a second, quieter problem in the same pipeline. A partner marketplace sends its sales with up to six weeks of delay. When one of those sales was loaded, it was joined to the customer's current row, so a sale made while Priya lived in Chennai got attributed to Pune.

Key concepts

Concept What it means Why it matters here
Early-arriving fact Fact arrives before its dimension member exists The most common case in event-driven systems
Inferred member Placeholder dimension row created from the fact's natural key Gives the fact a real, permanent surrogate key
Unknown member Shared -1 row meaning "we don't know" Safe and simple, but loses the link
Late-arriving fact Fact arrives long after its event time Needs a point-in-time lookup, not a current lookup
Point-in-time join Join on natural key where event time is within valid_from / valid_to Attaches the fact to the right SCD2 version

Timeline showing an early-arriving fact creating an inferred member, the dimension arriving later, and a late fact joining the historical version The fact gets a real key on day one. The dimension fills in the details on day two. Late facts look up the version that was true at event time.

Step-by-step implementation

Step 1 — Add an inferred flag to the dimension

-- Databricks Runtime 16.4 LTS
ALTER TABLE northbridge.gold.dim_customer
  ADD COLUMNS (is_inferred BOOLEAN);
 
UPDATE northbridge.gold.dim_customer SET is_inferred = false WHERE is_inferred IS NULL;

The flag does two jobs. It lets reports exclude or label placeholder customers, and it tells the dimension load to handle that row differently when the real data arrives.

Step 2 — Create inferred members before loading facts

MERGE INTO northbridge.gold.dim_customer AS t
USING (
  SELECT DISTINCT customer_id
  FROM northbridge.silver.pos_sales_batch
  WHERE customer_id IS NOT NULL
) AS s
ON t.customer_id = s.customer_id
WHEN NOT MATCHED THEN INSERT
  (customer_id, full_name, city, loyalty_tier, row_hash, valid_from, valid_to, is_current, is_inferred)
VALUES
  (s.customer_id, 'Pending CRM', 'Unknown', 'Unknown', 'inferred',
   TIMESTAMP'1900-01-01', NULL, true, true);

This runs as the first task of the fact load. Any customer the fact batch mentions but the dimension doesn't contain gets a row and a real customer_key from the identity column. valid_from goes back to 1900 deliberately, so the point-in-time join in Step 4 finds this row for any event time.

Use a placeholder value like 'Pending CRM' rather than NULL. A NULL in a slicer reads as "blank"; "Pending CRM" tells the user what's going on.

Step 3 — Overwrite the inferred member when the real data arrives

This is where inferred members need special handling in the SCD2 MERGE. When the CRM finally sends C-7781, you don't want a Type 2 change. That would close the placeholder and open a new version, leaving the first-day sale attached to "Pending CRM" forever. You want to update the placeholder in place, as if it had always had the right values.

MERGE INTO northbridge.gold.dim_customer AS t
USING customer_changes AS s
ON t.customer_id = s.customer_id AND t.is_current AND t.is_inferred
WHEN MATCHED THEN UPDATE SET
  t.full_name    = s.full_name,
  t.city         = s.city,
  t.loyalty_tier = s.loyalty_tier,
  t.row_hash     = s.row_hash,
  t.is_inferred  = false;

Run this before the normal SCD2 MERGE from SCD Type 2 with Delta MERGE. Afterwards, the row is a normal current row, the normal MERGE sees an identical hash and does nothing, and the first-day sale now shows the real customer. No fact rows were touched.

Step 4 — Load facts with a point-in-time lookup

INSERT INTO northbridge.gold.fact_sales
SELECT
  CAST(date_format(s.sale_ts, 'yyyyMMdd') AS INT)  AS date_key,
  coalesce(st.store_key, -1)                       AS store_key,
  coalesce(p.product_key, -1)                      AS product_key,
  CASE WHEN s.customer_id IS NULL THEN -2          -- anonymous: not applicable
       ELSE coalesce(c.customer_key, -1) END       AS customer_key,
  s.order_number, s.line_number, s.quantity, s.unit_price,
  s.discount_amount, s.net_amount, s.cost_amount
FROM northbridge.silver.pos_sales_batch s
LEFT JOIN northbridge.gold.dim_customer c
  ON  c.customer_id = s.customer_id
  AND s.sale_ts >= c.valid_from
  AND (s.sale_ts < c.valid_to OR c.valid_to IS NULL)
LEFT JOIN northbridge.gold.dim_store   st ON st.store_id  = s.store_id
LEFT JOIN northbridge.gold.dim_product p  ON p.product_id = s.product_id;

The customer join is the important one. It doesn't look for is_current. It looks for the version whose validity window contains the sale's own timestamp. For a sale that's on time, that's the current row. For the marketplace sale from six weeks ago, it's Priya's Chennai row, which is what the business wants.

Note the distinction between -2 and -1. -2 means the sale had no customer (an anonymous cash sale). -1 means it had one and the lookup failed. After Step 2, -1 should be rare, and you should alert on it.

Step 5 — Re-key anything that fell through

MERGE INTO northbridge.gold.fact_sales AS f
USING (
  SELECT f.order_number, f.line_number, c.customer_key
  FROM northbridge.gold.fact_sales f
  JOIN northbridge.silver.pos_sales s
    ON s.order_number = f.order_number AND s.line_number = f.line_number
  JOIN northbridge.gold.dim_customer c
    ON  c.customer_id = s.customer_id
    AND s.sale_ts >= c.valid_from
    AND (s.sale_ts < c.valid_to OR c.valid_to IS NULL)
  WHERE f.customer_key = -1
    AND f.date_key >= CAST(date_format(date_sub(current_date(), 30), 'yyyyMMdd') AS INT)
) AS fix
ON f.order_number = fix.order_number AND f.line_number = fix.line_number
WHEN MATCHED THEN UPDATE SET f.customer_key = fix.customer_key;

A nightly sweep for any -1 keys from the last 30 days. It exists for the cases the inferred-member step can't cover: a dimension load that failed, or a source that sent a fact with a malformed customer ID that got fixed later. Bounding it by date keeps it from scanning the whole fact table.

From production: gotchas and lessons

  1. Symptom: The customer dimension had 40,000 rows called "Pending CRM" that never got filled in. Cause: Test transactions and staff cards at the POS used customer IDs that the CRM would never send. Fix: A weekly report of inferred members older than seven days, sent to the CRM team. Most were junk IDs that got a source-side fix. The rest pointed to a real CRM extract bug.

  2. Symptom: After the CRM data arrived, first-day sales still showed "Pending CRM". Cause: The normal SCD2 MERGE treated the real data as a change and created version 2. Fix: The in-place update for inferred rows in Step 3, run before the SCD2 MERGE.

  3. Symptom: A backdated address correction ("she actually moved on 1 February, not 1 March") left February's sales on the wrong city. Cause: A backdated change means the boundary between versions moves, and facts in the gap point at the wrong version. Fix: We built a correction job: split or adjust the affected versions, then re-key facts for that customer within the changed window. It's expensive and fiddly, so it runs on request with an approval step. Most teams should accept "effective from when we learned about it" and document that.

  4. Symptom: The streaming fact pipeline's stream-static join to dim_customer kept missing customers created minutes earlier. Cause: The static side of a stream-static join is re-read at each micro-batch, but the dimension's nightly load meant it was simply out of date. Fix: Inferred-member creation moved into the stream's foreachBatch, writing to the dimension before joining. It works because the stream is the only writer for inferred rows.

Performance and cost notes

Range joins on valid_from / valid_to can be slow on big dimensions, because they can't use a simple hash join. Clustering the dimension on the natural key keeps the candidate rows small. Databricks also supports a range join optimisation hint (/*+ RANGE_JOIN(c, <bin_size>) */) that helps when one side is large. Measure before you reach for it. On most customer dimensions, an equality join on customer_id plus the range filter is fast enough.

When not to use this

If facts are almost never early and a 24-hour delay in attaching them to customers is acceptable, mapping to -1 and running the Step 5 sweep alone is less machinery. And if your dimensions are Type 1 only, there's no point-in-time problem: every fact joins to the single current row.

Checklist

  • Inferred members created from the fact batch before the fact load
  • Dimension load updates inferred rows in place before running the SCD2 logic
  • Facts join SCD2 dimensions by event time, not is_current
  • Separate keys for "not applicable" (-2) and "lookup failed" (-1)
  • Alert on any new -1 keys; nightly re-key sweep bounded by date
  • Policy written down for backdated dimension changes

Practice

Further reading

#late-arriving#early-arriving-facts#inferred-member#dimensions#scd2

Keep going

One practical note a week

Get new articles, problems and guides in your inbox.