Character.ToNumber

Updated on

Character.ToNumber is a Power Query M function that returns the number equivalent of a given character.

Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365

Syntax

Character.ToNumber( character as nullable text ) as nullable number

Description

The Character.ToNumber function turns characters into their equivalent unicode decimals. You supply a character or text (which can be null), and the function returns its numeric equivalent, according to the Unicode standard. A null value is returned if the text is null.

Examples

To understand how the Character.ToNumber function works, consider the letter ‘A’. When we use this as our input:

Character.FromNumber( "A" ) // Output: 65

The function returns 65, which is the Unicode representation of the uppercase letter ‘A’.

A situation where you can use this function is when we dive into the world of lists and sequences. Consider the intriguing ‘a..b’ construct in list generation. This construct essentially maps onto the Unicode values, yielding a list of characters in the given range.

{ "a" .. "g" } returns { "a", "b", "c", "d", "e", "f", "g" }

Under the hood, the list converts the ‘a’ and ‘g’ into their Unicode equivalents, before generating the sequence:

Character.ToNumber( "a" ) // Output: 97
Character.ToNumber( "g" )  // Output: 103.

This correlation between characters and their Unicode representations is what makes the list generation possible.

This becomes even more apparent when we generate a sequence from ‘Z’ to ‘a’. In line with the Unicode values, Power Query provides the following sequence:

{ "Z" .. "a" } // Returns { "Z", "[", "\", "]", "^", "_", "`", "a" }

Character.ToNumber( "Z" ) // Output: 90
Character.ToNumber( "[" ) // Output: 91
Character.ToNumber( "`" ) // Output: 96
Character.ToNumber( "a" ) // Output: 97

In essence, the Character.ToNumber function helps in transforming Unicode characters into their numeric equivalents. It’s helpful when working with list generation and other functions that rely on character manipulation.

Learn more about Character.ToNumber in the following articles:

Other functions related to Character.ToNumber are:

Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/character-tonumber
© 2023 BI Gorilla. All rights reserved. Content derived from Microsoft documentation is property of Microsoft Corp.