Data contracts and rejects
Make quality rules explicit
A data contract states the expected columns, their types, grain, key semantics, units, permitted values and response to invalid data. Our fixture represents one delivery of an immutable sales event per arrival sequence, not one unique business event per row. ArrivalSeq is a unique increasing integer. EventID identifies the business event; identical repeated payloads are replays, while different payloads for an existing ID require review. EventTime is UTC encoded without a zone suffix using yyyy-MM-dd HH:mm:ss. Category must be Stationery or Lighting; AmountUSD is a nonnegative whole dollar value including zero. Strictly reject a changed header before row processing. Keep the source row and reason separately from accepted facts; do not replace malformed timestamps with today or invalid amounts with zero.
Example 1: inspect an M error record
Use a local, self-contained M query to classify raw timestamp text. try evaluates a conversion and returns a record; check HasError before accessing Value, and inspect Error only for failures. Null must be checked separately because a conversion may return null rather than an error.
let Source = #table(type table [RawTime=nullable text], {{"2026-01-02 10:00:00"},{"bad"},{null}}), Checked = Table.AddColumn(Source, "Audit", each if [RawTime] = null then [Status="MissingTimestamp", Parsed=null] else let p = try DateTime.FromText([RawTime], [Format="yyyy-MM-dd HH:mm:ss", Culture="en-US"]) in if p[HasError] then [Status="InvalidTimestamp", Parsed=null] else [Status="OK", Parsed=DateTime.AddZone(p[Value],0)]), Result = Table.ExpandRecordColumn(Checked, "Audit", {"Status","Parsed"})in ResultExpected statuses in source order: OK, InvalidTimestamp, MissingTimestamp. The valid parsed value represents 10:00 UTC on January 2. The explicit zone is applied to already-UTC wall time; it is not a conversion from local time. In production log only approved error fields; p[Error][Message] can include sensitive source text. This snippet handles only timestamps, not the whole contract.
Example 2: reconcile a quarantine
For arrival sequences 101–105, E1=20 and E2=30 are accepted. Sequence 103 repeats E1 exactly and is classified Replay (20). Sequence 104 has a malformed timestamp (40); sequence 105 has category Unknown (50). Reject those last two rows with their raw fields. Five inputs = two accepted + one replay + two rejected. Raw amount 160 = accepted 50 + replay 20 + rejected 90 USD. Raw amount is a delivery subtotal, not business revenue. Replays are tracked separately from data-quality rejects. A zero-value accepted event is still a record. Keep all three classes in the audit to avoid unexplained disappearance.
Schema drift and safe recovery
Validate required headers and types before selecting or expanding columns. If a required field is renamed or disappears, stop that batch and preserve the raw file; do not quietly substitute null. Optional new fields can be tolerated only under an explicit contract version. Record rule ID, source row identifier, batch, reason and disposition; track whether a corrected record has been reprocessed. A query-level missing source or schema error may stop the whole query, while a cell-level conversion error can be captured per row. Separate reject review from normal report consumption. Never disable a quality gate simply to make the refresh green.
Practice: review the second delivery
Classify sequences 106–108 after the first delivery is committed. Identify accepted, replay and rejected counts and amounts. Explain why the changed E2 amount must not overwrite the first E2 under this immutable-event contract. Propose a rule for correcting an incorrectly recorded event without silently changing history.