At first glance, it seems logical to expect the mashup engine to error if you attempt to ascribe a seemingly incompatible type to a value. Surprisingly, this doesn’t always occur.
During ascription, the engine validates that the type’s base type matches the value’s base type (so, for example, you can’t ascribe type logical
onto a date value because the two base types are different). However, for types with children, those child types (list’s item type, record’s field types, table’s column types and function’s argument and return type assertions) have no effect on mashup engine behavior and are not validated for compatibility. What?!
For example, you can ascribe an item type of text onto a list of numbers, and the mashup engine won’t complain. Ascribe a column type of logical onto a column containing dates and M will comply. Similar holds true for records: a field type of duration can be ascribed onto a field containing a function and no error is raised.
Value.ReplaceType({1, 2, 3 }, type { text })
Value.ReplaceType(#table({"Col1"}, {{ #date(2020, 6, 24)}}), type table [Col1 = logical])
Value.ReplaceType([FieldA = () => ...], type [FieldA = duration])
For functions, the newly ascribed type’s argument and return type assertions aren’t validated or enforced; instead, the assertions specified when the function was originally defined continue to be what the engine follows. Take a function argument originally defined as number
and ascribe as text
to it. Despite the ascription, the mashup engine will expect the argument’s value to be compatible with number
, not text
, when the function is invoked.
let
Func = (input as number) as number => input,
NewType = type function (input as text) as text,
Ascribed = Value.ReplaceType(Func, NewType)
in
Ascribed("hi") // errors
// Ascribed(1) // works fine
These behaviors seem strange—and they aren’t the only strangeness related to Power Query’s types. Comparing type values may also not work the way you expect. Think TypeValueA = TypeValueB
will return true if the two types are identical? Maybe. Maybe not!
Fasten your seat belt. We’ll try to define and then clear up a bit of this confusion. It will be a journey! Here we go….
Continue reading →