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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Anonymous
Not applicable

Survey data set transform to pivot column and merge

Hi Community,

 

I would like to turn my survey-like data into a pivoted result table.

My raw data looks like survey reponses. One survey could have one or multiple people answered. When there are multiple people under same survey ID, they are mapped by Repeat ID. Each people has same question set. 

 

Below table is the sample. Survey ID 001 has aaa, bbb, ccc total 3 people answered. Survey ID 002 has single reponse.

IDRepeat IDQuestionResponse
001aaaStateMA
001aaaCell12234
001aaaViolationY
001aaaModelcivic
001aaaMakehonda
001aaaGenderF
001aaaAge40
001bbbStateFL
001bbbCell12543
001bbbViolationN
001bbbModelvw
001bbbMakejetta
001bbbGenderM
001bbbAge29
001cccStateTX
001cccCell23445
001cccViolationY
001cccModelcooper
001cccMakemini
001cccGenderF
001cccAge23
002dddStateCA
002dddCell246456
002dddViolationN
002dddModelhonda
002dddMakeCRV
002dddGenderM
002dddAge34

 

 

What I would like my data set looks like:

IDRepeatIDStateCellViolationModelMakeGenderAge
001aaaMA12234YcivichondaF40
001bbbFL12543NvwjettaM29
001cccTX23445YcooperminiF23
002dddCA246456NhondaCRVM34

 

And I tried to use "Piovt Column" in Power Query, but the result looks like:

 

IDRepeatIDStateCellViolationModelMakeGenderAge
001aaaMAnullnullnullnullnullnull
001aaanull12234nullnullnullnullnull
001aaanullnullYnullnullnullnull
001aaanullnullnullcivicnullnullnull
001aaanullnullnullnullhondanullnull
001aaanullnullnullnullnullFnull
001aaanullnullnullnullnullnull40
001bbbFLnullnullnullnullnullnull
001bbbnull12543nullnullnullnullnull
001bbbnullnullYnullnullnullnull

 

 

 

Is there any way I can make my data set looks like as my expectation? 

(I can only use Power BI to build data set now. SQL is not available currently) 

 

Thank you!

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    Partition = Table.Group(Source, {"Question"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
    #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"ID", "Repeat ID", "Response", "Index"}, {"ID", "Repeat ID", "Response", "Index"}),
    #"Sorted Rows" = Table.Sort(#"Expanded Partition",{{"ID", Order.Ascending}, {"Repeat ID", Order.Ascending}}),
    #"Pivoted Column" = Table.Pivot(#"Sorted Rows", List.Distinct(#"Sorted Rows"[Question]), "Question", "Response"),
    #"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
in
    #"Removed Columns"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

4 REPLIES 4
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    Partition = Table.Group(Source, {"Question"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
    #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"ID", "Repeat ID", "Response", "Index"}, {"ID", "Repeat ID", "Response", "Index"}),
    #"Sorted Rows" = Table.Sort(#"Expanded Partition",{{"ID", Order.Ascending}, {"Repeat ID", Order.Ascending}}),
    #"Pivoted Column" = Table.Pivot(#"Sorted Rows", List.Distinct(#"Sorted Rows"[Question]), "Question", "Response"),
    #"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
in
    #"Removed Columns"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
PhilipTreacy
Super User
Super User

Hi @Anonymous 

Download this sample PBIX with this transformation/code

Select the Question and Response columns then from the menu Transform tab -> Pivot Column and use these settings

piv-col.png

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddJBC4IwHIfh77KzB7MpdBTBLtmhQoroMLdRK3MRw75+G0td/OriJg9DX/8ejySOZyQijDF73RpmpF2rnJyibypk29plliRzClgr3TKjdGf3B9BKC+nOctUrjspu7pEX3QkGuJSdkE+7KYHysztG4wCapgkiyhXQGJHSOWAYsQYdIvoXki+4SmMY4FhQAfmCZBEA5zwo2O2BPgV2CDQF/DcGr+MYtH7YNwL2FXfVKbCfY/D0iRg+Z2JvhBBBRJEDDRE0o2kG+msOkw4V4f8SqI8oNjUQDGIi3+D+69Mb", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"Repeat ID" = _t, Question = _t, Response = _t]),
    #"Pivoted Column" = Table.Pivot(Source, List.Distinct(Source[Question]), "Question", "Response")
in
    #"Pivoted Column"

 

 

Giving this

piv-res.png

 

Regards

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Anonymous
Not applicable

Hi Phil,

 

Thanks for replying. I tried this and I was given:

IDRepeatIDStateCellViolationModelMakeGenderAge
001aaaMAnullnullnullnullnullnull
001aaanull12234nullnullnullnullnull
001aaanullnullYnullnullnullnull
001aaanullnullnullcivicnullnullnull
001aaanullnullnullnullhondanullnull
001aaanullnullnullnullnullFnull
001aaanullnullnullnullnullnull40
001bbbFLnullnullnullnullnullnull
001bbbnull12543nullnullnullnullnull
001bbbnullnullYnullnullnullnull

 

I would like to know how to remove all the null and merge into a single reponse. 

Hi @Anonymous 

I'm not sure what you are doing to get that result. The query in my sample file works correctly.  Did you download the sample PBIX file I supplied so you could see what I did to get the table in the way you wanted it?

Can you please supply your file with the query that results in all the null values - or at least supply the query copy/paste if the file contains sensitive data.  I can use the query against the sample data you've already supplied.

Regards

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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