Logical.FromText

D

T

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

The M code behind the Logical.FromText function is relatively simple and can be broken down into a few key components. In this article, we will take a look at the M code behind this function and explore how it works.

Understanding Logical.FromText

Before we dive into the M code behind the Logical.FromText function, it is important to first understand what this function does. As mentioned earlier, this function allows users to convert text values into logical values. Specifically, it converts text values that represent boolean or logical values into their corresponding true/false values.

For example, suppose we have a column in our data that contains text values “Yes” and “No”. We can use the Logical.FromText function to convert these text values into their corresponding true/false values, which can then be used for analysis.

The M Code Behind Logical.FromText

The M code behind the Logical.FromText function is relatively simple and can be broken down into a few key components. The code is as follows:


(Text as text) as logical =>

if Text = “true” or Text = “1” then true

else if Text = “false” or Text = “0” then false

else null


Let's take a closer look at each of these components.

Text as text

The first part of the code, `Text as text`, specifies that the input to the function should be treated as text.

as logical

The second part of the code, `as logical`, specifies that the output of the function should be a logical value.

if/else statements

The bulk of the code consists of a series of if/else statements that determine the output of the function based on the input.

The first if statement checks if the input text is equal to "true" or "1". If it is, the function returns true.

The second if statement checks if the input text is equal to "false" or "0". If it is, the function returns false.

If neither of these conditions are met, the function returns null.

Using Logical.FromText

Now that we understand the M code behind the Logical.FromText function, let's take a look at how we can use this function in our data analysis.

Suppose we have the following data:

| Text |

|--------|

| Yes |

| No |

| True |

| False |

| 1 |

| 0 |

| Maybe |

| Unknown|

We can use the Logical.FromText function to convert the text values in the "Text" column into their corresponding logical values. The resulting table would look like this:

| Text | Logical |

|--------|---------|

| Yes | True |

| No | False |

| True | True |

| False | False |

| 1 | True |

| 0 | False |

| Maybe | null |

| Unknown| null |

As we can see, the Logical.FromText function has successfully converted the text values into their corresponding logical values. The text values that could not be converted (i.e. "Maybe" and "Unknown") have been replaced with null values.

The Logical.FromText function is a powerful tool in Power Query that allows users to convert text values into logical values for analysis. Understanding the M code behind this function can help users better utilize this tool and improve their data analysis workflows.

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)