Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Text.PadEnd Function: Guide and Examples

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The Text.PadEnd function in Power Query is used to append a specific character or string to the end of an existing text value until the resulting string reaches a specified total length. This is primarily used for data standardization, such as ensuring all product IDs have a uniform length for database uploads or formatting fixed-width text files. To use it, you provide the original text, the desired total length, and the character you want to use for padding.

What You'll Need

  • Microsoft Excel or Power BI Desktop with Power Query (Get & Transform) enabled.
  • A dataset containing a text column that requires uniform length (e.g., account numbers, SKUs, or codes).
  • Basic familiarity with the Power Query Editor interface and the "Add Custom Column" feature.

Step-by-Step

  1. Open Power Query Editor: In Excel, go to the Data tab and select Get Data or launch an existing query. In Power BI, click Transform Data on the Home ribbon.
  2. Identify the Target Column: Locate the column containing the text you wish to pad. Ensure the data type is set to Text. If the column is currently set to Whole Number or Decimal, right-click the column header, select Change Type, and choose Text. The Text.PadEnd function will return an error if applied to non-text data types.
  3. Add a Custom Column: Navigate to the Add Column tab on the top ribbon and select Custom Column. This opens the Custom Column dialog box where you can write your M formula.
  4. Enter the Formula: In the Custom column formula box, type the following syntax: Text.PadEnd([ColumnName], length, "character").
    • Replace [ColumnName] by double-clicking the name of your target column from the available columns list on the right.
    • Replace length with the total number of characters you want the final string to be (e.g., 10).
    • Replace "character" with the specific character you want to append, enclosed in double quotes (e.g., "0" or " ").
    Example: Text.PadEnd([ProductID], 8, "0") will take a product ID like "123" and turn it into "12300000".
  5. Verify the Preview: Look at the preview window in the Custom Column dialog. If the formula is correct, you will see the padded values in the sample rows. If you see an "Error" message, double-check that your column is typed as Text and that you used double quotes around your padding character.
  6. Confirm and Name: Give your new column a descriptive name, such as "Padded Product ID", and click OK.
  7. Clean Up: If you no longer need the original unpadded column, right-click the original column header and select Remove.
  8. Apply Changes: Go to the Home tab and click Close & Apply to load the standardized data back into your Excel sheet or Power BI model.

Common Mistakes

One of the most frequent errors is attempting to use Text.PadEnd on a numeric column. In Power Query, functions are strictly typed. If your column contains numbers, the function cannot "append" a character because it sees the value as a mathematical entity rather than a string of characters. You must explicitly convert the column to Text first.

Another common mistake is confusing Text.PadEnd with Text.PadStart. While Text.PadEnd adds characters to the right side of the string, Text.PadStart adds them to the left. Using the wrong one will result in data that is technically the correct length but logically incorrect for your system (e.g., turning "123" into "00000123" instead of "12300000").

Users also frequently forget the double quotes around the padding character. In the M language, any literal text string must be enclosed in quotes. Writing Text.PadEnd([ID], 5, 0) will cause an error because Power Query looks for a variable named 0 instead of the character "0".

Tips

For dynamic padding, you can replace the hard-coded length number with a reference to another column or a parameter. This is useful if different categories of data require different lengths. For example, if you have a "LengthRequirement" column, your formula would look like Text.PadEnd([ID], [LengthRequirement], "0").

If you need to pad with a space to create a fixed-width file for legacy system imports, use " " as your padding character. While spaces are invisible in the Power Query preview, you can verify they are there by clicking on a cell in the preview and checking the formula bar or by loading the data into Excel and using the =LEN() function.

To handle null values, wrap your function in an if statement or use the coalesce operator (?? in newer versions). A null value passed into Text.PadEnd will typically return a null result, which might break subsequent steps in your data pipeline. Use Text.PadEnd(Text.From([Column] ?? ""), 10, "0") to ensure nulls are treated as empty strings before padding.

Troubleshooting

If the function returns an "Expression.Error: We cannot convert the value to type Text," the issue is almost certainly the data type. Even if the column looks like text, check the icon to the left of the column header. If it shows "123", it is a number. Change it to "ABC" (Text) and try again.

If the padding does not seem to be happening, check if the existing text is already equal to or longer than the length specified in the formula. Text.PadEnd only adds characters if the current length is less than the target length. If your string is 10 characters long and you set the length to 10, no characters will be added. If the string is 12 characters long, the function will return the original 12 characters without truncating them.

If you encounter a syntax error (red underline in the formula box), check for case sensitivity. Power Query M is case-sensitive. Writing text.padend or Text.Padend will not work; it must be exactly Text.PadEnd.

Frequently Asked Questions

Does Text.PadEnd truncate text that is too long?

No, Text.PadEnd does not truncate. If the input text is already longer than the specified length, the function returns the original text exactly as it is. To force a specific length by cutting off extra characters, you would need to combine this with Text.Start.

Can I pad with multiple characters at once?

The padding character argument accepts a string. If you provide a multi-character string like "ab", Power Query will repeat that sequence until the total length is reached. If the length cannot be reached exactly by repeating the sequence, the final sequence will be truncated to fit the exact total length.

Is there a way to pad based on the longest item in the column?

Yes, but it requires a two-step process. First, create a separate step to find the maximum length using List.Max(List.Transform(TableName[ColumnName], each Text.Length(_))). Then, use that resulting value as the length argument in your Text.PadEnd formula.

How does Text.PadEnd handle nulls?

If the input value is null, the function returns null. It does not treat a null as an empty string automatically. You must use Text.From() or a conditional if statement to handle nulls if you want them to be padded.

Can I use this function in a calculated column in DAX?

No, Text.PadEnd is a Power Query (M) function. In DAX, you would achieve similar results using a combination of FORMAT or by concatenating the value with a string of spaces/zeros and then using the LEFT function to trim it to the desired length.