Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
jorgen82
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
Zubair_Muhammad
Community Champion
Community Champion

@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),
",")
)


Regards
Zubair

Please try my custom visuals

View solution in original post

3 REPLIES 3
Zubair_Muhammad
Community Champion
Community Champion

@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),
",")
)


Regards
Zubair

Please try my custom visuals

@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


Regards
Zubair

Please try my custom visuals

@Zubair_Muhammad  thank you very very much for your help.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.