Character.ToNumber

D

T

The M Code Behind the Power Query M function Character.ToNumber

Understanding the Character.ToNumber Function

The Character.ToNumber function is used to convert a character value into a numeric value. It takes a single parameter, which is the character to be converted. The function then returns the numeric value of the character. For example, if the character “4” is passed to the function, it will return the number 4.

This function is useful when working with data that is stored as text and needs to be converted to a numeric value. It can also be used in conjunction with other Power Query M functions to perform more complex transformations.

The M Code Behind the Character.ToNumber Function

The M code behind the Character.ToNumber function is relatively simple. It consists of a single line of code that uses the Text.ToNumber function to convert the character to a numeric value. Here is the M code:


(Text.ToNumber(Text.From()))


The Text.From function is used to convert the character value to a text value, while the Text.ToNumber function is used to convert the text value to a numeric value. The result is then returned as the output of the function.

Using the Character.ToNumber Function

The Character.ToNumber function can be used in various ways to improve data transformation. Here are some examples:

Converting a Column from Text to Numeric

Suppose you have a column in a dataset that contains numeric values stored as text. You can use the Character.ToNumber function to convert the text values to numeric values. Here is the M code:


= Table.TransformColumns(

,{{,Character.ToNumber}})


This code uses the Table.TransformColumns function to apply the Character.ToNumber function to the specified column in the table.

Converting Multiple Columns from Text to Numeric

If you have multiple columns in a dataset that need to be converted from text to numeric values, you can use a loop to apply the Character.ToNumber function to each column. Here is the M code:


let

columns = List.RemoveFirstN(Table.ColumnNames(

),1),

convert = List.Transform(columns, each {_,Character.ToNumber}),

output = Table.TransformColumns(

,convert)

in

output


This code creates a list of column names in the dataset, removes the first column (assuming it is a header), transforms each column using the Character.ToNumber function, and then applies the transformation to the entire table.

Converting a Single Value from Text to Numeric

If you need to convert a single value from text to numeric, you can use the Character.ToNumber function directly. Here is an example:


let

value = “123”,

output = Character.ToNumber(value)

in

output


This code assigns a text value to a variable, converts the value to a numeric value using the Character.ToNumber function, and then assigns the result to another variable.

The Character.ToNumber function is a powerful tool in the Power Query M language that can be used to convert text values to numeric values. The M code behind the function is simple and easy to use, and it can be applied to various data transformation scenarios. By taking advantage of this function, users can improve their data transformation process and make better use of their data.

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)