BS Consulting
Blog

Bronze, silver, gold: Follow a bad row through the lakehouse

Four production messages arrive. One is a duplicate, another reports more rejected units than inspected units. Following all four makes the medallion layers easier to understand than naming them on an architecture slide.

Preserve the delivery well enough to reconstruct it

Imagine a file resent after an interrupted transfer. Bronze preserves the received content with its source, arrival time and load identifier. To investigate the issue, engineers must be able to establish whether two deliveries carried the same payload. Silently dropping suspicious rows on arrival removes that evidence and makes later reconciliation difficult.

Four rows in an example delivery
Input rowEvent IDInspectedRejected
1E11004
2E11004
3E2503
4E32030
From delivery to business metric
  1. Bronze · 4 rowsKeep raw values and lineage for every incoming row.
  2. Silver · 2 valid eventsAccount separately for one exact duplicate and one invalid quantity.
  3. Gold · 1 daily KPI7 rejects out of 150 inspected units give about 4.67 percent.

Define the silver data contract

For this example a valid event has an ID, non-negative whole counts and no more rejects than inspected units. The SQL illustrates type and range checks for integer source text. If your contract permits only decimal digits or specifies lengths, validate the original text format before conversion as well. Route rows with a reason code to quarantine while retaining a link to their original delivery so the owner can investigate.

Databricks SQL
WITH typed AS (
  SELECT event_id, try_cast(inspected AS BIGINT) AS n,
         try_cast(rejected AS BIGINT) AS r
  FROM bronze_runs
)
SELECT *, CASE
  WHEN event_id IS NULL THEN 'missing_id'
  WHEN n IS NULL OR r IS NULL THEN 'invalid_count'
  WHEN n < 0 OR r < 0 OR r > n THEN 'invalid_range'
  ELSE 'valid'
END AS validation
FROM typed;

This fragment validates values. A separate step compares events with the same ID. The two E1 rows have identical values and business versions, so exactly one survives. Two different payloads with the same ID and version need different handling. Define an authoritative revision order or raise a conflict for investigation. Keeping whichever row happens to arrive first can conceal a correction.

Design gold for the consumer

Group accepted events by the agreed operational day and, where appropriate, by station. Keep inspected and rejected units as separate additive totals. Power BI can calculate the multi-day rate from their sums. A table retaining only rounded daily percentages often cannot produce a correct monthly rate.

Reconcile every run. Here, four input rows correspond to two accepted events, one exact duplicate and one quarantined record. Record the counts against this delivery ID. If later changes arrive as revisions, the reconciliation must also distinguish new events from updated versions. Check the quality gate before exposing the affected gold result to business users.

Decide which layer to rerun after a correction

Suppose E3 is corrected to 20 inspected and 3 rejected parts. Preserve the new delivery with its own provenance and associate it with the original E3. Silver can now accept the corrected event. Keep the original quarantined delivery traceable as evidence of what first arrived.

Gold for that day should now contain 170 inspections and 10 rejects. The corrected rate is about 5.88 percent. Other days should retain their control totals. This cross-check tests that the pipeline rebuilt the right business partition. It also catches a common error: appending the newly calculated complete daily total to the old one and summing both counts E1 and E2 twice.

Try it yourself

A report shows 11 rejects out of 250 inspected parts. Which step was probably missed?

Show the solution

The invalid E3 was removed but E1 was counted twice. The totals are then 4 + 4 + 3 rejects and 100 + 100 + 50 inspected. Test business-key uniqueness in silver and reconcile against the four bronze rows.

Sources and further reading