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?
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_id | customer | amount | failed_rule |
|---|---|---|---|
| 50211 | Contoso | 120.00 | passed |
| 50212 | (null) | 89.50 | customer_missing |
| 50213 | Fabrikam | -40.00 | amount_negative |
| 50214 | Northwind | 75.00 | order_date_in_future |
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?
- Microsoft Learn: How to use Microsoft Fabric notebooks
- Microsoft Learn: Notebook activity in Data Factory for Microsoft Fabric
- Apache Spark docs: pyspark.sql.functions.coalesce
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.