Power Query Add Leading Zeros: Step-by-Step Guide
In this article
To add leading zeros in Power Query, you must first change the column data type to Text and then use the Text.PadStart function in a Custom Column. Power Query removes leading zeros from numeric data types because mathematically, 001 is the same as 1; therefore, converting the data to a string is the only way to preserve these characters for IDs, SKU numbers, or zip codes.
What You'll Need
- Microsoft Excel or Power BI Desktop with Power Query installed.
- A dataset containing a column of numbers (or text) that requires a consistent character length.
- Basic familiarity with the Power Query Editor 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.
- Identify the Target Column: Locate the column that needs leading zeros. If the column is currently formatted as a Whole Number or Decimal Number, you will notice that any existing leading zeros are already missing.
- Change Data Type to Text: This is the most critical step. Click the data type icon (e.g., 123) on the left side of the column header. Select Text from the dropdown menu. When prompted to "Replace current" or "Add new step," choose Replace current if you do not need to keep the numeric version of the column. Note that labels and menu options may vary slightly depending on your version of Excel or Power BI.
- Create a Custom Column: Navigate to the Add Column tab on the top ribbon and select Custom Column. This opens a dialog box where you can write a formula to handle the padding.
- Enter the PadStart Formula: In the Custom Column formula box, give your new column a name (e.g., "Formatted ID"). Enter the following formula:
Text.PadStart([ColumnName], 5, "0"). Replace[ColumnName]by double-clicking your actual column name from the list on the right. Replace the number5with the total length you want the final string to be. The"0"tells Power Query to use zeros as the filler character. - Verify the Results: Look at the new column. If your original value was "123" and you set the length to 5, the result should now be "00123".
- Clean Up the Dataset: Right-click the original numeric column and select Remove. You can then right-click the new padded column and select Rename to give it the original column's name.
- Apply Changes: Go to the Home tab and click Close & Apply (Power BI) or Close & Load (Excel) to bring the formatted data back into your spreadsheet or model.
Common Mistakes
The most frequent error is attempting to apply the Text.PadStart function while the column is still set to a numeric data type. Because this function is specifically designed for text strings, Power Query will return an Expression.Error stating that it cannot convert a value of type Number to type Text. Always ensure the data type icon shows "ABC" before adding the custom column.
Another common mistake is confusing the length parameter in the formula. Users often enter the number of zeros they want to add rather than the total length of the final string. For example, if you have a 3-digit number and you want to add 2 zeros to make it 5 digits, you must enter 5 in the formula, not 2. If you enter 2, Power Query will see that the string is already longer than 2 characters and will make no changes at all.
Some users try to solve this by changing the cell formatting in the Excel worksheet after loading the data (using Custom Format 00000). While this looks correct in the grid, the underlying data remains a number. If you export that data to a CSV or use it in a VLOOKUP against a text-based system, the leading zeros will vanish again. The Power Query method is the only way to ensure the zeros are physically part of the data string.
Tips
If you have a column where the required length varies based on another column's value, you can replace the static number in the Text.PadStart formula with a reference to another column. For example, Text.PadStart([ID], [RequiredLength], "0") allows for dynamic padding.
To keep your Power Query steps clean, you can avoid creating a new column entirely by editing the M-code in the Formula Bar. Instead of using "Add Custom Column," you can wrap the existing transformation step in the Table.TransformColumns function. This is more advanced but prevents your query from becoming cluttered with multiple "Removed Column" and "Renamed Column" steps.
When dealing with very large datasets, remember that converting numbers to text increases the memory footprint of the column. While negligible for a few thousand rows, it can impact performance in models with millions of rows. Only convert to text if the leading zeros are required for a specific business key or external system requirement.
Troubleshooting
If you see "Error" in every cell of your new column, the first thing to check is the data type of the source column. Go back to the step immediately preceding the Custom Column and ensure the type is set to Text. If there are null values in your source column, Text.PadStart will return a null rather than an error, but if you have combined the column with other text, you might see errors. Use the Replace Values feature to replace nulls with an empty string or a placeholder before padding.
If the zeros are not appearing, double-check your length argument. If your data contains hidden spaces (e.g., "123 "), Power Query counts those spaces as characters. A value of "123 " is 4 characters long. If your padding length is 4, no zeros will be added. To fix this, right-click the column and select Transform > Trim to remove all leading and trailing whitespace before applying the padding formula.
If you find that the zeros disappear again after you click Close & Load, check the data type settings in the destination. In Excel, if the column is formatted as "General," Excel may automatically convert the text back to a number upon loading. To prevent this, select the destination column in Excel and set the format to "Text" before loading the data from Power Query.
Frequently Asked Questions
Can I add leading zeros to only some rows?
Yes. Instead of a simple Text.PadStart, use an if statement in your custom column. For example: if [Category] = "A" then Text.PadStart([ID], 5, "0") else [ID]. This ensures only specific rows are padded while others remain untouched.
Does this work for zip codes?
Yes, this is the primary use case for zip codes, especially for Northeast US codes that start with zero. Converting the zip code column to text and padding to 5 characters ensures that codes like "02108" are not truncated to "2108".
Is there a way to do this without writing a formula?
Not directly. Power Query does not have a built-in "Pad" button in the ribbon. You must use the Custom Column feature or the M-code editor to achieve this, as it requires a specific length definition that a standard menu toggle cannot provide.
What happens if the number is already longer than the padding length?
If you set the length to 5 but the value is "123456", Power Query will not truncate the number. It will leave the original value exactly as it is. Text.PadStart only adds characters; it never removes them.
Can I use a character other than zero?
Yes. The third argument in the Text.PadStart function is the padding character. If you want to pad with underscores or letters, simply change "0" to "_" or "X". Just ensure the character is enclosed in double quotes.