Type.Is

Updated on

Type.Is is a Power Query M function that checks if a value of type1 is always compatible with type2. The function returns a boolean indicating the compatibility between the two types.

Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365

Syntax

Type.Is(
   type1 as type,
   type2 as type,
) as logical

Description

The Type.Is functionevaluates the compatibility between two data types. It takes two types as input: the first is the type to check, and the second is a type against which compatibility is assessed. It returns a boolean value indicating whether the first type is compatible with the second.

Examples

Let’s see some examples of how you can use the Type.Is function.

Assessing Text and Nullable Text Compatibility

To find out if a value of type text is compatible with the type nullable text you can use the following expression:

// Output: true 
// Explanation: A text type is inherently compatible with a nullable text type.

Type.Is( type text, type nullable text )

However, you will find that the reverse is not true. The type nullable text is not compatible with type text. The reason for this is that type nullable text value, accepts both text and null values, whereas type text only accept text values. The following expression therefore returns false:

// Output: false
// Reason: Nullable text type's flexibility for null values isn't supported by the text type.

Type.Is( type nullable text, type text )

Comparing Record and Table Types

An intriguing comparison involves checking if a record type aligns with a table type:

// Output: false 
// Reason: Despite both being complex types, a record and a table type are inherently different.

Type.Is( type record, type table ) 

Custom Type Compatibility Checks

You can also perform tests with custom types. For example, you can test whether a custom record type is compatible with the type record.

// Returns true 
// Reason: A custom record type still adheres to the general structure of a record.


let
  MyRecordType = type [Name = text, Age = number],
  testTypes = Type.Is(MyRecordType, type record)
in
  testTypes

The reverse however isn’t true. A value of type record does not always conform to the custom type we defined. Because of that, reversing the order of the Type.Is parameter returns false:

// Returns false - a custom record type conforms to type record
// Reason: The record type does not always conform to the  structure of the custom record type.

let
  MyRecordType = type [Name = text, Age = number],
  testTypes = Type.Is( type record, MyRecordType )
in
  testTypes

Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/type-is