Smart Systems for Home

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

Power Query Text.Length 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.Length function in Power Query is used to count the number of characters in a text string, including spaces, punctuation, and special characters. To use it, you wrap the target text or column reference inside the function: Text.Length([ColumnName]). This is essential for data validation, such as ensuring ID numbers are the correct length or identifying truncated entries in a dataset.

What You'll Need

  • Microsoft Excel or Power BI Desktop.
  • A dataset loaded into the Power Query Editor.
  • A column containing text data (or data that can be converted to text).

Step-by-Step

  1. Open 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.
  2. Select Your Table: In the Queries pane on the left, click the table containing the text you wish to measure.
  3. 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 write the M formula. (Note: Menu labels may vary slightly depending on your version of Excel or Power BI).
  4. Name the Column: In the New column name field, enter a descriptive name such as "Character Count" or "Text Length".
  5. Enter the Formula: In the Custom column formula box, type Text.Length([YourColumnName]). To avoid typing errors, you can double-click the column name from the Available columns list on the right to insert it into the brackets.
  6. Verify the Preview: Look at the preview window at the bottom of the dialog box. You should see a new column containing integers representing the length of the strings in your original column.
  7. Confirm the Action: Click OK. The new column will now be added to your table.
  8. Set the Data Type: By default, the new column may be set to the "Any" data type. Click the data type icon (the small rectangle next to the column header) and select Whole Number to ensure the data is treated as a numeric value for future calculations.

Common Mistakes

One of the most frequent errors is attempting to use Text.Length on a column that is not explicitly set to the Text data type. If the column is formatted as a Number or Date, Power Query will return an Error for every row because the function expects a text string as its input. You must first change the column type to Text using the Transform tab or the column header icon.

Another common mistake is forgetting that Power Query is case-sensitive. Writing text.length or TEXT.LENGTH will result in a formula error. The function must be written exactly as Text.Length with a capital T and a capital L.

Users often overlook the impact of trailing or leading spaces. A cell that looks like it contains "Apple" but actually contains "Apple " (with a space at the end) will return a length of 6 instead of 5. This can lead to incorrect data validation results if the data has not been cleaned first.

Finally, some users try to use this function within the "Format" menu options. While there are some built-in text transformations in the ribbon, Text.Length is specifically an M-language function that requires the Custom Column dialog or the Advanced Editor to implement.

Tips

To get the most accurate results, always use the Text.Trim function before calculating length. You can nest these functions together in a single custom column formula: Text.Length(Text.Trim([ColumnName])). This removes all leading and trailing whitespace, ensuring you are counting only the actual content of the cell.

If you need to check if a string meets a specific length requirement, you can combine Text.Length with an if statement. For example: if Text.Length([ID]) = 10 then "Valid" else "Invalid". This creates a flag column that makes it easy to filter for errors in your dataset.

For advanced users, you can use the Text.Length function within a Table.AddColumn step directly in the Advanced Editor. This allows you to integrate the length calculation into a larger sequence of transformations without needing to use the GUI for every single step.

If you are dealing with null values, the function will return null. If you prefer a zero instead of a null, you can use the coalescing operator or a conditional statement: if [ColumnName] = null then 0 else Text.Length([ColumnName]).

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. Right-click the source column header, select Change Type, and choose Text. If the error persists, check the capitalization of the function name; ensure it is exactly Text.Length.

If the character counts seem higher than expected, your data likely contains hidden characters or non-printing characters (such as carriage returns or tabs). To fix this, use the Transform tab, select Replace Values, and try to identify and remove these characters, or use Text.Clean to remove non-printable characters before applying the length function.

If the custom column dialog refuses to save, check for syntax errors. Ensure that every opening parenthesis ( has a corresponding closing parenthesis ) and that the column name is enclosed in square brackets [ ]. A missing bracket is the most common cause of formula rejection in the Custom Column window.

If you are experiencing slow performance on a very large dataset (millions of rows), avoid creating multiple intermediate columns. Instead of creating one column for Trim and another for Length, combine them into one step to reduce the number of times Power Query has to iterate through the table.

Frequently Asked Questions

Does Text.Length count spaces?

Yes, the Text.Length function counts every single character within the string, including leading spaces, trailing spaces, and spaces between words.

Can I use this function on numbers?

Not directly. If you apply it to a numeric column, you will get an error. You must first convert the number to text using Text.From([ColumnName]) or by changing the column type to Text.

How do I count only specific characters?

Text.Length only provides the total count. To count specific characters, you can use a combination of Text.Length and Text.Replace. For example, to count how many times the letter "a" appears, subtract the length of the string with "a" removed from the original length.

Is there a limit to the length of text this function can process?

There is no practical character limit for Text.Length within the constraints of Power Query's memory and the underlying data source's maximum cell size.

What happens if a cell is empty (null)?

If the input is a null value, the Text.Length function returns null rather than 0. You must use a conditional statement if you want nulls to be treated as zero.

Can I use this to find the shortest or longest string in a column?

Yes. First, create the length column using Text.Length, then sort that column in ascending or descending order to identify the shortest or longest entries in your dataset.