Guid.From

Guid.From

D

The M Code Behind the Power Query M function Guid.From

What is a GUID?

A GUID is a unique identifier that is used to represent a piece of data in a globally unique way. GUIDs are often used in computer systems to ensure that data is unique, even when it is copied or transferred between different systems. GUIDs are usually represented as a 16-byte sequence of hexadecimal digits, such as “6F9619FF-8B86-D011-B42D-00C04FC964FF”.

The Guid.From Function

The Guid.From function is a built-in function in Power Query that is used to convert a 16-byte GUID into a text string. The syntax of the Guid.From function is as follows:


Guid.From(binaryGUID as binary) as text


The function takes a single argument, which is a binary value representing the 16-byte GUID. The function returns a text value representing the GUID in the standard format, with groups of hexadecimal digits separated by hyphens.

The M Code Behind Guid.From

The M code behind the Guid.From function is relatively simple, but it is still worth examining in detail. The following code snippet shows the implementation of the Guid.From function:


let

Bytes = List.Buffer(Binary.ToList(binaryGUID)),

Guid = Text.Combine(

{

Text.From(Bytes{0}, “X2”), Text.From(Bytes{1}, “X2”), Text.From(Bytes{2}, “X2”), Text.From(Bytes{3}, “X2”),

“-“,

Text.From(Bytes{4}, “X2”), Text.From(Bytes{5}, “X2”),

“-“,

Text.From(Bytes{6}, “X2”), Text.From(Bytes{7}, “X2”),

“-“,

Text.From(Bytes{8}, “X2”), Text.From(Bytes{9}, “X2”),

“-“,

Text.From(Bytes{10}, “X2”), Text.From(Bytes{11}, “X2”), Text.From(Bytes{12}, “X2”), Text.From(Bytes{13}, “X2”), Text.From(Bytes{14}, “X2”), Text.From(Bytes{15}, “X2”)

},

“”

)

in

Guid


The code starts by defining a variable called "Bytes" that contains a list of the individual bytes in the binary GUID. The List.Buffer function is used to create a buffer of the byte list, which can improve performance by reducing the number of calls to the underlying data source.

The code then defines a variable called "Guid" that uses the Text.Combine function to concatenate the individual bytes into a text string. The Text.From function is used to convert each byte into a two-digit hexadecimal representation, and the "X2" format specifier is used to ensure that each byte is padded with a leading zero if necessary.

The text string is then formatted using the standard GUID format, with groups of hexadecimal digits separated by hyphens. The Text.Combine function is used again to concatenate the individual groups of digits, and the empty string "" is used as the separator.

Finally, the Guid variable is returned as the result of the function.

In this article, we have examined the M code behind the Guid.From function in Power Query. We have seen how the function converts a 16-byte GUID into a text string, and we have examined the detailed implementation of the function in M code. By understanding the M code behind built-in Power Query functions like Guid.From, we can gain a deeper understanding of the capabilities and limitations of the Power Query language, and we can use this knowledge to create more powerful and efficient data processing solutions.

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)

Copyright, G Com Solutions Ltd, 2024.
Tower Bridge Business Centre, 46-48 East Smithfield, London E1W 1AW
0800 998 9248   |   9:00 a.m. till 5:30 p.m.

Connect With Me: