Smart Systems for Home

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

Power Query Combiner.CombineTextByRanges: How to Use

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

Combiner.CombineTextByRanges is a Power Query (M) function that rebuilds a text value by pulling specific character ranges from a source string and placing them in a new order. You give it a list of ranges—each range is a start position and an optional length—and it returns a single text value made of those slices. It is useful when fixed-width data (legacy exports, bank files, government feeds) needs to be split, reordered, or trimmed without writing custom M code. There is no dialog or button for it in the Power Query Editor; you use it by writing M in the formula bar or Advanced Editor, so you need a query that already contains a text column and a basic familiarity with the Power Query Editor.

What You'll Need

  • Power BI Desktop, Excel with Power Query, or another host that includes the Power Query engine. Menu labels can differ slightly by version and by host.
  • A query with at least one text column to work on.
  • Access to the formula bar or the Advanced Editor (Home tab in the Power Query Editor) so you can type M directly.
  • Knowledge of the exact character positions in your source text. Fixed-width files are the usual case; count positions from 1, not 0.

Step-by-Step

  1. Load your source into Power Query. For a fixed-width text file, use Get Data and pick the text/CSV connector, then choose the fixed-width option if the connector offers it. If the file is already imported, confirm the column you want to slice is type text. Select the column header and check the type icon; if it says Whole Number or Any, use Transform > Data Type > Text.
  2. Add a step you can edit. The easiest route is to duplicate the column first: right-click the column header and choose Duplicate Column. This keeps the original intact while you build the new value.
  3. Open the Advanced Editor from the Home tab. Find the step that creates your duplicated column. It will look something like Table.DuplicateColumn(PreviousStep, "Source", "Source - Copy").
  4. Replace the duplicate step with a Table.TransformColumns call that applies Combiner.CombineTextByRanges. The function takes a list of ranges, where each range is written as a list of two numbers: start position and length. For example, to take characters 1–4 and then characters 10–13 from each value, write . The underscore refers to the current cell value.
  5. Click Done. Power Query evaluates the step. Each row now contains a new text value made of the requested slices, concatenated in the order you listed them.
  6. To reorder without duplicating, you can apply the function to the original column instead: . This overwrites the column, so duplicate first if you need the original.
  7. To omit a length and take everything from the start position to the end of the string, use a null in place of the length: {10, null}. This is handy for trailing free-text fields where the length varies row by row.
  8. Check the result against a few known rows. Compare the output to the raw source text in a temporary column or in the source file. Pay attention to rows where the source string is shorter than the start position; those return an empty string for that range rather than an error.
  9. Rename the new column to something meaningful, then apply any further cleaning (Trim, Clean, change type) as separate steps so the logic stays readable.
  10. Load the query to your worksheet or data model once the output matches expectations.

Common Mistakes

The most frequent error is counting from 0. M text functions count characters from 1, so the first character is position 1, not 0. A range of {0, 4} will not behave the way most people expect.

Second, ranges are written as lists inside a list: . Writing {1, 4, 10, 4} produces a different, incorrect result because the function reads it as two ranges with missing values.

Third, forgetting the type argument in Table.TransformColumns. Without type text, Power Query may infer a different type or leave the column as any, which causes errors downstream.

Fourth, overwriting the original column and then discovering you need it. Always duplicate first when you are experimenting.

Finally, assuming the function trims or cleans. It does not. If the source has trailing spaces or non-printing characters, the slices will include them. Add a Trim and Clean step afterward.

Tips

Build the range list in a separate query or a small table if you have many fixed-width layouts. You can then reference it, which makes maintenance easier than editing M code each time a layout changes.

Use a comment in the Advanced Editor to record what each range represents, for example // {1,4} = account code. Future you will thank present you.

Test on a single row first by filtering the query down to one record, then remove the filter once the output is correct. This shortens the feedback loop considerably.

If the source file has a header row with different widths, exclude it before applying the transformation, or the header will be sliced incorrectly and may produce confusing output.

Troubleshooting

If the step returns an error saying the value cannot be converted, check that the column is text. Numbers and dates need to be converted to text first with Transform > Data Type > Text.

If the output is empty for every row, your start positions are probably beyond the length of the strings, or you counted from 0. Recheck the positions against a known sample.

If only some rows are wrong, compare the lengths of the source strings. Fixed-width files sometimes contain short records, and a start position past the end returns an empty string rather than an error, which can look like missing data.

If the Advanced Editor will not accept the step, check the brackets. Each range is a list, and the whole set is a list of lists. A missing brace is the usual culprit.

If the column type changes unexpectedly after the step, add an explicit type change step afterward rather than relying on inference.

Frequently Asked Questions

Does Combiner.CombineTextByRanges work in Excel Power Query as well as Power BI?

Yes. It is part of the M language and is available wherever the Power Query engine runs, including Excel and Power BI Desktop. The editor interface differs slightly between hosts.

Can I use null as the length to take the rest of the string?

Yes. A range written as {start, null} returns everything from the start position to the end of the text value.

What happens if a start position is beyond the end of the string?

That range returns an empty string. The function does not raise an error, so check your output against the source when rows have varying lengths.

Can I combine this with other text functions?

Yes. Wrap the result in Trim, Text.Upper, or Text.Proper in the same step, or add separate steps afterward. Keeping cleaning as separate steps makes the query easier to audit.

Is there a user interface for this function?

No. It is only available by writing M in the formula bar or Advanced Editor. There is no dialog in the Power Query Editor for building range lists.