Smart Systems for Home

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

Power Query Splitter.SplitTextByPositions Guide

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

Yes, you can split text into columns at specific character positions using the Splitter.SplitTextByPositions function in Power Query. This function is part of the M language and is especially useful when you have fixed-width data, such as log files, legacy database exports, or formatted reports where each field starts at a known position. Instead of using the generic "Split Column by Delimiter" or "Split Column by Number of Characters" (which splits at a fixed length repeatedly), this function lets you define exact breakpoints. For example, you might have a string where the first 5 characters are an ID, the next 10 are a name, and the next 8 are a date. Splitter.SplitTextByPositions takes a list of positions and returns a function that splits text at those positions.

What You'll Need

  • Power Query in Excel (Get & Transform Data) or Power BI (Power Query Editor). The steps are the same in both.
  • A column containing text values that have a consistent, fixed-width structure. If the data is not fixed-width, this function will produce incorrect splits.
  • Basic familiarity with the Power Query editor: you should know how to add a custom column and how to open the Advanced Editor.

Step-by-Step

  1. Load your data into Power Query. In Excel, select your data range and go to Data > From Table/Range. In Power BI, click Transform Data in the Home tab to open the Power Query Editor.
  2. Identify the split positions. Determine the character position (0-based) where each new column should begin. For example, if your string is "ABCDE12345", and you want the first column to be "ABCDE" (characters 0-4) and the second to be "12345" (characters 5-9), your positions list would be {0, 5}. Note that you do not need to include the end of the string; the function automatically uses the full length. If you want to skip characters, you can include positions that are not at the start of a field, but that will include the skipped characters in the previous column. To truly skip, you would need to split and then remove columns.
  3. Add a custom column. In Power Query, go to Add Column > Custom Column. In the dialog, give the new column a name like "SplitData".
  4. Enter the M code. In the custom column formula box, enter the following, replacing [YourColumn] with the actual column name and {0, 5, 15} with your positions:
    Splitter.SplitTextByPositions({0, 5, 15})([YourColumn])
    This will return a list of text pieces. For example, if your column is named "Data", the formula is Splitter.SplitTextByPositions({0, 5})([Data]).
  5. Click OK. The new column will appear with a list icon in each cell. Each list contains the split parts.
  6. Expand the list to new columns. Click the expand icon (the two opposing arrows) in the column header of your new column. In the expand dialog, choose "Expand to New Rows" or "Expand to New Columns". For most fixed-width splits, you'll want "Expand to New Columns". If you choose "Expand to New Rows", each split part becomes a separate row, which is rarely what you want for fixed-width data. When expanding to columns, you can optionally rename the columns in the same dialog by editing the names in the list.
  7. Remove the original column if desired. After expanding, you can delete the original column and the temporary list column if you no longer need them.

Here is a more detailed example. Suppose you have a column called "Code" with values like "US12345JohnDoe". You want to split into Country (2 chars), ID (5 chars), and Name (rest). Your positions are {0, 2, 7} because position 0 is the start, position 2 is after "US", and position 7 is after "US12345". The formula is Splitter.SplitTextByPositions({0, 2, 7})([Code]). When you expand to new columns, you get three columns: "1" (US), "2" (12345), and "3" (JohnDoe). You can then rename them.

If you have a header row that contains the field names, you can use Table.FromColumns and Table.RenameColumns in a more advanced script, but the custom column approach is the simplest for most users.

Common Mistakes

  • Using 1-based positions instead of 0-based. The function uses 0-based indexing. The first character is position 0. If you split at position 1, you will drop the first character.
  • Forgetting that the function returns a list. You cannot directly use the result as a column; you must expand it. If you see "Error" in the column, it often means you forgot to apply the function to a column value. The syntax is Splitter.SplitTextByPositions(list)(column) — note the two sets of parentheses.
  • Using positions that are not in increasing order. The list must be strictly increasing. If you use {5, 0}, you'll get an error.
  • Assuming the function handles variable-length fields. It does not. If your data has fields that vary in length, the split will be wrong. For example, if a name is sometimes 10 characters and sometimes 8, using fixed positions will chop or merge incorrectly.
  • Expanding to new rows instead of columns. This will multiply your rows and make a mess. Always choose "Expand to New Columns" for fixed-width splits.

Tips

  • Test on a small sample first. Before applying to a large dataset, create a custom column on a few rows and expand to see if the positions are correct.
  • Use a helper column to verify positions. You can add a custom column that shows the character positions using Text.PositionOf or Text.Length to double-check your indexing. For example, Text.Middle([Code], 0, 2) shows the first two characters.
  • Combine with Table.SplitColumn for a more direct approach. Instead of a custom column, you can use Table.SplitColumn in the Advanced Editor. The syntax is Table.SplitColumn(Source, "ColumnName", Splitter.SplitTextByPositions({0, 5}), {"New1", "New2"}). This splits directly into new columns without an intermediate list column. This is often cleaner for repeated use.
  • If you need to split into rows instead of columns, you can use Table.ExpandListColumn after adding the custom column, but for fixed-width data this is rarely needed.
  • Remember that the positions are relative to the start of the string. If your data has leading spaces, they count as characters. Use Text.Trim first if you want to ignore leading spaces.

Troubleshooting

If you get an "Expression.Error: The key didn't match any rows" — this usually means your column name is wrong. Check the exact column name in the formula bar. Column names are case-sensitive and may have spaces or special characters; use the correct name as it appears in the header.

If you get an error about the function not being found — ensure you are using the correct function name: Splitter.SplitTextByPositions. It is part of the standard M library, so it should always be available. If you are in a custom function, you might need to use #shared to access it, but in a normal query it works directly.

If the split produces empty strings — this happens when two positions are the same or when a position is beyond the length of the string. For example, if you have "ABC" and positions {0, 3}, the second part is empty. Check your positions against the actual string lengths. You can use Text.Length to see the length of each value.

If the expansion creates more columns than expected — count the number of positions you provided. The number of columns after expansion equals the number of positions plus one (for the trailing part). If you have 3 positions, you get 4 columns. If you only want 3 columns, you need 2 positions (the start of the second and third fields).

If the split is off by one character — you are likely using 1-based positions. Remember that position 0 is the first character. For example, to split after the first character, use position 1, not 0. To split after the fifth character, use position 5.

If you want to split every N characters but with different N per segment, this function is perfect. But if you want to split every N characters uniformly, you can use the built-in "Split Column by Number of Characters" in the ribbon, which is simpler.

Frequently Asked Questions

Can I use Splitter.SplitTextByPositions to split into rows instead of columns?

Yes, but you would need to expand to new rows. However, for fixed-width data, splitting into rows is unusual. If you need to unpivot, consider other functions like Table.Unpivot after splitting.

What is the difference between Splitter.SplitTextByPositions and Splitter.SplitTextByLengths?

Splitter.SplitTextByLengths splits by specifying the length of each segment, not the starting positions. For example, Splitter.SplitTextByLengths({2, 5}) splits the first 2 characters, then the next 5, then the rest. Splitter.SplitTextByPositions uses cumulative positions. Both are useful; choose based on which is easier to calculate for your data.

Can I use a dynamic list of positions from another column?

Yes, but it requires more advanced M code. You can use Table.AddColumn with a custom function that reads the positions from a column. For example: Table.AddColumn(Source, "Split", each Splitter.SplitTextByPositions(Text.Split([Positions], ",") |> List.Transform(Number.FromText))([Data])). This is advanced; ensure your positions column is correctly formatted.

Does this function work with numbers or dates?

The function works on text. If your column is numeric or date, Power Query will implicitly convert it to text when you reference it in the custom column formula, but it's safer to explicitly convert using Text.From if needed. For example: Splitter.SplitTextByPositions({0, 2})(Text.From([NumberColumn])).

Why do I get a list in my column instead of separate columns?

Because the function returns a list. You must expand it using the expand button in the column header. If you don't see the expand button, make sure the column data type is "List" (it should be automatically). If you clicked "Expand to New Rows" by mistake, you can undo (Ctrl+Z) and try again.

Can I use this function in a custom function for multiple columns?

Yes. You can wrap it in a function that takes a text value and returns a list, then apply it to multiple columns using Table.TransformColumns. For example: Table.TransformColumns(Source, {"Column1", each Splitter.SplitTextByPositions({0, 3})(_)}).