Leading Zeros in Power Query: Step-by-Step Guide
In this article
Power Query strips leading zeros because it looks at text like "007" and decides it is the number 7, so when it converts that column back it saves "7" instead of "007". The fix is to keep the value as text and control the conversion yourself: either prefix each value with zeros using Text.PadStart, or, if you are formatting a number, convert to text first and then pad. Nothing needs installing, but you do need to know two things before you start: the column's current data type (text versus whole number or decimal number), and the exact length your finished value must be, because every method here relies on you telling Power Query a target width.
What You'll Need
- Excel with Power Query, or Power BI Desktop, with the data already loaded into the Power Query Editor
- Comfort opening the Power Query Editor (in Excel: Data tab, then Get Data, or Data > Queries & Connections to edit an existing query)
- The required length of the finished field, for example 5 characters for product codes or 9 for employee IDs
- An idea of which columns are legitimately text and which are true numbers, since you should never pad a column you plan to sum
Step-by-Step
Menu and ribbon labels can differ slightly between Excel versions, Power BI Desktop versions and regions, so treat the paths below as a guide rather than a guarantee of exact wording.
- Open the query. In Excel, click the Data tab and choose Queries & Connections, then right-click your query and pick Edit. In Power BI Desktop, click Transform Data on the Home ribbon. The Power Query Editor opens in its own window.
- Check the current data type. Look at the small icon to the left of each column header. If it shows a number icon (123 or 1.2), Power Query has already treated the values as numbers and the zeros were discarded on import. If it shows an ABC icon, the values are still text and the zeros you see are real characters.
- Fix the type at the source step first. Select the column, then in the Home tab use the Data Type dropdown (in the Transform tab it appears as Data Type as well) and choose Text. Do this at the earliest step you can. If you change the type at the final step, the earlier import step has already stripped zeros, so the damage is done; instead find the step where the column is typed as a number and change that step's type to Text there.
- Add the padding step. With the column selected, open the Transform tab and look for Format > Add Prefix, or use Add Column > Custom Column if you prefer a formula. For exact control, Custom Column is the better route.
- Write the formula. In the Custom Column dialog, name the column something like Padded Code and enter a formula of the form
Text.PadStart([Code], 5, "0"). The first argument is your column, the second is the total width you want, and the third is the pad character. UseText.From([Code])inside it if the column is still numeric:Text.PadStart(Text.From([Code]), 5, "0"). - Confirm the result. Click OK, then look at the preview. "7" becomes "00007", "42" becomes "00042" and a value that is already five characters long is left unchanged. PadStart only adds characters; it never truncates, so a "123456" stays as it is in a 5-wide request.
- Remove the original column if you no longer need it. Right-click the old column header and choose Remove, or keep it for auditing. Removing it early keeps the applied steps readable.
- Set the new column's type to Text and protect it. Right-click the new column header, choose Change Type > Text if the icon is not already ABC. This matters because if the column later gets typed as a number, the zeros disappear again at refresh time.
- Clean up empty values. PadStart on a null returns an error. Wrap it:
if [Code] = null then null else Text.PadStart(Text.From([Code]), 5, "0"). - Load and verify. Click Close & Load (or Home > Close & Load > Close & Load To for a table on a worksheet). Check that the sheet shows the zeros as visible characters, not as a shortened number, and confirm the cell format for the column is Text rather than General.
Common Mistakes
Padding too early in the query. If an early step (often the auto-generated Changed Type step) declares the column as a number, every later text operation runs on already-stripped values. Change that early step, not the last one.
Padding a column you need to do maths on. Once a value is text, SUM and arithmetic in Excel treat it as text; some cells will be ignored by formulas. Keep a numeric copy if you still need totals, or do the totals in Power Query before converting.
Using Text.PadStart with the wrong width. The second argument is the total length, not the number of zeros to add. A width of 5 on "42" gives three zeros plus two digits; asking for 3 would leave "42" untouched because it is already longer.
Forgetting the pad character quotes. The third argument must be a text value in double quotes: "0", not 0. A bare zero produces an error.
Expecting the preview to update Excel automatically. You must close and load; the editor preview is not the worksheet.
Tips
- Reuse a helper query. Put a single function, or a small reference query containing your widths and pad formula, and call it from several tables so the rule lives in one place.
- Add a trailing safeguard:
Text.PadStart(Text.From([Code]), 5, "0")returns the value padded to at least five characters. Combine withText.End(result, 5)only if you deliberately want to trim longer values, but be aware that silently truncating an ID can collide two different records. - If your codes always have a fixed structure, consider concatenating a prefix and a padded sequence instead, which reads more clearly in the applied steps.
- Document your padding rule in the query name, such as Products (5-digit code), so the next person knows why the column is text.
- For decimal-style values such as times, split the number into whole and fractional parts and pad the whole part, rather than padding the entire string.
Troubleshooting
If the zeros still vanish after loading, the most likely cause is a number type being re-applied at the end of the query. Reopen the editor, scroll the Applied Steps and click each step in turn; the preview shows exactly where the zeros disappear, and that is the step whose type you need to change.
If you get an "Expression.Error: We cannot convert the value null to type Text", a blank or empty cell is in the column. Add the null check shown in step 9.
If the preview shows errors for some rows only, filter those rows to inspect the raw values; a stray space or non-breaking character counts as text and can make a column mixed-type. Trim with Text.Trim before padding.
If the column looks right in the editor but wrong in Excel, check the worksheet column format: set it to Text, and re-run Close & Load. If a value such as "00012" shows as 12 in the formula bar, the query is still outputting a number.
If the whole query suddenly fails to refresh after a source change, look at the Changed Type step the editor regenerates on refresh; auto-detected types are a common source of regressions, so pin text columns explicitly.
Frequently Asked Questions
Why does Power Query remove leading zeros in the first place?
Because it converts columns to a numeric type on import. Numbers do not store leading zeros, so the zeros are discarded as soon as the type becomes whole number or decimal number.
Can I just format the cell in Excel instead?
You can set a custom number format such as 00000 on a numeric column, which displays the zeros without changing the underlying value. That does not work once the data is refreshed as text, so pick one approach and stay consistent.
What is the difference between Text.PadStart and Text.PadEnd?
PadStart adds characters before the value; PadEnd adds them after. For leading zeros you want PadStart.
Does Text.PadStart ever shorten a value that is too long?
No. It only adds characters until the target width is reached and returns longer values unchanged.
How do I pad multiple columns at once?
Power Query has no bulk pad command. Either transform each column with its own Custom Column step, or write one function and apply it per column with Table.TransformColumns.
Why does my padded column break my totals?
Text values are not summed. Keep a numeric column alongside the padded text column and total that one.