Power Query CombineTextByPositions: How-To Guide
In this article
To use Combiner.CombineTextByPositions in Power Query, you call it with a list of positions and a list of texts, and it returns a single text where each text is placed at the corresponding position. For example, Combiner.CombineTextByPositions({0, 5}, {"Hello", "World"}) returns "HelloWorld" because "Hello" starts at position 0 and "World" starts at position 5. This function is part of the M language's Combiner library and is typically used with Table.CombineColumns to merge columns at fixed character offsets, such as rebuilding a fixed-width file layout or padding values into a specific format.
What You'll Need
- Power Query Editor open in Excel, Power BI Desktop, or another host that supports M.
- A table loaded into Power Query with at least two text columns you want to combine.
- Basic familiarity with the formula bar and the Advanced Editor (optional but helpful for custom steps).
- No additional software or licenses are required.
Step-by-Step
- Load your data into Power Query. In Excel, select your table and go to Data > From Table/Range. In Power BI Desktop, use Home > Transform data to open the Power Query Editor. Ensure the columns you want to combine are of type Text; if not, right-click the column header and choose Change Type > Text.
- Decide the target positions. Determine the character position (0-based) where each source text should start in the final string. For example, if you want to place the first column at position 0 and the second at position 10, your positions list is
{0, 10}. Positions must be non-negative integers and should not overlap unless you intend to overwrite characters. - Add a custom column (optional). If you want to test the function on a single row before applying it to the whole column, go to Add Column > Custom Column. Name it something like "Combined" and enter a formula such as:
= Combiner.CombineTextByPositions({0, 10}, {[Column1], [Column2]})
Replace Column1 and Column2 with your actual column names. Click OK. This creates a new column with the combined text for each row. - Use Table.CombineColumns for a column-level operation. If you prefer to replace the original columns with a single combined column, select the columns you want to combine (hold Ctrl to select multiple), then go to Transform > Merge Columns. In the Merge Columns dialog, choose Custom as the separator, but since CombineTextByPositions does not use a separator, you cannot directly use this dialog. Instead, use the Advanced Editor or a custom step. A common approach is to add a custom column as in step 3, then remove the original columns.
- Apply the function via the Advanced Editor (for advanced users). Open the Advanced Editor (Home > Advanced Editor). Locate the step where you want to add the combination. You can add a step like:
Combined = Table.AddColumn(PreviousStep, "Combined", each Combiner.CombineTextByPositions({0, 10}, {[Column1], [Column2]}))
Then remove the original columns if desired. Be careful to reference the correct previous step name. - Handle null values. If any source text is null, Combiner.CombineTextByPositions treats it as an empty string. To avoid unexpected results, replace nulls with empty text using Table.ReplaceValue or the Replace Values feature before combining.
- Check the output. After adding the combined column, review a few rows to ensure the positions produce the expected result. If the output looks wrong, verify that the positions list length matches the texts list length and that positions are in ascending order (though not strictly required, it helps avoid confusion).
- Load the result. Once satisfied, close and load the query to your worksheet or data model. The combined column will be available for further analysis.
Common Mistakes
- Using 1-based positions. The function uses 0-based indexing, so the first character is at position 0, not 1. Using 1 will shift all text one character to the right.
- Mismatched list lengths. The number of positions must equal the number of texts. If you provide three positions but only two texts, you will get an error.
- Overlapping positions. If two texts are placed at positions that overlap, the later text overwrites the earlier one. For example, positions {0, 2} with texts {"abc", "def"} results in "abdef" because "def" starts at index 2, overwriting the "c".
- Forgetting to convert numbers to text. The function expects text values. If your columns contain numbers, convert them to text first using Text.From or change the column type.
- Assuming the function pads with spaces. It does not insert any characters between texts; it only places them at the specified positions. If you need padding, you must include spaces in your text values or use a different approach.
Tips
- Use
Text.PadStartorText.PadEndon individual columns before combining if you need fixed-width fields with padding. - Test your positions with a small sample of rows using a custom column before applying to the entire dataset.
- Remember that positions are measured in characters, not bytes, so Unicode characters count as one position each.
- If you need to combine more than two columns, simply extend the lists:
{0, 10, 20}and{[Col1], [Col2], [Col3]}. - Document your position logic in a comment or a separate query step name for maintainability.
Troubleshooting
If you encounter an error like "We cannot convert a value of type List to type Text", check that you are passing a list of texts as the second argument, not a single text. If the output is truncated or missing characters, verify that your positions are within the length of the resulting string; the function does not extend the string with spaces beyond the last placed text. If you see unexpected overwriting, review your positions for overlaps and adjust them to be sequential based on the length of each text. For null values, replace them with empty strings using the Replace Values feature or Table.ReplaceValue before combining. If the function is not recognized, ensure you are using it in a context where the Combiner library is available (it is part of the standard M library, so it should work in any Power Query environment).
Frequently Asked Questions
What is the difference between Combiner.CombineTextByPositions and Text.Combine?
Text.Combine concatenates texts with an optional separator, while Combiner.CombineTextByPositions places texts at specific character positions, allowing overwriting and precise placement without separators.
Can I use Combiner.CombineTextByPositions with more than two columns?
Yes, provide a list of positions and a list of texts of the same length. For example, {0, 5, 10} and {"A", "B", "C"}.
Does the function insert spaces automatically?
No, it does not add any characters. If you need spaces, include them in your text values or pad the texts before combining.
What happens if a position is beyond the length of the current string?
The function will pad with spaces up to that position before placing the text. For example, combining at position 5 with an empty initial string results in five spaces followed by the text.
Is Combiner.CombineTextByPositions available in all Power Query versions?
It is part of the standard M library and is available in Excel 2016 and later, Power BI Desktop, and other hosts that support Power Query. Menu labels may differ slightly by version or region.