Value.ViewFunction

D

T

The M Code Behind the Power Query M function Value.ViewFunction

Value.ViewFunction is a versatile function that allows you to view and modify the underlying M code behind other Power Query functions. This can be incredibly useful when troubleshooting issues with a particular function or simply gaining a better understanding of how Power Query works.

In this article, we will dive into the M code behind Value.ViewFunction and explore how it can be used to gain deeper insights into Power Query.

What is the M Code?

Before we dive into the M code behind Value.ViewFunction, it is essential to understand what the M code is and how it works.

M code is the programming language used by Power Query to transform and cleanse data. It is a functional programming language that allows you to define functions and apply them to data sets. Power Query automatically generates M code when you use its graphical user interface to perform transformations.

However, M code is not limited to Power Query. You can generate M code in other applications, such as Power BI, Excel, and Visual Studio.

Understanding Value.ViewFunction

Value.ViewFunction is a function that allows you to view and modify the underlying M code of other Power Query functions. It takes one argument, which is the function you want to view. The function you want to view must be surrounded by quotes.

The syntax for Value.ViewFunction is as follows:


Value.ViewFunction("function_name")


For example, if you want to view the underlying M code for the Table.SelectColumns function, you would use the following syntax:


Value.ViewFunction("Table.SelectColumns")


This would return the M code for the Table.SelectColumns function, which you can then modify as needed.

The M Code Behind Value.ViewFunction

Now that we understand what Value.ViewFunction does let’s dive into the M code behind it.

The M code behind Value.ViewFunction is relatively simple. It consists of a single let statement that defines a variable called fn. The value of this variable is the result of calling the Expression.Evaluate function with the argument Text.From(Value.Metadata(function_name)).

Here is the M code behind Value.ViewFunction:


(Value as any) =>

let

fn = Expression.Evaluate(

Text.From(

Value.Metadata(Value.ReplaceType(Value.Type(Value))),

Value.NativeExpression,

function_name

)

),

in

if (Function.Is(fn)) then

fn

else

error "Value.ViewFunction: function_name is not a function"


The let statement defines a variable called fn. This variable is set to the result of calling Expression.Evaluate with the argument Text.From(Value.Metadata(function_name)). The Value.Metadata function extracts metadata about the function specified by the function_name argument.

The result of Value.Metadata is a record that contains information about the function, including its documentation, argument names, and default values. The Text.From function converts this record into a string that can be evaluated as M code.

The result is an M function that represents the function specified by the function_name argument. This function can be modified, executed, or combined with other M functions to create more complex transformations.

In conclusion, Value.ViewFunction is a powerful function that allows you to view and modify the underlying M code behind other Power Query functions. Understanding the M code behind Value.ViewFunction can help you gain deeper insights into how Power Query works and how you can use it to transform and cleanse your data.

So, next time you are troubleshooting an issue with a Power Query function or simply want to gain a better understanding of how it works, consider using Value.ViewFunction to view and modify its underlying M code.

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)