cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
rpiboy_1
Helper I
Helper I

Dynamically Merge Multiple Columns

Hi all, I want to dynamically merge columns in a function.

 

Let's say I have this fairly 'standard' PQ expression:

 

 

#"Inserted Merged Column" = Table.AddColumn(#"PreviousStepp", "NewColumnName", each Text.Combine({[Column1], [Column2]}, "."), type text)

 

 

The result would be:

 

Column1Column2NewColumnName
ABA.B
CDC.D

 

Now, I'd like to make this expression dynamic in a function where I pass the columns to be merged. So the code might look like this:

 

(ColumnNamesList as list) =>

#"Inserted Merged Column" = Table.AddColumn(#"PreviousStepp", "NewColumnName", each Text.Combine(ColumnNamesList, "."), type text)

 

 

The problem is that Text.Combin expects an explict column name reference bracketed i.e. '[Column1]'.

 

Whereas the list will pass a list of Text values that are not converted to expclit column references (even if brackets are included)

 

i.e. this list will not work

List
[Column1]
[Column2]

 

It will be passed as {"[Column1]", "[Column2]"} and Text.Combine will fail.

 

How do I dymically pass a list of column name references that will function with the Text.Combine and respect the 'each' iterator for the rows? Normally I would use:

 

 

Record.Field(_, varColumnName)

 

 

But I don't know how to dynamically expand that expression to iterate Record.Field over each Column in the list?

1 ACCEPTED SOLUTION
ppm1
Solution Sage
Solution Sage

Here is one way to do it. You can provide the colnames as a concatenated text string and parse it in the function.

 

//fnAddMergeColumn
let
    Source = (inputtable as table, newcolumn as text, colnames as text) => 
        let 
        
            result = Table.AddColumn(inputtable, "Concatenated", each Text.Combine(Record.ToList(Record.SelectFields(_, Text.Split(colnames, "-")) ), "."))
        in 
            result
in
    Source

ppm1_0-1669773068983.png

 

Microsoft Employee

View solution in original post

5 REPLIES 5
PhilipTreacy
Super User
Super User

Hi @rpiboy_1 

 

I don't see the advantage of creating another function to do this.  At some point you have to declare what ColumnNamesList is so not sure how 'dynamic' your desired solution is.

 

Why not just concatenate the columns ?

 

= [Column1] & "." & [Column2]

 

Regards

 

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


FYI, to close the loop on this. I did not end up writing this as a seperate function, but it was helpful to see the solution written as a stand-alone function from the perspective of parsing the code to understand how to adopt it to my own needs.

 

I was actually able to drop the 'Text.Split' as in my real function I already have the columns names as list, so conviently I'm able to just pass that list directly and it all works!

 

Thanks again!

ppm1
Solution Sage
Solution Sage

Here is one way to do it. You can provide the colnames as a concatenated text string and parse it in the function.

 

//fnAddMergeColumn
let
    Source = (inputtable as table, newcolumn as text, colnames as text) => 
        let 
        
            result = Table.AddColumn(inputtable, "Concatenated", each Text.Combine(Record.ToList(Record.SelectFields(_, Text.Split(colnames, "-")) ), "."))
        in 
            result
in
    Source

ppm1_0-1669773068983.png

 

Microsoft Employee

@ppm1 brilliant! I should be able to adopt this. Fresh eyes in the morning helps. I was missing the combination of Record.ToList, SelectFields with a Text.Split.

 

Thanks much!

ok, this has me thinking... and I think this is kinda of what your code is doing.

 

I could use my Text list of of column names to isolate the columns I'm interested in merging into a seperate table, as a Table I should be able to just merge all the associate columns, then pass the merged values as a column back into my original table through a merge. Will tackle this further in the morning.

Helpful resources

Announcements
Carousel_PBI_Wave1

2023 Release Wave 1 Plans

Power BI release plans for 2023 release wave 1 describes all new features releasing from April 2023 through September 2023.

Power BI Summit Carousel 2

Global Power BI Training

Make sure you register today for the Power BI Summit 2023. Don't miss all of the great sessions and speakers!

Thank you 2022 Review

2022 Monthly Feature Releases

We had a great 2022 with a ton of feature releases to help you drive a data culture.

Top Solution Authors
Top Kudoed Authors