Splitter.SplitTextByLengths

Splitter

Returns a splitter function that divides text into substrings of specified lengths.

Examples on this page use shared sample tables. View them to understand the input data before reading the examples below.

Syntax

Splitter.SplitTextByLengths(lengths as list, optional startAtEnd as nullable logical) as function

Parameters

NameTypeRequiredDescription
lengthslistYesA list of numbers specifying the character length of each resulting segment.
startAtEndlogicalNoWhen true, splits from the end of the string rather than the start. Defaults to false.

Return Value

functionA function that splits text into substrings according to the specified list of lengths.

Remarks

Splitter.SplitTextByLengths returns a splitter function that, when applied to a text value, extracts consecutive substrings of the lengths specified by the lengths list. Each element of lengths defines how many characters to take for that segment.

If the total of all lengths is less than the full string length, any remaining text is returned as an additional final element. If a length extends beyond the remaining characters, only the available characters are returned for that segment.

When startAtEnd is true, lengths are applied from right to left.

This is useful for parsing fixed-width file formats where each field has a predetermined character count.

Examples

Example 1: Split into segments of 3, 4, and 3 characters

Splitter.SplitTextByLengths({3, 4, 3})("PowerQuery")
Result
Result
1Pow,erQu,ery

Example 2: Parse a fixed-width record

let
    record = "ALICE030F",
    splitter = Splitter.SplitTextByLengths({5, 2, 1}),
    parts = splitter(record)
in
    parts
Applied Steps

The final output — a list of the three fixed-width segments extracted from the input string.

Result
1ALICE,03,0F

Example 3: Split from the end

Splitter.SplitTextByLengths({3, 3}, true)("ABCDEF")
Result
Result
1ABC,DEF

Example 4: Use with Table.SplitColumn

Table.SplitColumn(
    #table({"Code"}, {{"US1234"}}),
    "Code",
    Splitter.SplitTextByLengths({2, 4}),
    {"Country", "Number"}
)
Result
Country
Number
1US1234

Compatibility

Power BI Desktop Power BI Service Excel Desktop Excel Online Dataflows Fabric Notebooks