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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
rshh
New Member

Combining header names and values for all rows into new column

Hi folks,

 

I am new in this community and I hope someone can help me with this slightly unusual problem. I did a lot of web research without success, but maybe I did not ask the right questions.

 

I got a table with product data and have to combine it into a new column that can be fed into an LLM to generate a product description based on that information. Let's say my table looks like this:

 

IDColorWeightLength
P001blue50100
P003red4090

 

Then I need to create a new column containing the column names and the corresponding values for each row.

 

IDColorWeightLengthProductInformation
P001blue50100ID: P001, Color: blue, Weight: 50, Length: 100
P003red4090ID: P003, Color: red, Weight: 40, Length: 90

 

Of course, I do not want to set up the statement to create the new column manually, since in reality I have over 40 columns of product data and that number might change in the future. If possible, columns with missing data should be skipped instead of creating an meaningless entry like "Color:".

 

Any tips and comments if and how this be done with Power Query M would be greatly appreciated.

Thanks and best regards,

René

1 ACCEPTED SOLUTION
AlienSx
Super User
Super User

hello, @rshh 

 

let
    Source = your_table,
    cols = List.Buffer(Table.ColumnNames(Source)),
    p_info = Table.AddColumn(
        Source, "ProductInformation",
        (x) => 
            [a = List.Zip({cols, Record.FieldValues(x)}),
            b = List.Select(a, (w) => w{1} <> null),
            c = List.Transform(b, (w) => w{0} & ": " & Text.From(w{1})),
            d = Text.Combine(c, ", ")][d]
    )
in
    p_info

 

View solution in original post

6 REPLIES 6
AlienSx
Super User
Super User

hello, @rshh 

 

let
    Source = your_table,
    cols = List.Buffer(Table.ColumnNames(Source)),
    p_info = Table.AddColumn(
        Source, "ProductInformation",
        (x) => 
            [a = List.Zip({cols, Record.FieldValues(x)}),
            b = List.Select(a, (w) => w{1} <> null),
            c = List.Transform(b, (w) => w{0} & ": " & Text.From(w{1})),
            d = Text.Combine(c, ", ")][d]
    )
in
    p_info

 

Hi, @AlienSx I am attempting to use the code, but have a use case to build off of this. The columns that I'd like to use this with are only a portion of the entire table. For example using the example above, say there's a total of 8 columns in the table, and I only want to capture the columns 4-8 and not include ANY of the 1-4. 

Column1-IgnoreHeaderColumn2-IgnoreHeaderColumn3-IgnoreHeaderColumn4-IgnoreHeaderIDColorWeightLength
Column1-IgnoreContent1Column2-IgnoreContent1Column3-IgnoreContent1Column4-IgnoreContent1P001blue50100
Column1-IgnoreContent2Column2-IgnoreContent2Column3-IgnoreContent2Column4-IgnoreContent2P003red4090

 

then the results would be:

Column1-IgnoreHeaderColumn2-IgnoreHeaderColumn3-IgnoreHeaderColumn4-IgnoreHeaderIDColorWeightLengthProductInformation
Column1-IgnoreContent1Column2-IgnoreContent1Column3-IgnoreContent1Column4-IgnoreContent1P001blue50100ID: P001, Color: blue, Weight: 50, Length: 100
Column1-IgnoreContent2Column2-IgnoreContent2Column3-IgnoreContent2Column4-IgnoreContent2P003red4090ID: P003, Color: red, Weight: 40, Length: 90

Hi @jballGT, check this:

 

You have to specify columns for Product Information:

dufoq3_0-1744285254624.png

 

Output

dufoq3_1-1744285282037.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs7PKc3NM9T1TM/LL0p1zs8rSc0rMVTSgUoY4ZIwxiVhgikRYGAAopJySlOBlKkBkDA0MFCK1cFhvREu641wWW+Ey3ojiPXGQKooNQVImoBstwRaHgsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Column1-IgnoreHeader" = _t, #"Column2-IgnoreHeader" = _t, #"Column3-IgnoreHeader" = _t, #"Column4-IgnoreHeader" = _t, ID = _t, Color = _t, Weight = _t, Length = _t]),
    ProductInfoCols = {"ID", "Color", "Weight", "Length"},
    Ad_ProductInformation = Table.AddColumn(Source, "Product Information", each Text.Combine(List.Transform(List.Intersect({Record.FieldNames(_), ProductInfoCols}), (x)=> Text.Combine({x, Record.Field(_, x)}, ": ")), ", "), type text)
in
    Ad_ProductInformation

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Wow, @AlienSx, thanks a lot for your quick help! Works almost perfectly. I only had to modify one line:

b = List.Select(a, (w) => w{1} <> null and w{1} <> ""),

because my empty cells do not contain null.

I am quite new to Power Query M and still have trouble to understand the syntax, but I want to learn. As far as I understand you do the following:

Create a paired list (zip) of the column names and the row
Create a reduced list without empty values
Write column names and values into a single field using ': ' as separator
Combine these fields using ', ' as separator
Create a new column "Productinformation" holding the combined fields

Right?

Thanks and best regards,

René

Write column names and values into a single field using ': ' as separator

@rshh we still have a list at this point. This line transforms this list from list of pairs to the list of single (text) values. Next step combines these values into a text string. Other than this you are right.

I modified my original code a little bit. Now I simply add new column with the same set of transformations (was Table.ToList >> transformations >> Table.FromList). 

Thanks for the clarification and the code update!

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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