Splitter.SplitTextByLengths
Makes a function that cuts text into pieces of given lengths, one after the other: fixed-width fields.
Group: Splitters, combiners, replacers and comparers. Power Query M has it too: a query that uses it is written as in Power BI.
Syntax
Splitter.SplitTextByLengths(lengths as list, optional startAtEnd as nullable logical) as function
| Parameter | Type | Description |
|---|---|---|
lengths | list | The length of each piece, in characters. |
startAtEnd | nullable logical optional | true to work from the end of the text toward its start. |
Returns
function. A splitter: given a text, a piece for each length.
Remarks
- A splitter is a function of one text that gives a list of texts.
Table.SplitColumnandTable.FromListtake one; you can also call it yourself, as the examples do. - Lengths count characters, so the pieces line up with fixed-width files and codes.
- With startAtEnd the first length is measured from the end of the text, the next one before it; the pieces come in the text's order.
Examples
A fixed-width code
Splitter.SplitTextByLengths({2, 4, 3})("NO0150OSL")
{"NO", "0150", "OSL"}
The last fields of a code
Splitter.SplitTextByLengths({3, 4}, true)("NO0150OSL")
{"0150", "OSL"}
The first length is taken from the end, the city's three letters; then the postcode's four before them.
A fixed-width column into three
let
Codes = #table({"Code"}, {{"NO0150OSL"}, {"SE1112STO"}})
in
Table.SplitColumn(Codes, "Code", Splitter.SplitTextByLengths({2, 4, 3}), {"Country", "Postcode", "City"})
| Country | Postcode | City |
|---|---|---|
| NO | 0150 | OSL |
| SE | 1112 | STO |
Related functions
Splitter.SplitTextByPositions, Text.Middle, Table.SplitColumn.