Text.NewGuid

D

T

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

The Basics of GUIDs

Before we dive into the M code, let’s first go over some basics about GUIDs. A GUID, or globally unique identifier, is a 128-bit value that is guaranteed to be unique across time and space. This means that even if two different systems generate GUIDs at the same time, the probability of those GUIDs being the same is extremely low.

GUIDs are often used in computer systems to create a unique identifier for each object or record. For example, if you have a table of customer data, you might use a GUID to create a unique identifier for each customer. This can be useful when you need to merge tables or track changes over time.

The M Code Behind Text.NewGuid

Now, let’s take a look at the M code behind the Text.NewGuid function. In Power Query, you can open the Advanced Editor to view the M code for any query. Here’s the M code for the Text.NewGuid function:


let

Source = "",

Guid = Guid.NewGuid(),

TextGuid = Text.From(Guid)

in

TextGuid


Let’s break this code down line by line:

– The first line creates a variable called “Source” and sets it to an empty string. This variable is not used in the rest of the code, but it’s included here for completeness.

– The second line creates a variable called “Guid” and sets it to the result of the Guid.NewGuid() function. This function is part of the .NET Framework and generates a new GUID each time it’s called.

– The third line creates a variable called “TextGuid” and sets it to the result of the Text.From function, which converts the GUID to a text string.

Finally, the code returns the value of the “TextGuid” variable, which is the unique identifier generated by the function.

How Text.NewGuid Works

Now that we’ve seen the M code behind the Text.NewGuid function, let’s take a closer look at how it works. As we saw earlier, the function uses the Guid.NewGuid() function to generate a new GUID. This function is part of the .NET Framework and is called using the .NET Framework interop feature of Power Query.

Once the GUID is generated, it’s converted to a text string using the Text.From function. This function takes any value and converts it to a text string. In the case of a GUID, the resulting text string will be in a format like this:


f0bbf6b8-7e1e-4f09-8d5a-2e010690c275


This format is known as a “hyphenated GUID,” and it’s the most common format used in computer systems. The hyphens are included to make the GUID easier to read and to ensure that it’s always 36 characters long.

In this article, we’ve taken a closer look at the M code behind the Text.NewGuid function in Power Query. We’ve seen that the function uses the Guid.NewGuid() function to generate a new GUID and then converts it to a text string using the Text.From function. While this function may seem simple, it’s an important tool for generating unique identifiers in Power Query.

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)