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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
Anonymous
Not applicable

Transforming wide table to long table in Power Query

IDNameRegionDo you love EconomicsEconomics commentsDo you love MathematicsMath CommentsDo you love ScienceScience commentsDo you love EnglishEnglish comments
1JohnTXYesValuable contentNOIt is hardNoNot enough motivationYesEasy
2JaydenMNNoTeacher is boringYesEasyYesI need it to become an engineerYesSpeak it daily
           

Hello,

I have to report on a survey data which comes in the wide form as shown below: (Wehave the raw data in the table above)

MummyG_0-1701425048442.png

 

I want to transform the data to the long form as shown in the picture below (that is, keep all the initial 3 columns as they are but transform the respective questions, answers and comments into 3 other columns). The data for the result is below.

MummyG_1-1701425108189.png

IDNameRegionQuestionsResponseComments
1JohnTXDo you love EconomicsYesValuable content
1JohnTXDo you love MathematicsNoIt is hard
1JohnTXDo you love ScienceNoNot enough motivation
1JohnTXDo you love EnglishYesEasy
2JaydenMNDo you love EconomicsNoTeacher is boring
2JaydenMNDo you love MathematicsYesEasy
2JaydenMNDo you love ScienceYesI need it to become an engineer
2JaydenMNDo you love EnglishYesSpeak it daily

Please, can someone share an insight on how to about this?

Thanks

 

 

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"ID", "Name", "Region"}, "Subject", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "Custom", each if Text.StartsWith([Subject], "Do you",Comparer.OrdinalIgnoreCase) then "Question" else "Comments"),
    #"Replaced Value" = Table.ReplaceValue(#"Added Custom"," comments","",Replacer.ReplaceText,{"Subject"}),
    #"Replaced Value2" = Table.ReplaceValue(#"Replaced Value"," Comments","",Replacer.ReplaceText,{"Subject"}),
    #"Replaced Value3" = Table.ReplaceValue(#"Replaced Value2","Mathematics","Math",Replacer.ReplaceText,{"Subject"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value3","Do you love ","",Replacer.ReplaceText,{"Subject"}),
    #"Pivoted Column" = Table.Pivot(#"Replaced Value1", List.Distinct(#"Replaced Value1"[Custom]), "Custom", "Value")
in
    #"Pivoted Column"

Hope this helps.

Ashish_Mathur_0-1701579215876.png

 


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

View solution in original post

11 REPLIES 11
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"ID", "Name", "Region"}, "Subject", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "Custom", each if Text.StartsWith([Subject], "Do you",Comparer.OrdinalIgnoreCase) then "Question" else "Comments"),
    #"Replaced Value" = Table.ReplaceValue(#"Added Custom"," comments","",Replacer.ReplaceText,{"Subject"}),
    #"Replaced Value2" = Table.ReplaceValue(#"Replaced Value"," Comments","",Replacer.ReplaceText,{"Subject"}),
    #"Replaced Value3" = Table.ReplaceValue(#"Replaced Value2","Mathematics","Math",Replacer.ReplaceText,{"Subject"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value3","Do you love ","",Replacer.ReplaceText,{"Subject"}),
    #"Pivoted Column" = Table.Pivot(#"Replaced Value1", List.Distinct(#"Replaced Value1"[Custom]), "Custom", "Value")
in
    #"Pivoted Column"

Hope this helps.

Ashish_Mathur_0-1701579215876.png

 


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

This solution worked for this sample data. Thanks so much.

Unfortunately, it did not work for the project that I needed it for. I got the following error when I tried the pivoting step:

Expression Error: We cannot apply operator < to types List and List

Details: 

Operator=<

Left =[List]

Right=[List]

 

How do i resolve this?

You are welcome.  I cannot know the reason just by looking at that line of error.  Try to debug it yourself.


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

I worked on the code and wrote more optimized code

 

Ahmedx
Super User
Super User

pls try

 

Anonymous
Not applicable

The records for the Question column are not correct. Thanks

sorry? pls try again

 

Ahmedx
Super User
Super User

share an example, it’s impossible to copy from a picture

 

Anonymous
Not applicable

I have added the data to the post.

miTutorials
Super User
Super User

In Power Query Editor, Select the columns that you dont want to unpivot (Name, region etc). Right click and choose unpivot other columns.

 

There is a detailed tutorial on the Pivot and Unpivot feature do check it out.

 

Pivot/Unpivot PowerBI Feature explained | Power Query | MiTutorials - YouTube

Anonymous
Not applicable

That will not solve the problem because the unpivoting will result in only two columns for attribute and values but in my case, I need to end up with thre columns, Question, Response and comments

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 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.

Top Solution Authors