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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
irfan_abdrhman
Helper II
Helper II

Combining Columns to create Main Columns

The table below is the data that i have which can't be changed until is transformed into power queryirfan_abdrhman_0-1714101613588.png

What I want it to become is the columns become Material, Type/Grade, Quantity Delivered and other columns like MRDO NO, MRDO DATE is populated. Inside Material has columns : Material 1, Material 2, Material 3,... Material 10, and same goes to Type/Grade and Quantity Delivered and i want them to match in a row where each row has the correct MRDO NO, MRDO DATE , MATERIAL (n), Type/Grade (n), and Quantity Delivered (n) where (n) is the same number per row. Below is the expected outcome

irfan_abdrhman_2-1714101920471.png

May I know if this is workable, and the possible workaround(s), Thank you.

 

1 ACCEPTED SOLUTION

Expected Outcome 1

Result

dufoq3_0-1714148463718.png

let
    Source = Excel.Workbook(File.Contents("C:\Downloads\PowerQueryForum\irfan_abdrhman\MRDO.xlsx"), null, true),
    #"Dashboard _Sheet" = Source{[Item="Dashboard ",Kind="Sheet"]}[Data],
    FilteredRows = Table.SelectRows(#"Dashboard _Sheet", each ([Column1] <> null)),
    PromotedHeaders = Table.PromoteHeaders(FilteredRows, [PromoteAllScalars=true]),
    Transformed = [ a = Table.ColumnNames(PromotedHeaders),
    b = List.Select(a, (x)=> Text.StartsWith(x, "Material")),
    c1 = {"MRDO NO", "MRDO DATE"},
    c2 = {"Requestor", "Delivery Location"},
    c3 = List.RemoveMatchingItems(a, c1 & c2),
    d = Table.SelectColumns(PromotedHeaders, c3),
    e = List.TransformMany(
            Table.ToRows(PromotedHeaders),
            each List.Split(List.RemoveLastN(List.Skip(_,2),2), List.Count(c3) / List.Count(b)),
            (x,y)=> List.FirstN(x, 2) & y & List.LastN(x, 2)),
    f = List.Transform({ 0..List.PositionOf(a, List.Skip(b){0}) -1 }, (x)=> a{x}),
    g = Table.FromRows(e, Value.Type(Table.SelectColumns(PromotedHeaders, f & c2))),
    h = Table.TransformColumnNames(g, (x)=> Text.Remove(x, {"0".."9"}))
  ][h],
    FilteredRows2 = Table.SelectRows(Transformed, each ([#"Material "] <> null)),
    ChangedType = Table.TransformColumns(FilteredRows2, {
        //{"MRDO NO", each Text.PadStart(Text.From(_), 3, "0"), type text},
        {"MRDO DATE", each Date.ToText(Date.From(_), [Format="d-MMM-yy", Culture="en-US"]), type text} })
in
    ChangedType

 

 


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

View solution in original post

6 REPLIES 6
dufoq3
Super User
Super User

Hi @irfan_abdrhman,

for future requests provide sample data in usable format (not as a screenshot). If you don't know how to do it - read note below my post.

 

If you don't know how to use my query, also read Note below my post.

 

Result:

dufoq3_0-1714117649660.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dZDBCsMgDIZfRTy3JUbt2LFQKUL14NpdpO//GovWUgsONIlJ/H5jjFzwjuuBiYEhoKLDOm0m2M2cIVkBQHYJ09esFCA4l5yGVrOm7ay3fmHz/kkZV7wEfnSRYybWgu121Co9be/nMFlP0QinsFbwyAu8CplWVpKSRQr/z4YS69neBSUE3qgHU+Vqzbxfct0eyye0xnqhbJGPHw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"MRDO NO" = _t, #"MRDO DATE" = _t, #"Material 1" = _t, #"Type/Grade 1" = _t, #"Quantity Delivered 1" = _t, #"Material 2" = _t, #"Type/Grade 2" = _t, #"Quantity Delivered 2" = _t, #"Material 3" = _t, #"Type/Grade 3" = _t, #"Quantity Delivered 3" = _t, #"Material 4" = _t, #"Type/Grade 4" = _t, #"Quantity Delivered 4" = _t]),
    Transformed = [ a = Table.ColumnNames(Source),
    b = List.Select(a, (x)=> Text.StartsWith(x, "Material")),
    c = List.FirstN(a, 2),
    d = Table.RemoveColumns(Source, c),
    e = List.TransformMany(
            Table.ToRows(Source),
            each List.Split(List.Skip(_, 2), List.Count(List.RemoveMatchingItems(a, c)) / List.Count(b)),
            (x,y)=> List.FirstN(x, 2) & y ),
    f = List.Transform({ 0..List.PositionOf(a, List.Skip(b){0}) -1 }, (x)=> a{x}),
    g = Table.FromRows(e, Value.Type(Table.SelectColumns(Source, f)))
  ][g],
    ChangedType = Table.TransformColumns(Transformed, {
        {"MRDO NO", each Text.PadStart(Text.From(_), 3, "0"), type text},
        {"MRDO DATE", each Date.ToText(Date.From(_), [Format="d-MMM-yy", Culture="en-US"]), type text} })
in
    ChangedType

 


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

Hi, sorry for not providing sample data. Click Here for the sample data, please see the other sheets for the expected outcome as well.

Expected Outcome 1

Result

dufoq3_0-1714148463718.png

let
    Source = Excel.Workbook(File.Contents("C:\Downloads\PowerQueryForum\irfan_abdrhman\MRDO.xlsx"), null, true),
    #"Dashboard _Sheet" = Source{[Item="Dashboard ",Kind="Sheet"]}[Data],
    FilteredRows = Table.SelectRows(#"Dashboard _Sheet", each ([Column1] <> null)),
    PromotedHeaders = Table.PromoteHeaders(FilteredRows, [PromoteAllScalars=true]),
    Transformed = [ a = Table.ColumnNames(PromotedHeaders),
    b = List.Select(a, (x)=> Text.StartsWith(x, "Material")),
    c1 = {"MRDO NO", "MRDO DATE"},
    c2 = {"Requestor", "Delivery Location"},
    c3 = List.RemoveMatchingItems(a, c1 & c2),
    d = Table.SelectColumns(PromotedHeaders, c3),
    e = List.TransformMany(
            Table.ToRows(PromotedHeaders),
            each List.Split(List.RemoveLastN(List.Skip(_,2),2), List.Count(c3) / List.Count(b)),
            (x,y)=> List.FirstN(x, 2) & y & List.LastN(x, 2)),
    f = List.Transform({ 0..List.PositionOf(a, List.Skip(b){0}) -1 }, (x)=> a{x}),
    g = Table.FromRows(e, Value.Type(Table.SelectColumns(PromotedHeaders, f & c2))),
    h = Table.TransformColumnNames(g, (x)=> Text.Remove(x, {"0".."9"}))
  ][h],
    FilteredRows2 = Table.SelectRows(Transformed, each ([#"Material "] <> null)),
    ChangedType = Table.TransformColumns(FilteredRows2, {
        //{"MRDO NO", each Text.PadStart(Text.From(_), 3, "0"), type text},
        {"MRDO DATE", each Date.ToText(Date.From(_), [Format="d-MMM-yy", Culture="en-US"]), type text} })
in
    ChangedType

 

 


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

AlienSx
Super User
Super User

i don't see Requestor and Delivery Location columns in original image so we go w/o these columns

    lst = Table.ToList(
        your_table, 
        (w) => List.TransformMany(
            {w},
            (x) => List.Split(List.Skip(List.RemoveNulls(x), 2), 3),
            (x, y) => List.FirstN(x, 2) & y
        )
    ),
    tbl = Table.FromList(
        List.Combine(lst), 
        each _, 
        {"MRDO NO", "MRDO DATE", "Material", "Type/Grade", "Quantity Delivered"}
    )

May I know where do I insert that in?

irfan_abdrhman_0-1714114941697.png

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.