cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
jorgen82 Frequent Visitor
Frequent Visitor

Power Query: Add new column with combination of columns based on their names

Hello

 

I have an XML document, for which i am expanding all its elements. What i would like to achieve next is to add a new column, on which i combine the values for any other column where e.g. its name is ending with "group.id". Same to be done for some other columns with other names. I want to do this in order to be able to do some kind of hierarcy and relation at the end between devices, sensors etc.

 

What i did was the below, which will combine the columns i need, but since it uses the Table.SelectColumns, it will only return the specific columns and not the full table.

 

#"step1" = Table.ColumnNames(Source),
#"step2" = Table.SelectColumns(Source,List.Select(#"step1", each Text.End(_,8)="group.id")),
#"Added Custom" = Table.AddColumn(#"step2", "Groups", each Text.Combine(List.Transform(List.Skip(Record.FieldValues(_), each _ =""),Text.From),",")),

 

Is there a way to achive this? Of cource i can duplicate the base table, add the new column there and then merge it, but since it will need a lot of time, i am wondering if there is a way to achive this in one query.

 

I hope i explained it adequately...

 

Thanks

George

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: Power Query: Add new column with combination of columns based on their names

@jorgen82

 

Try this one

#"step1" = Table.ColumnNames(Source),

#"Added Custom" = Table.AddColumn(Source, "Groups", each
Text.Combine(
List.Transform(List.Skip(
Record.FieldValues(Record.SelectFields(_,List.Select(Record.FieldNames(_), each Text.End(_,8)="group.id"))), each _ =""),Text.From),
",")
)

3 REPLIES 3
Super User
Super User

Re: Power Query: Add new column with combination of columns based on their names

@jorgen82

 

Try this one

#"step1" = Table.ColumnNames(Source),

#"Added Custom" = Table.AddColumn(Source, "Groups", each
Text.Combine(
List.Transform(List.Skip(
Record.FieldValues(Record.SelectFields(_,List.Select(Record.FieldNames(_), each Text.End(_,8)="group.id"))), each _ =""),Text.From),
",")
)

Super User
Super User

Re: Power Query: Add new column with combination of columns based on their names

@jorgen82

 

Basically I am using the same logic

but instead of Table.SelectColumns I am using the equivalent Record.SelectFields function

 

Please see the attached Excel file as well

gdfg.png

jorgen82 Frequent Visitor
Frequent Visitor

Re: Power Query: Add new column with combination of columns based on their names

@Zubair_Muhammad  thank you very very much for your help.