Splitter.SplitTextByRepeatedLengths in Power Query
In this article
The Splitter.SplitTextByRepeatedLengths function in Power Query (used in Excel, Power BI and other tools with the M language) splits a text value into chunks of a fixed length, repeating that length until the whole string is consumed. For example, a length of 4 turns "ABCDEFGH" into {"ABCD", "EFGH"}. You typically use it inside Table.SplitColumn or Text.Split-style operations when you need fixed-width parsing instead of splitting on a delimiter.
What You'll Need
- Excel with Power Query built in, or Power BI Desktop, or another tool that supports the M language.
- A table with a text column containing fixed-width values (such as codes, dates and amounts packed into one string).
- Basic familiarity with the Power Query Editor and the Advanced Editor.
Step-by-Step
- Load your data into Power Query. In Excel, select the data, then go to Data > From Table/Range. In Power BI Desktop, use Home > Transform Data.
- Select the column you want to split by clicking its header.
- Open the Advanced Editor (Home > Advanced Editor) so you can add a custom splitting step. Note that menu labels can differ slightly between versions and tools, but the Advanced Editor exists in all of them.
- Add a step that calls the function. The function takes a single argument, the length of each chunk, and returns a splitter function, which you then pass to
Table.SplitColumn. For example, to split a column called Code into 3-character chunks:Split = Table.SplitColumn(PreviousStep, "Code", Splitter.SplitTextByRepeatedLengths(3), {"Code.1", "Code.2", "Code.3"}) - Replace PreviousStep with the actual name of the previous step in your query.
- Set the number of output column names in the list to match how many chunks your longest value will produce. If your values are always 9 characters and the length is 3, you need three names.
- Click Done to apply. Each chunk appears in its own column.
- If you prefer not to create new columns, use the splitter with
Text.Splitstyle logic inside a custom column instead:Splitter.SplitTextByRepeatedLengths(4)([MyText])returns a list of chunks you can expand or combine. - Clean up: set correct data types on the new columns (Transform > Data Type), remove the original column if you no longer need it, and close and load.
Common Mistakes
- Passing a number directly to
Table.SplitColumninstead of calling the splitter function. You must writeSplitter.SplitTextByRepeatedLengths(3), with the length in its own parentheses, so it returns the splitter. - Providing too few new column names. If a value produces more chunks than names you supplied, the extra chunks are dropped. Count your maximum string length divided by the chunk size and supply at least that many names.
- Using it on non-text columns. The splitter works on text values only; convert numbers or dates to text first with
Text.Fromor by changing the column type to Text. - Confusing it with
Splitter.SplitTextByLengths, which takes a list of different lengths for each chunk.SplitTextByRepeatedLengthsrepeats one length over and over. - Expecting padding. If a string does not divide evenly by the length, the last chunk is simply shorter; nothing is padded. Pad with
Text.PadEndbeforehand if you need equal chunks.
Tips
- For classic fixed-width files where each field has a different width, use
Splitter.SplitTextByLengths({2, 5, 10})instead, which accepts a list of varying lengths. - You can generate column names programmatically with
List.Transform({1..n}, each "Chunk." & Text.From(_))to avoid typing them manually. - Nulls in the column stay null after splitting; you may want to replace them first with Replace Values.
- Test the splitter on a sample: add a custom column with
Splitter.SplitTextByRepeatedLengths(2)([MyText])and inspect the list output before splitting the whole table. - The chunk length must be a positive number; a length of zero or a negative number will raise an error.
Troubleshooting
Expression error: the name of the column was not found. You referenced a column that does not exist or has a different spelling. Column names are case-sensitive in M; check the exact name in the editor.
Fewer chunks than expected. You likely supplied too few output column names, or some values contain leading or trailing spaces that shift characters. Trim the column first with Transform > Format > Trim.
Error about applying a number where a function was expected. You passed a plain number to Table.SplitColumn. Wrap it: Splitter.SplitTextByRepeatedLengths(n).
The wrong number of columns appears. Count the maximum length of your values with a custom column using Text.Length([MyText]), divide by the chunk size, round up, and supply that many names.
Everything works but types are wrong after loading. The split output is always text. Set data types explicitly on the new columns before loading.
If the step still fails, delete it in Applied Steps, re-add it carefully, and verify each parenthesis pair in the formula bar. If the query itself is broken, refresh the preview (Home > Refresh Preview) or reopen the Power Query Editor.
Frequently Asked Questions
What is the difference between SplitTextByRepeatedLengths and SplitTextByLengths?
SplitTextByRepeatedLengths takes a single number and uses that same chunk length for the whole string. SplitTextByLengths takes a list of lengths, one per chunk, which is useful when each field in a fixed-width record has a different width.
Can I split by a delimiter and fixed length at the same time?
Not with one splitter function. Do two passes: first split on the delimiter with Splitter.SplitTextByDelimiter, then apply the length splitter to the resulting column.
Does it pad short strings?
No. The final chunk is shorter if the text does not divide evenly. Use Text.PadEnd to add filler characters first if you need uniform chunks.
Can I use it directly in a custom column?
Yes. Splitter.SplitTextByRepeatedLengths(3)([MyText]) returns a list of strings that you can expand into rows or columns, or process further with list functions.
Does this work in both Excel and Power BI?
Yes. The function is part of the shared M language, so it behaves identically in Excel's Power Query, Power BI Desktop and other tools that run M queries.
What happens to null values?
Nulls are passed through as null rather than producing empty chunks, so the new columns will contain nulls for those rows.