Number.From

D

T

The M Code Behind the Power Query M function Number.From

In this article, we will explore the M code behind the Number.From function and how it can be used to convert values into numbers.

Understanding the Number.From Function

The Number.From function is used to convert a value into a number. It takes a single parameter which can be a text, date, datetime, or time value. The syntax for the Number.From function is as follows:


Number.From(value as any) as nullable number


The value parameter is the value that needs to be converted into a number. The nullable number is the result that is returned by the function.

The Number.From function works by converting the input value into a number. If the input value cannot be converted into a number, the function returns a null value.

The M Code Behind the Number.From Function

The M code behind the Number.From function can be broken down into three main parts: the function definition, the parameter type checking, and the value conversion.

Function Definition

The function definition is the first part of the M code behind the Number.From function. It defines the name of the function, the parameter, and the output value.


Number.From = (value) =>


The above code defines the Number.From function with a single parameter called value. The arrow operator (=>) is used to indicate the start of the function body.

Parameter Type Checking

The second part of the M code behind the Number.From function is the parameter type checking. This part of the code checks whether the input value is of a valid type that can be converted into a number.


if (not (value is text or value is datetime or value is date or value is time)) then null


The above code checks whether the input value is of type text, datetime, date, or time. If the input value is not of any of these types, the function returns null.

Value Conversion

The final part of the M code behind the Number.From function is the value conversion. This part of the code converts the input value into a number.


else if (Number.Is(value)) then value

else Number.FromText(Text.From(value), “en-US”)


The above code checks whether the input value is already of type number using the Number.Is function. If the input value is already a number, the function returns the input value as is. If the input value is not a number, the function converts the input value into a text using the Text.From function and then converts it into a number using the Number.FromText function.

In summary, the M code behind the Number.From function defines the function name, checks whether the input value is of a valid type, and converts the input value into a number.

The Number.From function is a built-in function in Power Query that converts a value into a number. The M code behind the function defines the function name, checks whether the input value is of a valid type, and converts the input value into a number. Understanding the M code behind the Number.From function can help you use it effectively in your data transformation 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)