Type.FunctionParameters

D

T

The M Code Behind the Power Query M function Type.FunctionParameters

Understanding Type.FunctionParameters

Before we dive into the M code behind the Type.FunctionParameters function, let’s first understand what it does. The Type.FunctionParameters function takes a function as input and returns a record that describes the parameters of the function. The record includes information such as the name, data type, and optional/default values of each parameter.

For example, let’s say we have a function called “AddNumbers” that takes two parameters, “number1” and “number2”. We can use the Type.FunctionParameters function to get information about these parameters as follows:


let

func = (number1 as number, number2 as number) => number1 + number2,

paramInfo = Type.FunctionParameters(func)

in

paramInfo


The result of this query would be a record with two fields, “Parameters” and “ReturnType”. The Parameters field would be another record with two fields, “number1” and “number2”. Each of these fields would describe the corresponding parameter, including its name, data type, and any optional/default values.

The M Code Behind Type.FunctionParameters

Now that we understand what the Type.FunctionParameters function does, let’s take a closer look at the M code behind it. Here is the M code for the Type.FunctionParameters function:


(type as type) =>

let

func = Value.Metadata(type){0},

parameters = func[Parameters],

returnType = func[ReturnType]

in

[Parameters=parameters, ReturnType=returnType]


This M code defines a function that takes a type as input and returns a record. The first line of the function simply defines the argument, “type”.

The second line of the function uses the Value.Metadata function to extract metadata from the input type. The {0} at the end of the line is simply an index that specifies which metadata to extract. In this case, we want the first metadata item, which contains information about the function.

The third line of the function extracts the “Parameters” field from the metadata record. This field contains a list of records, one for each parameter of the function. Each record describes a parameter, including its name, data type, and any optional/default values.

The fourth line of the function extracts the “ReturnType” field from the metadata record. This field describes the data type returned by the function.

Finally, the function returns a record with two fields, “Parameters” and “ReturnType”. The “Parameters” field is set to the list of parameter records extracted in the third line, and the “ReturnType” field is set to the return type extracted in the fourth line.

In this article, we explored the M code behind the Type.FunctionParameters function in Power Query. We saw that this function is used to extract information about the parameters of a given function, including their names, data types, and optional/default values. We also looked at the M code behind the function, which extracts metadata from a given type and returns a record with the parameter and return type information. With this knowledge, you should be able to use the Type.FunctionParameters function in your own Power Query queries to extract and manipulate function parameter information.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)