Smart Systems for Home

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

Power Query CombineTextByLengths Function Guide

· 5 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 CombineTextByLengths function in Power Query allows you to split a text column into multiple columns based on specified character lengths, useful for parsing fixed-width data like legacy system exports or formatted identifiers. You don’t need additional tools—just Power Query in Excel or Power BI. The function takes a list of lengths and applies them sequentially to the input text, creating one output column per length value.

What You'll Need

  • Microsoft Excel 2016 or later, or Power BI Desktop
  • A data table with at least one text column containing strings to split
  • Knowledge of the exact character lengths for each segment you want to extract

Step-by-Step

  1. Open your data in Excel or Power BI and load it into Power Query Editor. In Excel, select your data range and choose Data > From Table/Range. In Power BI, select Get Data > Excel or your source, then Load.
  2. In Power Query Editor, identify the column you want to split. Right-click its header and select Change Type > Text if it isn’t already text. This ensures the function processes it correctly.
  3. Go to the Add Column tab and select Custom Column. In the dialog box, enter a name for your new column, such as "SplitText".
  4. In the formula box, type: CombineTextByLengths([YourColumnName], {5, 3, 4}). Replace "YourColumnName" with the actual name of your text column (e.g., [IDNumber]) and replace the numbers in the list with your desired segment lengths. For example, {5, 3, 4} means extract the first 5 characters, then the next 3, then the next 4.
  5. Click OK. A new column will appear with a list of text segments. Each item in the list corresponds to a segment defined by your lengths.
  6. Click the Expand icon (two arrows) next to the new column header. Select "Expand to New Columns". This will create individual columns for each segment, named Column1, Column2, etc.
  7. Rename the new columns to meaningful labels like "Prefix", "Code", "Suffix" by double-clicking their headers and typing new names.
  8. Remove the original text column if it’s no longer needed. Right-click its header and choose Remove.
  9. Click Close & Load to apply changes and return the updated data to Excel or Power BI.

Common Mistakes

  • Using incorrect segment lengths that don’t match the actual data structure, resulting in truncated or misaligned output. Always validate the structure of your input text before defining lengths.
  • Forgetting to convert the source column to Text type first, which may cause errors if the column contains numbers or nulls.
  • Supplying an empty list or zero-length segments like {0, 5}—this causes the function to fail or return null values.
  • Miscounting characters in the source data. For example, if your data has leading spaces or hidden characters, they count toward the length. Use Trim() before CombineTextByLengths to remove them.
  • Assuming the function works on multiple rows at once without applying it per row. It always operates row-by-row; it does not aggregate across rows.

Tips

  • Use Text.Length([ColumnName]) to check the total length of your input strings before defining segments. This helps avoid mismatches.
  • Combine CombineTextByLengths with Text.Trim() to clean up whitespace: CombineTextByLengths(Text.Trim([RawData]), {4, 6, 2}).
  • If your data has variable-length segments, use Text.Start(), Text.Middle(), and Text.End() instead. CombineTextByLengths only works with fixed, predetermined lengths.
  • Test your function on a small subset of data (e.g., 5 rows) before applying it to your full dataset to verify alignment.
  • Use the View > Advanced Editor to inspect the generated M code and ensure the syntax matches your intent.
  • When parsing date or numeric segments, convert the resulting columns to their proper type after splitting: select the column > Transform > Data Type > Date or Whole Number.

Troubleshooting

  • Error: "Expression.Error: The field 'ColumnName' was not found." Verify the column name in your formula exactly matches the column header in Power Query. Check for typos or extra spaces. Use the dropdown in the formula bar to auto-fill the column name.
  • Error: "Expression.Error: The value provided is not a list of numbers." Ensure the second argument is a list of integers enclosed in curly braces, like {3, 5}, not text like "3, 5" or a single number without braces.
  • Output shows null or blank values. Check if the input text is shorter than the sum of your specified lengths. The function returns null if the string is too short. Use if statements to handle this: if Text.Length([Data]) >= 12 then CombineTextByLengths([Data], {5, 3, 4}) else null.
  • Columns are misaligned after expanding. Recheck your list of lengths. If your data has inconsistent lengths, you may need to clean or pad the data first using Text.PadStart() or Text.PadEnd().
  • Function not recognized. CombineTextByLengths is available in Power Query from Excel 2019 and Power BI Desktop (2020 and later). Update your software if you’re on an older version and don’t see the function.

Frequently Asked Questions

Can CombineTextByLengths split text by delimiters like commas?

No, it only splits by character count. Use Text.Split() for delimiter-based splitting.

What happens if the text is shorter than the total length specified?

The function returns null for any segment that cannot be fully extracted. Always validate input length before applying.

Can I use CombineTextByLengths on multiple columns at once?

No, it works on one column per transformation. Apply it separately to each column you need to split.

Does this function preserve leading zeros in numeric segments?

Yes, because it treats everything as text. The output will retain leading zeros unless you convert the column to a number afterward.

Is CombineTextByLengths case-sensitive?

No, it doesn’t evaluate content—only length. Case doesn’t affect extraction.

Can I use this function in Power Query Online or Excel Online?

No, it is not available in Power Query Online or Excel for the web. Use desktop versions only.