Text.Length

D

T

The M Code Behind the Power Query M function Text.Length

However, while the Text.Length function is simple to use, its underlying M code is more complex. In this article, we will explore the M code behind Text.Length, and how it works to provide users with accurate character counts.

What is the M language?

Before we dive into the M code behind Text.Length, it’s important to understand the M language itself. M is the language used by Power Query to manipulate and transform data. It is a functional language, which means that it relies on the input and output of functions to perform specific tasks.

While M may seem daunting at first, it is actually quite intuitive and easy to use once you become familiar with its syntax and structure. In fact, many of Power Query’s most powerful functions, including Text.Length, are built using M code.

Understanding Text.Length

The Text.Length function is used to count the number of characters in a given text string. To use Text.Length, simply enter the text string you wish to count as the function’s argument. For example:


=Text.Length("Hello, world!")


This will return the number of characters in the text string “Hello, world!” (which is 13, if you’re counting).

But how does Text.Length actually work? Let’s take a look at the M code behind the function to find out.

The M code behind Text.Length

The M code behind Text.Length is actually quite simple. Here is the code:


let

Source = (#"Text" as text) =>

Text.Length(#"Text")

in

Source


Let’s break this code down and examine each component in turn.

The ‘let’ statement

The ‘let’ statement is used to define a variable within M code. In this case, we are defining a variable called ‘Source’.

The Source variable

The Source variable is defined as follows:


Source = (#"Text" as text) =>

Text.Length(#"Text")


This code defines a function that takes a single argument (#”Text”) of type ‘text’. The function then returns the length of the text string using the Text.Length function.

The ‘in’ statement

The ‘in’ statement is used to indicate the end of the variable definition. In this case, it signifies the end of the ‘Source’ variable definition.

In conclusion, the M code behind Text.Length is a simple yet powerful function that allows users to count the number of characters in a given text string. While the M code itself may seem complex, it is actually quite straightforward once you break it down into its component parts.

By understanding the M code behind Text.Length, users can gain a better understanding of how Power Query functions work, and how they can be used to manipulate and transform data. So the next time you use Text.Length, you’ll know exactly how it’s working behind the scenes!

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)