Int32.From

Int32.From

D

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

One of the functions available in the M language is the Int32.From function. This function is used to convert a text string to a 32-bit integer value. In this article, we will explore the M code behind the Int32.From function and how it works.

Understanding the Int32.From Function

The Int32.From function is a conversion function that takes a text string as input and returns a 32-bit integer value. The syntax for the function is as follows:


Int32.From(text as text) as number


The 'text' argument is the text string that is to be converted, and the 'number' is the 32-bit integer value that is returned. The function will return an error if the text string is not a valid integer or if it is outside the range of a 32-bit integer.

The M Code Behind the Int32.From Function

The M code behind the Int32.From function is quite simple. The function is defined as follows:


let

Int32.From = (text as text) as number =>

if Text.StartsWith(text, “-“) then

-1 List.Sum(List.Transform(

List.Skip(Text.ToList(text), 1),

each Number.From(Text.Start(_, 1)) 10 ^ (List.Count(Text.ToList(text)) – _)

))

else

List.Sum(List.Transform(

Text.ToList(text),

each Number.From(Text.Start(_, 1)) 10 ^ (List.Count(Text.ToList(text)) – _)

))

in

Int32.From


Let's break down the code line by line:

- The 'let' statement is used to define the function. The function is given a name 'Int32.From'.

- The 'text as text' argument defines the input text string that is to be converted.

- The 'as number' argument specifies that the function will return a 32-bit integer value.

- The next line of code uses an 'if' statement to check if the input text string starts with a '-'. If it does, the function will convert the text string to a negative integer value. If not, it will convert it to a positive integer value.

- The 'List.Skip(Text.ToList(text), 1)' statement is used to skip the first character of the text string if it is a '-'. This is done because the '-' character is not a numeric value and cannot be converted to an integer.

- The 'List.Transform' statement is used to iterate through each character of the text string and convert it to a numeric value.

- The 'each Number.From(Text.Start(_, 1))' statement is used to convert each character of the text string to a number.

- The '10 ^ (List.Count(Text.ToList(text)) - _)' statement is used to calculate the positional value of each digit in the integer value. The highest positional value is to the left and decreases by a factor of 10 for each digit to the right.

- The 'List.Sum' statement is used to calculate the final integer value by summing up all the positional values of each digit in the text string.

In conclusion, the Int32.From function is a powerful function in the M language that enables users to convert text strings to 32-bit integer values. The M code behind the function is simple yet effective, using a combination of conditional statements, list transformations, and positional value calculations to convert the text string to an integer value.

Understanding the M code behind the Int32.From function can help users to write their own custom functions and scripts in Power Query, enabling them to manipulate and transform their data in new and powerful ways.

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)

Copyright, G Com Solutions Ltd, 2024.
Tower Bridge Business Centre, 46-48 East Smithfield, London E1W 1AW
0800 998 9248   |   9:00 a.m. till 5:30 p.m.

Connect With Me: