Pivoting/transposing problem

kcmuppet

Active Member
Joined
Nov 2, 2005
Messages
439
Office Version
  1. 365
Platform
  1. Windows
Hello

I'm struggling to get power query to transpose / pivot this (dummy survey data, 1 row per question per user):

UserIDCatergoryQuestion No.QuestionAnswerAnswer ScoreAnswer Band
1200General1Have you done X?Yes1None
1200General2Have you done Y?No3Low
1200General3Which do you prefer?This10High
1200Part 14Which do you prefer?That6Medium
1200Part 15What is this?Something4Medium
1200Part 26Which do you prefer?Else0None
1200Part 27Have you done X?Yes4Medium
AXLFGeneral1Have you done X?Yes1None
AXLFGeneral2Have you done Y?Yes2Low
AXLFGeneral3Which do you prefer?This10High
AXLFPart 14Which do you prefer?This8Medium
AXLFPart 15What is this?That4Medium
AXLFPart 26Which do you prefer?That3Low
AXLFPart 27Have you done X?Something3Low
OO567General1Have you done X?Yes1None
OO567General2Have you done Y?Else0None
OO567General3Which do you prefer?No8Medium
OO567Part 14Which do you prefer?No2Low
OO567Part 15What is this?Something4Medium
OO567Part 26Which do you prefer?That8Medium
OO567Part 27Have you done X?That6Medium


...into one row per user, like this:
UserIDQ. 1 answerQ. 2 answerQ. 3 answerQ. 4 answerQ. 5 answerQ. 6 answerQ. 7 answerQ. 1 ScoreQ. 2 ScoreQ. 3 ScoreQ. 4 ScoreQ. 5 ScoreQ. 6 ScoreQ. 7 ScoreQ. 1 RatingQ. 2 RatingQ. 3 RatingQ. 4 RatingQ. 5 RatingQ. 6 RatingQ. 7 Rating
1200YesNoThisThatSomethingElseYes13106404NoneLowHighMediumMediumNoneMedium
AXLFYesYesThisThisThatThatSomething12108433NoneLowHighMediumMediumLowLow
OO567YesElseNoNoSomethingThatThat1082486NoneNoneMediumLowMediumMediumMedium


Can anybody help?


In case it helps, here is the source dummy data in M:
Power Query:
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZQ7b8IwEID/ipWZIRgIbKgDJQNtKlEJEGKw4Eos5VHlAeLf9+y6VcAXN83g2LL13dn32dnvvSH3fW/gLSGDQiQ4GmILxQXYLa/ZKc+Abec4tYPSLL7inHcYECi30N1cA/gZYVvlVxpUi5tYHmOkNPxZwAcUCn6PpU6sgFCe42aAN1FUTG1p7OJFhV2A7QVOsk7JABMdQFRMlqzCjIpc5yngODubBC04N9Fb8i+SErDzicL94lNXyR9SP21Xzz19WWiLr2+UN4RZ5H+FmQDdhGl+Rh7bKcyopgvWxZUJMLJP/peq5l1p4lE0CaY9ddlsiy/qitmwQ5l+oA8F/wnQRZnmOXHsvu/rju8mzbV9hzji93D4Ag==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [UserID = _t, Catergory = _t, #"Question No." = _t, Question = _t, Answer = _t, #"Answer Score" = _t, #"Answer Band" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"UserID", type text}, {"Catergory", type text}, {"Question No.", Int64.Type}, {"Question", type text}, {"Answer", type text}, {"Answer Score", Int64.Type}, {"Answer Band", type text}})
in
    #"Changed Type"
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Note I started from a table in Excel, not a Json script...
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="survey"]}[Content],
    #"Removed Columns" = Table.RemoveColumns(Source,{"Catergory", "Question"}),
    #"Unpivoted Other Columns1" = Table.UnpivotOtherColumns(#"Removed Columns", {"UserID", "Question No."}, "Attribute", "Value"),
    #"Added Prefix" = Table.TransformColumns(#"Unpivoted Other Columns1", {{"Question No.", each "Q" & Text.From(_), type text}}),
    #"Sorted Rows" = Table.Sort(#"Added Prefix",{{"Attribute", Order.Ascending}, {"Question No.", Order.Ascending}}),
    #"Merged Columns" = Table.CombineColumns(#"Sorted Rows",{"Question No.", "Attribute"},Combiner.CombineTextByDelimiter(". ", QuoteStyle.None),"Attribute.1"),
    #"Pivoted Column" = Table.Pivot(#"Merged Columns", List.Distinct(#"Merged Columns"[Attribute.1]), "Attribute.1", "Value")
in
    #"Pivoted Column"
 
Upvote 0
Many thanks @GraH ! That seems to be fewer and simpler steps than in the approaches where I cross-posted here.

Can you tell which columns you selected at this step please:
Power Query:
  #"Unpivoted Other Columns1" = Table.UnpivotOtherColumns(#"Removed Columns", {"UserID", "Question No."}, "Attribute", "Value"),
and here:
Power Query:
    #"Pivoted Column" = Table.Pivot(#"Merged Columns", List.Distinct(#"Merged Columns"[Attribute.1]), "Attribute.1", "Value")
 
Upvote 0

Forum statistics

Threads
1,223,698
Messages
6,173,897
Members
452,536
Latest member
Chiz511

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top