Power Query Text.NewGuid Function: How to Generate GUIDs
In this article
The Text.NewGuid function in Power Query is used to generate a Globally Unique Identifier (GUID), which is a 128-bit integer used as a unique identifier. To use it, you simply enter = Text.NewGuid() into a Custom Column formula or the Formula Bar. This is primarily used when your dataset lacks a primary key and you need a guaranteed unique ID for every row to facilitate merging tables or tracking records across different systems without risk of duplication.
What You'll Need
- Microsoft Excel (with Power Query enabled) or Power BI Desktop.
- A dataset loaded into the Power Query Editor.
- Basic familiarity with adding custom columns in the Power Query interface.
Step-by-Step
- Open the Power Query Editor: In Excel, go to the Data tab and select Get Data > Launch Power Query Editor. In Power BI, click Transform Data on the Home ribbon.
- Select Your Table: In the Queries pane on the left, click the table where you want to add the unique identifiers.
- Add a Custom Column: Navigate to the Add Column tab on the top ribbon. Click on Custom Column. This opens a dialog box where you can define the logic for your new column.
- Enter the Formula: In the Custom column formula box, type exactly:
= Text.NewGuid(). Ensure you include the parentheses at the end, as this is a function call. - Name the Column: In the New column name field at the top of the dialog, enter a descriptive name such as "RowID" or "UniqueKey".
- Verify the Data Type: Click OK. You will see a new column populated with strings like "550e8400-e29b-41d4-a716-446655440000". Right-click the column header, select Change Type, and choose Text.
- Apply Changes: Go to the Home tab and click Close & Apply to load the data with the new GUIDs into your data model or worksheet.
Common Mistakes
One of the most frequent errors is forgetting the parentheses. Because Text.NewGuid is a function, writing just Text.NewGuid without () will result in an expression error because Power Query thinks you are referring to the function object itself rather than executing the function to get a value.
Another common mistake is assuming that the GUID is "static" or "persistent" across every single refresh in the same way a hard-coded ID would be. While the GUID is unique for that specific row during that specific execution, Power Query is a functional language. If the steps preceding the GUID generation change or if the data is re-sorted, the function may re-evaluate, potentially assigning new GUIDs to rows. If you need these IDs to be permanent for a database, you should load the data to a destination (like a SQL table) where the ID is stored permanently.
Users also occasionally try to use Text.NewGuid within a conditional statement without realizing that the function will run every time the condition is met. If you are using it inside an if...then...else block, ensure the logic is sound so you aren't generating IDs for rows that don't actually require them, which can slightly impact performance on massive datasets.
Tips
To ensure your GUIDs remain consistent during a single session of data transformation, place the Text.NewGuid step as late as possible in your query sequence. By adding the unique ID after all your filtering, grouping, and merging is complete, you avoid unnecessary recalculations of IDs for rows that might eventually be filtered out of the final set.
If you are working with extremely large datasets (millions of rows), be aware that generating a GUID for every single row consumes memory and processing time. If you only need a unique ID for a subset of data, use a conditional column to apply the Text.NewGuid() function only where it is strictly necessary.
For those who need a shorter unique identifier, you can wrap the function in other text functions. For example, using Text.Start(Text.NewGuid(), 8) will give you only the first eight characters of the GUID. While this increases the theoretical risk of a collision (two rows having the same ID), it is often sufficient for visual labels or short-form referencing.
Troubleshooting
If you receive a "Token Literal expected" or "Expression.Error" message, the first thing to check is the syntax. Ensure there are no spaces between the function name and the parentheses, and verify that you have used a capital 'T' and capital 'N', as Power Query (M language) is case-sensitive. text.newguid() will not work; it must be Text.NewGuid().
If you notice that the GUIDs are changing every time you click a different step in the Applied Steps pane, this is normal behavior for volatile functions in Power Query. The editor refreshes the preview of the data as you navigate the steps. To stop this during the design phase, you can disable the "Allow data preview to download in the background" option in the Query Options menu, though this is a global setting and affects all queries.
If the column shows "Error" in every cell, check if you have accidentally placed the formula inside a column that is being forced into a Numeric data type. Since a GUID contains hyphens and letters, it must be stored as a Text type. Change the column type to Text via the Transform tab to resolve this.
Frequently Asked Questions
Is a GUID the same as a Random Number?
No. A random number has a high probability of duplicating (collision) as the dataset grows. A GUID is specifically designed to be globally unique across all computers and networks, making it safe for use as a primary key in large-scale databases.
Can I use Text.NewGuid to link two different tables?
Yes, but only if you generate the GUID in one table and then merge that table into the second one. You cannot call Text.NewGuid() in two separate tables and expect them to generate the same ID for the same entity; every call to the function produces a brand new, unique string.
Does Text.NewGuid work in Excel formulas outside of Power Query?
No. Text.NewGuid is a function of the M language used exclusively within Power Query. Standard Excel worksheet formulas do not have a built-in GUID function; you would need to use VBA or a complex combination of RANDBETWEEN and DEC2HEX to mimic the behavior.
Will the IDs change when I refresh the data?
Yes, by default, Power Query re-runs all steps upon refresh. Since Text.NewGuid() generates a new value every time it is called, your rows will receive new IDs upon every refresh. To make them permanent, you must load the result to a static table or database.
Can I customize the format of the GUID?
The function always returns the standard 36-character format (32 alphanumeric characters and 4 hyphens). To change this, you must use Text.Replace to remove the hyphens or Text.Upper / Text.Lower to change the casing of the letters.