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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
SJG
New Member

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
1 ACCEPTED SOLUTION
sevenhills
Super User
Super User

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:

sevenhills_0-1685126198976.png

 

 

Output:

sevenhills_1-1685126213234.png

 

View solution in original post

1 REPLY 1
sevenhills
Super User
Super User

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:

sevenhills_0-1685126198976.png

 

 

Output:

sevenhills_1-1685126213234.png

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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