Types and error expressions
Keep missing values and invalid input distinct
Type values classify M values: type text, type number, type nullable number, and table types describe different contracts. x is number tests compatibility; x as number checks compatibility and raises an error for incompatible values rather than parsing. Use Number.FromText for a textual number with an explicit culture. Errors indicate failed evaluation; they are not null values. try lets a query inspect a successful value or an error record. Handle failures near the operation that produces them: a successful outer record or table does not guarantee all deferred members are valid. All examples are complete standalone expressions for a blank Power Query query in a disposable Desktop file unless marked otherwise. No account, connector, file or network is required. Expected results are derived, not observed; native M execution and exact Desktop build are NOT RUN.
Check type versus conversion
let Raw = "12.5", Parsed = Number.FromText(Raw, "en-US"), Checked = try (Raw as number)in [RawIsNumber = Raw is number, Parsed = Parsed, AssertionFailed = Checked[HasError]]Expected record: RawIsNumber = false, Parsed = 12.5, AssertionFailed = true. try without a handler provides HasError and either Value (on success) or Error (on failure). Access the corresponding field only after checking HasError. try Number.FromText("bad", "en-US") otherwise null is valid but deliberately loses the distinction between missing and invalid if the status is not retained.
Classify five input records
let Inputs = { [ID = "A", Qty = "4"], [ID = "B", Qty = "bad"], [ID = "C"], [ID = "D", Qty = null], [ID = "E", Qty = "0"] }, Clean = (r as record) as record => let Raw = Record.FieldOrDefault(r, "Qty", null), Attempt = try Number.FromText(Raw, "en-US"), Status = if Raw = null then "Missing" else if Attempt[HasError] then "Invalid" else "Accepted", Qty = if Status = "Accepted" then Attempt[Value] else null in [ID = r[ID], Qty = Qty, Status = Status], Rows = List.Transform(Inputs, Clean), Result = Table.FromRecords(Rows, type table [ID = text, Qty = nullable number, Status = text])in ResultExpected:
| ID | Qty | Status |
|---|---|---|
| A | 4 | Accepted |
| B | null | Invalid |
| C | null | Missing |
| D | null | Missing |
| E | 0 | Accepted |
Record.FieldOrDefault uses null only when the field is absent here. This policy groups absent and explicit null as Missing; preserve both if the business needs them distinguished. List.Transform applies Clean to each record, and Table.FromRecords creates rows under the explicit schema. Accepted quantities sum to 4; there are two accepted and three nonaccepted rows. This fixture accepts numeric zero; it does not implement a general quantity-range policy.
Retain a diagnostic trail
Do not use otherwise 0 for every parsing problem: it would make B indistinguishable from valid zero E. In a real audit retain source identity, raw input and error details separately. error "Missing required field" deliberately raises an error; try catches an error only when evaluation reaches it. Use explicit error handling around the conversion, not a blanket handler around a large lazy table. See the lab for an added missing-field test and accepted/rejected partitions.
Practice — Extend the cleaning policy
Add F with Qty = "2.5" and G with no Qty field. Keep the current numeric policy. Predict the two new statuses, accepted count, rejected count and accepted quantity sum. Explain why introducing a whole-number rule requires a separate business decision and test.
Official reading
types · errors · number · field · conditionals · Native execution NOT RUN. · transform · fromrecords