Quarantine bad rows before they reach silver

Data quality checks in a Fabric notebook: each rule has a name, failing rows go to a quarantine table with the rule they broke, and too many failures stop the run.

Short answer: write every check as a named rule, test each row on its way from bronze to silver, and send the rows that fail to a quarantine table together with the rule they broke. Count passed and set-aside rows on every run, and stop the run before silver changes when too many fail.

Dropping bad rows quietly makes the numbers look clean and wrong: totals shrink and nobody can say why. Letting them through is worse, because every report then counts them. Quarantine is the middle path: bad rows leave the load, and everyone can see which ones and why.

Where do bad rows go?

Bronze orders10,412 rows in this run
Silver10,377 rows passed every rule
Quarantine35 rows, each with the rule it broke
Every row from bronze ends up in exactly one place

What does a rule look like?

A name and a condition that a good row meets. Keeping them in one list makes them easy to read, to count and to change:

order_idcustomeramountfailed_rule
50211Contoso120.00passed
50212(null)89.50customer_missing
50213Fabrikam-40.00amount_negative
50214Northwind75.00order_date_in_future
Four rows from bronze: one passes, three go to quarantine with their rule
from pyspark.sql import functions as F

RULES = {
    "customer_missing":     F.col("customer").isNotNull() & (F.trim("customer") != ""),
    "amount_negative":      F.col("amount") >= 0,
    "order_date_in_future": F.col("order_date") <= F.current_date(),
}

rows = spark.table("bronze_orders_today")
failed = F.coalesce(*[F.when(~ok | ok.isNull(), F.lit(name)) for name, ok in RULES.items()])
checked = rows.withColumn("failed_rule", failed)

good = checked.filter(F.col("failed_rule").isNull()).drop("failed_rule")
bad = checked.filter(F.col("failed_rule").isNotNull()).withColumn("_run_at", F.current_timestamp())

coalesce picks the first rule a row breaks, so every bad row carries one clear reason. The ok.isNull() part matters: a comparison with a null is neither true nor false, and without it a row with a null amount would slip through both filters.

When does the run stop?

Before silver changes. A few bad rows are normal; a third of the file failing usually means the source changed, and loading the rest would hide it.

total, n_bad = checked.count(), bad.count()
bad.write.mode("append").saveAsTable("quarantine_orders")

if total and n_bad / total > 0.02:
    raise Exception(f"{n_bad} of {total} rows failed the rules (over 2%): silver left as it was")

good.write.mode("append").saveAsTable("silver_orders_staged")

An exception fails the notebook, so the pipeline that runs it fails too and can alert. The quarantine rows are written first on purpose: when the run stops, they show what went wrong.

What goes wrong?

  • Rules hidden in a long chain of filters. Nobody can say which rule dropped a row. One list of named rules, as above, is the documentation.
  • A quarantine nobody reads. Put the counts per rule in the run log and on a small report page. A rule that fails every day either has a bug or found a real problem in the source.
  • A threshold that is too tight. If ordinary days fail, people stop trusting the alert. Start from what a normal run looks like, then set the line above it.

Where can you check this?

Where do you start?

The second lesson of A lakehouse in three layers cleans bronze into silver with exactly this kind of quarantine, in your own Fabric workspace. It follows on from bronze, silver and gold, and the pattern sits on the map of my work.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.