Smart Systems for Home

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

Power Query Splitter.SplitTextByRanges: A How-To Guide

· 4 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

Splitter.SplitTextByRanges is a Power Query function that splits a text value into a list of substrings based on specified start positions and lengths. It is useful for extracting fixed-width fields from strings, such as parsing legacy data files or codes with consistent structure. To use it, you need Power Query available in Excel, Power BI, or another Microsoft tool that supports it. No special setup is required beyond a basic understanding of Power Query's M language and the concept of lists.

What You'll Need

  • Access to Power Query (in Excel 2016+ or Power BI Desktop).
  • A text column or value to split.
  • Knowledge of the start positions and lengths of the substrings you want to extract.
  • Basic familiarity with Power Query's formula bar and M functions.

Step-by-Step

  1. Open Power Query Editor. In Excel, go to Data > Get Data > Launch Power Query Editor. In Power BI Desktop, click Transform Data. The exact menu labels may vary slightly by version or region.
  2. Load your data. Import the table or text containing the strings you want to split. For example, load a table with a column named "Code" that contains fixed-width values like "AB123456".
  3. Add a custom column. Go to Add Column > Custom Column. In the formula box, you will write the Splitter.SplitTextByRanges function.
  4. Understand the function syntax. The function takes two arguments: a list of ranges and an optional default value. Each range is a list of two numbers: {start, length}. Start is zero-based (the first character is at position 0). Length is the number of characters to extract.
  5. Write the formula. This returns a list of two text values: the first two characters and the next four characters. Note the double parentheses: the first call returns a function that you then apply to the text value.
  6. Expand the list. The custom column will contain lists. To extract the individual values, click the expand icon next to the column header and choose "Expand to New Rows" or "Extract Values". Alternatively, use the formula bar to combine the list into a single text with Text.Combine, or access individual items with {0} and {1}.
  7. Adjust ranges as needed. If your text has variable lengths, ensure the ranges do not exceed the string length; otherwise, the function returns null for out-of-range segments. You can use Text.Length to check, or handle nulls later.
  8. Apply and close. Click OK, then Close & Load to apply the changes.

Common Mistakes

  • Using 1-based indexing. Power Query's Splitter.SplitTextByRanges uses zero-based positions. The first character is at index 0, not 1.
  • Forgetting the double parentheses. The function is curried: Splitter.SplitTextByRanges(ranges) returns a function that takes the text. You must call it with the text in a second set of parentheses.
  • Incorrect range lengths. If a range's start plus length exceeds the text length, the result for that segment is null. Ensure your ranges are valid for all rows, or handle nulls.
  • Not expanding the list. The output is a list, not a single text. You need to expand or extract the values to use them in further transformations.
  • Assuming the function splits by delimiter. This function splits by fixed positions, not by delimiters. For delimiters, use Splitter.SplitTextByDelimiter.

Tips

  • Test your ranges on a few sample values before applying to the whole column.
  • Use Text.Length to dynamically determine lengths if your strings vary, but Splitter.SplitTextByRanges requires fixed ranges. For variable lengths, consider other functions like Text.Range in a custom column.
  • Combine with List.Transform to apply the same splitting logic to multiple columns.
  • If you need to extract a single substring, Text.Middle or Text.Start may be simpler.
  • Document your ranges in a separate query or comment for maintainability.

Troubleshooting

If you get an error like "We cannot convert a value of type List to type Text", check that you are applying the function correctly: Splitter.SplitTextByRanges(ranges)(text). If the result is a list of nulls, verify that your start and length values are within the text's length. If the custom column shows "Error", click the error to see details; common causes include invalid range lists (e.g., not a list of lists) or non-text input. Ensure the input column is of type text. If the function is not recognized, confirm you are using a version of Power Query that supports it (it has been available for many versions). For dynamic ranges based on text length, you may need to write a custom function using Text.Range instead.

Frequently Asked Questions

What is the difference between Splitter.SplitTextByRanges and Splitter.SplitTextByDelimiter?

SplitTextByRanges splits by fixed character positions (start and length), while SplitTextByDelimiter splits at each occurrence of a specified delimiter string.

Can I use Splitter.SplitTextByRanges to extract multiple fields from a fixed-width file?

Yes, it is designed for that. Provide a list of ranges for each field, and the function returns a list of the extracted substrings.

What happens if a range exceeds the text length?

The function returns null for that segment. You can handle nulls with subsequent steps like replacing values or filtering.

Is the start position zero-based or one-based?

Zero-based. The first character is at position 0.

Can I use this function with a column that contains null values?

If the input text is null, the function returns null. You may need to filter out nulls or handle them before applying the split.

Does Splitter.SplitTextByRanges work in Power Query Online?

Yes, it is part of the standard M library and should work in any environment that supports Power Query.