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
CHealy
Frequent Visitor

Complex pivot of questions with ID's

I have on column that has some duplicate ID's that I am trying to pivot from long to wide 

ID'sQuestions 
Aq1
Aq2
Bq3
Bq4
Bq5
Cq6
Dq7
Dq8
Eq9

 taking this data set to make it look like this 

 

ID'sQ variant 1 Q variant 2Q variant 3
Aq1q2null
Bq3q4q5
Cq6nullnull
Dq7q8null
Eq9nullnull

 

I do not know how to do it but I was thinking of making a reference column corresponding to which column the question needed to be in then pivot with the reference column being the header 

ie 

IDQuestionreference
Aq11
Aq22
Bq31
Bq42
Bq53

 

any help or suggestions would be appriciated 

Thank you

1 ACCEPTED SOLUTION
AmiraBedh
Super User
Super User

let
    
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUSo0VIrVgTKNwEwnENMYwTRBME3BTGcQ0wzMdAExzRFMCzDTFcS0VIqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"ID's" = _t, #"Questions " = _t]),
    
   
    RenamedColumns = Table.RenameColumns(Source, {{"Questions ", "Questions"}}),
    
    SortedTable = Table.Sort(RenamedColumns, {{"ID's", Order.Ascending}, {"Questions", Order.Ascending}}),
    
    GroupedTable = Table.Group(SortedTable, {"ID's"}, {{"AllData", each Table.AddIndexColumn(_, "Reference", 1, 1, Int64.Type)}}),
    
    ExpandedTable = Table.ExpandTableColumn(GroupedTable, "AllData", {"Questions", "Reference"}),
    
    ConvertedReference = Table.TransformColumnTypes(ExpandedTable, {{"Reference", type text}}),
    
    PivotedTable = Table.Pivot(ConvertedReference, List.Distinct(ConvertedReference[Reference]), "Reference", "Questions"),
    
    RenamedPivotedColumns = Table.RenameColumns(PivotedTable, {{"1", "Q variant 1"}, {"2", "Q variant 2"}, {"3", "Q variant 3"}})
    
in
    RenamedPivotedColumns

 

AmiraBedh_0-1736981972566.png

 


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

View solution in original post

1 REPLY 1
AmiraBedh
Super User
Super User

let
    
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUSo0VIrVgTKNwEwnENMYwTRBME3BTGcQ0wzMdAExzRFMCzDTFcS0VIqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"ID's" = _t, #"Questions " = _t]),
    
   
    RenamedColumns = Table.RenameColumns(Source, {{"Questions ", "Questions"}}),
    
    SortedTable = Table.Sort(RenamedColumns, {{"ID's", Order.Ascending}, {"Questions", Order.Ascending}}),
    
    GroupedTable = Table.Group(SortedTable, {"ID's"}, {{"AllData", each Table.AddIndexColumn(_, "Reference", 1, 1, Int64.Type)}}),
    
    ExpandedTable = Table.ExpandTableColumn(GroupedTable, "AllData", {"Questions", "Reference"}),
    
    ConvertedReference = Table.TransformColumnTypes(ExpandedTable, {{"Reference", type text}}),
    
    PivotedTable = Table.Pivot(ConvertedReference, List.Distinct(ConvertedReference[Reference]), "Reference", "Questions"),
    
    RenamedPivotedColumns = Table.RenameColumns(PivotedTable, {{"1", "Q variant 1"}, {"2", "Q variant 2"}, {"3", "Q variant 3"}})
    
in
    RenamedPivotedColumns

 

AmiraBedh_0-1736981972566.png

 


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

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!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

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.