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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
rodg
Frequent Visitor

split column into matching columns

Hi, I'm looking for a way to split 1 column into multiple columns, where the columns should match across all rows. Sounds very abstract but I hope my screenshots make it clear. 🙂

 

rodg_0-1724088258201.png

rodg_1-1724088283594.png

thanks in advance for all the tips

Ronny

 

 

 

 

 

1 ACCEPTED SOLUTION
ronrsnfld
Super User
Super User

You can develop your desired result by

  • Splitting the comment column by the comma into rows
  • Create a list of all the desired new columns (Age1..Age12)
  • Prefixing the numbers in the splitted comment column with the string "Age"
  • Pivoting the table, but using All the desired column names instead of just the ones that are appearing in the Comments column.

ronrsnfld_0-1724094559330.png

M Code (in Advanced Editor)

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8nQxVNJRykvMTQXRpjpmSrE6IFEjqCiINtMx17FQio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Name = _t, Comment = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Name", type text}, {"Comment", type text}}),
    #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Changed Type", {
        {"Comment", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), 
        let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Comment"),
    
    #"All Column Names" = List.Transform({1..ColumnsToAdd}, each "Age" & Text.From(_)),
    #"Prefix with 'Age'" = Table.TransformColumns(#"Split Column by Delimiter",{{"Comment", each "Age" & _, type text}}),
    #"Pivoted Column" = Table.Pivot(#"Prefix with 'Age'", #"All Column Names", "Comment", "ID", List.Count)
in
    #"Pivoted Column"

 Results

ronrsnfld_1-1724094645183.png

 

 

View solution in original post

4 REPLIES 4
dufoq3
Super User
Super User

Hi @rodg, different approach:

 

Output

dufoq3_0-1724137319082.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8nQxVNJRykvMTQXRpjpmSrE6IFEjqCiINtMx17FQio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Name = _t, comment = _t]),
    Ad_Age = Table.AddColumn(Source, "Age", each 
        [ a = List.Transform(Text.Split([comment], ","), Number.From),
          b = List.Accumulate(
                    { 1..12 },
                    Table.FromRecords({_}),
                    (s,c)=> Table.AddColumn(s, "Age" & Text.From(c), (x)=> if List.Contains(a, c) then 1 else 0, Int64.Type))
        ][b], type table),
    Combined = Table.Combine(Ad_Age[Age])
in
    Combined

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

ronrsnfld
Super User
Super User

You can develop your desired result by

  • Splitting the comment column by the comma into rows
  • Create a list of all the desired new columns (Age1..Age12)
  • Prefixing the numbers in the splitted comment column with the string "Age"
  • Pivoting the table, but using All the desired column names instead of just the ones that are appearing in the Comments column.

ronrsnfld_0-1724094559330.png

M Code (in Advanced Editor)

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8nQxVNJRykvMTQXRpjpmSrE6IFEjqCiINtMx17FQio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Name = _t, Comment = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Name", type text}, {"Comment", type text}}),
    #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Changed Type", {
        {"Comment", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), 
        let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Comment"),
    
    #"All Column Names" = List.Transform({1..ColumnsToAdd}, each "Age" & Text.From(_)),
    #"Prefix with 'Age'" = Table.TransformColumns(#"Split Column by Delimiter",{{"Comment", each "Age" & _, type text}}),
    #"Pivoted Column" = Table.Pivot(#"Prefix with 'Age'", #"All Column Names", "Comment", "ID", List.Count)
in
    #"Pivoted Column"

 Results

ronrsnfld_1-1724094645183.png

 

 

So the solution was to create an extra list ("All Column Names") & use the list.count as the last argument to the Table.Pivot function.

Thanks @ronrsnfld 

lbendlin
Super User
Super User

Normally you would only unpivot your data and then let the data model (or the disconnected table) do the work for you.  Power BI is not Excel, don't try to treat it that way.

 

lbendlin_0-1724091098004.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8nQxVNJRykvMTQXRpjpmSrE6IFEjqCiINtMx17FQio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Name = _t, Content = _t]),
    #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(Source, {{"Content", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Content"),
    #"Renamed Columns" = Table.RenameColumns(#"Split Column by Delimiter",{{"Content", "Age"}})
in
    #"Renamed Columns"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

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 community update carousel

Fabric Community Update - June 2025

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