Value.As

D

T

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

Understanding the Value.As function

The Value.As function in Power Query is used to convert a value to a specific data type. The function syntax is as follows:


Value.As(expression as any, type as type)


The expression parameter is the value that you want to convert, and the type parameter is the data type that you want to convert the value to.

For example, if you have a text value that you want to convert to a number, you can use the Value.As function as follows:


Value.As("123", type number)


This will convert the text value “123” to a number.

The M code behind Value.As

The M code behind the Value.As function is fairly simple. Let’s take a look at the M code for the example we used above:


Value.As("123", type number)


In M code, this would look like:


Value.As("123", type number)


As you can see, the M code is very similar to the function syntax. The expression parameter is passed as a string, and the type parameter is passed as a data type.

Using Value.As with different data types

The Value.As function can be used with a wide range of data types. Let’s take a look at some examples of how the function can be used with different data types.

Converting text to a number

As we saw in the previous example, the Value.As function can be used to convert a text value to a number. Here’s an example:


Value.As("123", type number)


This will convert the text value “123” to a number.

Converting a date/time value to text

The Value.As function can also be used to convert a date/time value to text. Here’s an example:


Value.As(#date(2021, 10, 1), type text)


This will convert the date/time value of October 1, 2021 to the text value “10/1/2021”.

Converting a true/false value to a number

The Value.As function can also be used to convert a true/false value to a number. Here’s an example:


Value.As(true, type number)


This will convert the true value to the number value of 1.

The Value.As function in Power Query is a powerful tool that can be used to convert values to a specific data type. In this article, we explored the M code behind the function and looked at some examples of how it can be used with different data types. By understanding the M code behind the Value.As function, you can better understand how it works and use it more effectively in your data transformation and analysis tasks.

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)