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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

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
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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