Schema drift handling
Reject schema drift before converting values
Schema drift is an unexpected change in the input contract, such as missing or renamed fields, added fields or incompatible types. For this lab the exact headers ID and Qty are required; order is not significant. We quarantine an entire file if a header is missing or unexpected. Valid-schema files can still contain invalid rows, which we preserve separately with source and reason. This strict policy is a teaching decision; production teams may approve optional columns or versioned mappings. All snippets are standalone M expressions for a blank query in a disposable Desktop Import environment unless otherwise noted. Outputs are expected, not observed; native M execution and installed build are NOT RUN. Use the inline synthetic inputs; no external account, connection or folder permission is required.
Example 1: compare actual and expected headers
let Expected = {"ID", "Qty"}, Check = (t as table) as record => let Names = Table.ColumnNames(t), Missing = List.Difference(Expected, Names), Extra = List.Difference(Names, Expected) in [Valid=List.IsEmpty(Missing) and List.IsEmpty(Extra), Missing=Missing, Extra=Extra], Good = #table({"Qty","ID"},{{2,"001"}}), Drift = #table({"ID","Units"},{{"002",3}})in [Good=Check(Good), Drift=Check(Drift)]Expected Good.Valid=true; both lists empty. Drift.Valid=false, Missing={"Qty"}, Extra={"Units"}. No automatic rename is justified. Table.ColumnNames checks names, not content; Table.Schema can inspect column metadata, but actual values and business constraints still need validation. Detect duplicate raw headers before promotion if your file contract requires this; the lab uses unique raw headers.
Example 2: keep invalid row evidence
let Raw = #table(type table [ID=text, Qty=text], {{"001","2"},{"002","bad"},{"003","-1"}}), Inspect = (s as text) as record => let p = try Number.FromText(s, "en-US") in if p[HasError] then [Status="Invalid number", Value=null] else if p[Value] = null then [Status="Missing", Value=null] else if p[Value] < 0 or Number.RoundDown(p[Value]) <> p[Value] then [Status="Invalid quantity", Value=null] else [Status="Accepted", Value=p[Value]], Checked = Table.AddColumn(Raw,"Audit",each Inspect([Qty]),type record), Expanded = Table.ExpandRecordColumn(Checked,"Audit",{"Status","Value"}, {"Status","ParsedQty"})in ExpandedExpected: 001 accepted with 2; 002 invalid number; 003 invalid quantity. Rejected ParsedQty is null, not zero, and original Qty remains available. This example uses bounded non-null text inputs. The lab extends the policy to blank strings. Catch errors close to Number.FromText; wrapping a lazily evaluated table in try does not necessarily catch later cell errors. For real unbounded input also check finite values, maximum quantity and duplicate IDs.
Make acceptance explicit
Check header contract before accessing required fields; keep a file-level audit even when all its rows are rejected. Count raw rows = accepted rows + rejected rows, including rows in rejected files, using a defined counting rule. Sum only accepted quantities and label unknown rejected amounts as unknown. Recheck global uniqueness after append. Do not silently use MissingField.UseNull for a required ID or Qty. A missing required field is not equivalent to an optional missing value. The lab retains raw inputs, a per-file audit, and row status records; it does not implement a general CSV parser or production transaction mechanism.
Practice: distinguish zero from missing
In example two replace -1 with 0, and add a row 004 with Qty 2.5. Predict the accepted IDs, total accepted Qty and rejected reasons. Explain why replacing bad text with 0 would change the metric meaning.
Official reading
columnnames · difference · schema · errors · number · types