Forum Discussion

SJG's avatar
SJG
New Member
3 years ago
Solved

Combining Multiple Columns to One

Hello,

 

I have survey data that comes in a pretty poor format and need assistance reformatting. When a question is asked multiple times in the survey, each response is placed in a new column. An example question is, "What Position are you At?" Each reponse from the person performing the survey creates a columns. I need a way to put all the columns of data into 1 long column. I will provide an example below. Is this possible?

 

PersonPositionPosition2Position3
A123
B123
C123

 

Needs turned into:

 

Position

1
2
3
1
2
3
1
2
3
  • Yes it is possible.

     

    Here is the M Query code

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIEYiMgNlaK1YlWcsIQcUYViQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Person = _t, Position = _t, Position2 = _t, Position3 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Person", type text}, {"Position", Int64.Type}, {"Position2", Int64.Type}, {"Position3", Int64.Type}}),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Person"}, "Attribute", "Value"),
        #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute", "Person"}),
        #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Value", "Position"}})
    in
        #"Renamed Columns"

     

    Your source:

     

     

    Output:

     

1 Reply

  • Yes it is possible.

     

    Here is the M Query code

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIEYiMgNlaK1YlWcsIQcUYViQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Person = _t, Position = _t, Position2 = _t, Position3 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Person", type text}, {"Position", Int64.Type}, {"Position2", Int64.Type}, {"Position3", Int64.Type}}),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Person"}, "Attribute", "Value"),
        #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute", "Person"}),
        #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Value", "Position"}})
    in
        #"Renamed Columns"

     

    Your source:

     

     

    Output: