Null and error handling
Missing values are not failed conversions
M null means absence of a value; "" is text of length zero; 0 is a number and may be a valid measurement. An error signals that an expression failed, such as parsing bad as a number. These require different policies. A source can use empty fields or sentinel strings for absence; interpret them using an explicit contract. DAX uses BLANK for missing values in its model context and may coerce blanks in some operations. Do not assume a blank, zero and empty string remain distinct in every loaded model or comparison. Preserve a status field upstream when the distinction matters.
Example 1: capture conversion outcomes without hiding errors
This self-contained M query demonstrates an explicit input contract: null and empty/whitespace text mean missing; any other nonnumeric text is invalid; zero is valid. try produces a record. Inspect HasError before accessing Value; use Error only for failure. The result stores the original field and a stable classification.
let Source = #table(type table [Code = text, RawAmount = nullable text], {{"A", null}, {"B", ""}, {"C", "0"}, {"D", "bad"}, {"E", "12.50"}}), Audited = Table.AddColumn(Source, "Audit", each if [RawAmount] = null then [Status = "missing", Amount = null] else if Text.Trim([RawAmount]) = "" then [Status = "missing", Amount = null] else let p = try Number.FromText([RawAmount], "en-US") in if p[HasError] then [Status = "invalid", Amount = null] else [Status = "valid", Amount = p[Value]]), Expanded = Table.ExpandRecordColumn(Audited, "Audit", {"Status", "Amount"}, {"Status", "Amount"}), Typed = Table.TransformColumnTypes(Expanded, {{"Status", type text}, {"Amount", Currency.Type}}, "en-US")in TypedExpected: A and B missing, C valid 0, D invalid, E valid 12.50. Five rows remain; two valid, two missing, one invalid. The valid subtotal is 12.50 USD. It is not the complete amount for all five records. The raw text preserves the invalid input. In a production audit, capture a safe error reason as well, and avoid logging private raw values outside an approved boundary.
Example 2: reconcile the effect of a zero replacement
The two valid amounts (0 and 12.50) average 6.25 USD. If the two missing and one invalid amount are silently replaced with zero, the five-row average becomes 2.50 USD while the sum remains 12.50 USD. A matching sum therefore cannot prove that cleaning was correct. Report the number of valid observations alongside the subtotal and average. In DAX, ISBLANK(expression) checks a blank result; BLANK() produces a blank value. These are previews, not yet a measure lesson. Use a separate status column for missing and invalid input and define whether a KPI should remain blank when evidence is insufficient.
Common mistake: use try otherwise 0 for every failure
try Number.FromText([RawAmount], "en-US") otherwise 0 can collapse a conversion failure into a real zero; a null result may also be returned without error. Neither is a suitable blanket policy. A failed required field should be quarantined with its original row and reason. An optional missing field may remain null if the business definition allows it. A numeric parse alone also does not validate business rules such as allowed ranges, required dates or referential integrity. Keep accepted, missing and invalid counts and verify their sum equals the source row count.
Practice: produce an audit reconciliation
Complete the final exercise in labs/data-types/worksheet.md. Classify the supplied edge fixture using its culture whitelist and exact date formats; mark unsupported culture, missing fields and parse errors separately. Reconcile accepted counts and known totals without treating rejected amounts as zero. Native M execution is optional in an approved lab and must be recorded separately.
Official reading
M error handling · Handling errors · Text.Trim · BLANK · ISBLANK. Documentation checked; native execution NOT RUN.