Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

Append different columns from one table

Hello,

I have a table with four columns: A, B, C, D. 

<A>    <B>     <C>      <D>

John    Mike    Mark     Robyn

Maura Deb     Jim        Tim

 

I would like to create a new table with all the raws that include the relations between A and all of Columns B, C, D.

I would like to generate a new table with two columns including all the raws from the table with column A & B, append the same table with columns A & C and do the same with column A & D.

 

<A>      <New>

John      Mike

John      Mark

John      Robyn

Maura   Deb

Maura   Jim

Maura   Tim

 

I tried the funciton summarize and union but wasn't able to link the columns B, C, D to the new column.

 

Any chance you could help?

 

Thanks a lot !

  • HI, Anonymous 

    The best way is Unpivot the columns in Edit Queries.

    For example:

    In Edit Queries, Selected Column B,C,D.

    Then click Transform->Unpivot Columns

    Now remove Attribute column.

    Result:

     

    here is M colde, please try it.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyFPSUfLNzE4FUYlF2UAqKD+pMk8pVicaKFBalAgUcUlNApJemblAMgRIxsYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [A = _t, B = _t, C = _t, D = _t]),
        #"Unpivoted Columns" = Table.UnpivotOtherColumns(Source, {"A"}, "Attribute", "Value"),
        #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns",{"Attribute"})
    in
        #"Removed Columns"

    Best Regards,

    Lin

1 Reply

  • v-lili6-msft's avatar
    v-lili6-msft
    Community Support

    HI, Anonymous 

    The best way is Unpivot the columns in Edit Queries.

    For example:

    In Edit Queries, Selected Column B,C,D.

    Then click Transform->Unpivot Columns

    Now remove Attribute column.

    Result:

     

    here is M colde, please try it.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyFPSUfLNzE4FUYlF2UAqKD+pMk8pVicaKFBalAgUcUlNApJemblAMgRIxsYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [A = _t, B = _t, C = _t, D = _t]),
        #"Unpivoted Columns" = Table.UnpivotOtherColumns(Source, {"A"}, "Attribute", "Value"),
        #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns",{"Attribute"})
    in
        #"Removed Columns"

    Best Regards,

    Lin