Merge columns and skip nulls with a dynamic Power Query approach

Are you tired of legacy tools that complicate your data merging tasks?

3 min readMicrosoft Excel | Help & Support with your Formula, Macro, and VBA problems | A Reddit Community

This user's approach is exactly right, and their frustration with the table output is a sign they are thinking clearly. They have already solved the hard part: dynamically selecting columns that start with "cdp", removing nulls, and combining the remaining values with a semicolon. The code is smart, flexible, and handles any number of columns. What they are stuck on is a formatting detail, not a logic problem. That is a good place to be.

The issue is a subtle one in Power Query. When you use `Table.AddColumn` with a function that returns a list, Power Query often wraps that result in a nested table structure rather than a plain text string. The user's `Text.Combine` is correct, but the way they are calling `Record.Field` inside `List.Transform` is producing a list that Power Query treats as a record field, not a simple text value. The fix is straightforward: ensure the `Text.Combine` is applied at the row level, not as a transformation of the column list itself. A cleaner version of their code would be: `Table.AddColumn(Source, "merge", each Text.Combine(List.RemoveNulls(List.Transform(LabelColumns, (col) => Record.Field(_, col))), ";"), type text)`. The key change is moving the `type text` declaration inside the function so that Power Query knows to output a string, not a table.

For the working data professional, this is more than a syntax fix. It is a reminder that Power Query rewards precision with performance. The dynamic column selection they built is the kind of pattern that scales across datasets, months, and projects. Once they have the text output working, they can wrap this logic into a reusable function or a custom parameter. That means next month, when their source data has 12 "cdp" columns instead of 4, the query will adapt without a single edit. That is the real win: not just fixing today's problem, but building a query that stays out of your way tomorrow.

From Microsoft Excel | Help & Support with your Formula, Macro, and VBA problems | A Reddit Community

Hello! In power query, I want to merge specific columns that start with "cdp" Some columns are null values which I do not want in my final output. For example, I have 4 columns "cdp1" "cdp2" "cdp3" "cdp4" that should be merged into one column called "merge" skipping values that are null and separating each value by a semicolon. I need the code to work when there are varying amounts of columns to be merged like 3, 8, 18 columns need to be merged.

Read the original at Microsoft Excel | Help & Support with your Formula, Macro, and VBA problems | A Reddit Community