Type.ForFunction

Updated on

Type.ForFunction is a Power Query M function that creates a function type from a signature record, specifying ReturnType and Parameters, and a minimum number of arguments required. The function returns the newly created function type based on the given signature and minimum arguments.

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

Syntax

Type.ForFunction(
   signature as record,
   min as number,
) as type

Description

The Type.ForFunction function enables the creation of a function type from a record value. The function has two arguments, the ‘signature and a ‘min’ value.

The signature should be provided as a record and must contain two fields: ReturnType, specifying the type of value the function returns, and Parameters, defining the parameters the function accepts.

An additional argument in this function allows for marking some parameters as optional by setting a minimum number of arguments required to invoke the function. When a parameter is made optional, its corresponding type becomes nullable.

Examples

Creating Basic Function Types

Here’s an example of creating a function type that takes a single number parameter named ‘Amount’ and returns a number:

// Output: type function ( Amount as number ) as number
Type.ForFunction( 
  [ 
    ReturnType = type number, 
    Parameters = [ Amount  = type number ] 
  ], 
  1 
)

This expression results in a function type where ‘Amount’ is a required number parameter.

Defining Optional Arguments

To introduce optional arguments in a function type, set the ‘min’ value in the second argument to be less than the total number of parameters. For instance, to create a function type with two parameters but only one required, the following can be used:

// Output: type function ( Date as date, Amount as nullable number ) as number
// Providing 2 parameters and making only 1 required, makes the other optional.
Type.ForFunction( 
  [ 
    ReturnType = type number, 
    Parameters = [ Date = type date, Amount = type number ] 
  ], 
  1 
)

This code defines a function type with two parameters:

  • Date: as it’s the first argument and the ‘min’ value is 1, it’s a required argument.
  • Amount: as second argument, this value is optional receiving ‘nullable number’ as type.

To analyze the properties of the created function type, Power Query M offers functions like Type.FunctionParameters and Type.FunctionRequiredParameters.

Here’s how you can use them to examine the function type:

let
  myFunction =
    Type.ForFunction( 
      [ 
        ReturnType = type number, 
        Parameters = [ Date = type date, Amount = type number ] 
      ], 
      1 
  ),
  // Returns [ Date = date, Amount = nullable number ]
  myParameters = Type.FunctionParameters ( myFunction ), 
  // Returns 1
  myReqParameters = Type.FunctionRequiredParameters( myFunction ) 
in
  myReqParameters 

This script returns the parameters of myFunction, showing ‘Date’ as a date and ‘Amount’ as a nullable number, and indicating that only one parameter is required.

As the examples show, Type.ForFunction is a versatile function in Power Query M that allows for the creation of customized function types with the flexibility of optional and nullable parameters. It’s particularly useful in scenarios where function signatures need to be dynamically defined.

Other functions related to Type.ForFunction are:

BI Gorilla Youtube Channel

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