Logical.ToText

D

T

The M Code Behind the Power Query M function Logical.ToText

In this article, we will delve into the M code behind the Logical.ToText function and explore its features and capabilities.

Understanding the Logical.ToText Function

The Logical.ToText function is used to convert a logical value to text. The function takes two arguments: the value to be converted and an optional parameter that specifies the format of the output.

The syntax for the Logical.ToText function is as follows:


Logical.ToText(logical_value as any, optional format_text as nullable text)


The first argument, logical_value, is the value to be converted. This argument can be a logical value, such as true or false, or a column that contains logical values.

The second argument, format_text, is an optional parameter that specifies the format of the output. If this argument is not specified, the function returns the value "true" or "false" as text.

The M Code Behind the Logical.ToText Function

The Logical.ToText function is implemented in the M language, which is the language used by Power Query to define transformations and calculations. The M code behind the Logical.ToText function is relatively simple and can be modified to create custom transformations.

The M code for the Logical.ToText function is as follows:


let

logical_to_text = (value as any, optional format_text as nullable text) =>

if value = true then

if format_text = null then “true” else format_text

else if value = false then

if format_text = null then “false” else format_text

else

error “Value must be a logical.”

in

logical_to_text


The M code defines a function called "logical_to_text", which takes two arguments: "value" and "format_text". The "value" argument is the logical value to be converted, and the "format_text" argument is an optional parameter that specifies the format of the output.

The function starts with an "if" statement that checks whether the "value" argument is true. If the value is true and the "format_text" argument is null, the function returns the text "true". If the "format_text" argument is specified, the function returns the value of the "format_text" argument.

If the "value" argument is false, the function follows a similar logic to return either "false" or the value of the "format_text" argument.

Finally, if the "value" argument is not true or false, the function returns an error message.

Customizing the Logical.ToText Function

The M code behind the Logical.ToText function can be modified to create custom transformations that meet specific business requirements.

For example, suppose we want to convert a logical value to a custom text value based on certain criteria. In that case, we can modify the M code to include additional "if" statements that check for specific conditions.

Suppose we want to convert the logical value "true" to the text value "Yes" and the logical value "false" to the text value "No". In that case, we can modify the M code as follows:


let

logical_to_text = (value as any, optional format_text as nullable text) =>

if value = true then

if format_text = null then “Yes” else format_text

else if value = false then

if format_text = null then “No” else format_text

else

error “Value must be a logical.”

in

logical_to_text


The modified M code checks whether the value is true and returns "Yes" or the value of the "format_text" argument if specified. If the value is false, the function returns "No" or the value of the "format_text" argument if specified.

In conclusion, the Logical.ToText function in Power Query is a powerful tool that enables users to convert logical values to text. The M code behind the function can be modified to create custom transformations that meet specific business requirements.

By understanding the M code behind the Logical.ToText function, we can create more powerful and customized transformations in Power Query that enable us to better analyze and visualize 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)