Power Query + stack columns vertically

Anonymous
2020-06-06T17:20:44+00:00

I have

a c e
b d f

and I want

a
b
c
d
e
f

There could be more columns (and more rows) than showed above.

If possible I wish a Power Query solution.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes
Answer accepted by question author
  1. Anonymous
    2020-06-07T11:28:05+00:00

    Hans, don't forget to mark Lz's new reply as answer (and take the "answer" off of the earlier reply.

    .

    Excel allows you get the same result using many different paths.

    .

    Using Lz's reply as an inspiration (which I've done several times before), I came up with an alternative approach.

    .

    Load the source table into PowerQuery

    Select all of the columns

    Transform tab > Table group > Transpose command

    Select all of the columns

    Transform tab > Any Column group > Unpivot Columns drop down > Unpivot Columns command

    Remove the "Attribute" column

    .

    .

    Here is the resulting M Code:

    .

    let

        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],

        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}}),

        #"Transposed Table" = Table.Transpose(#"Changed Type"),

        #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Transposed Table", {}, "Attribute", "Value"),

        #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns",{"Attribute"})

    in

        #"Removed Columns"

    .

    .

    I actually started by using the Transpose() function in Excel, then unpivotting that result, which lead me to try the final solution I described above.

    .

    Lz's answer keeps the blank cell, mine doesn't

    If this order is important to you, then you should have some way of allowing Excel to sort to recreate it if you  use some other column to sort the data.  In this example I used PowerQuery to add an index column

    6 people found this answer helpful.
    0 comments No comments
Answer accepted by question author
  1. Ashish Mathur 101K Reputation points Volunteer Moderator
    2020-06-06T23:09:07+00:00

    Hi,

    This M code works

    let

        Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],

        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Text1", type text}, {"Text2", type text}, {"Text3", type text}}),

        #"Transposed Table" = Table.Transpose(#"Changed Type"),

        #"Added Index" = Table.AddIndexColumn(#"Transposed Table", "Index", 1, 1),

        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"Index"}, "Attribute", "Value"),

        #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Index", "Attribute"})

    in

        #"Removed Columns"

    Hope this helps.

    2 people found this answer helpful.
    0 comments No comments

16 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Anonymous
    2020-06-06T18:18:04+00:00

    Thanks but what I wanted was:  (based on your table)

    a

    b

    Hello

    c

    d

    Hans

    e

    f

    0 comments No comments
  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more