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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Unpivot columns based on other table that define the group

I have two tables where one has the base data that needs to unpivot the columns (close to 1000 different columns) into different set of grouping. The other table that i have identify the groups that I'd like to unpivot into. Is there a way we can unpivot the columns based on the definition defined in different table? See below for a simplified example:

 

Table 1:

Record IDQuestion 1Question 1-commentNameQuestion 2Question 2-CommentQuestion 3Question 3-comment
Ayes Eriknohello worldno 
Byestest commentAdamno nocomment 1
Cyes Adamyes nocomment2
Dno Erikyes yes 

 

Table 2:

ColumnsType
Question 1yes/no
Question 1-commentcomment
Namename
Question 2yes/no
Question 2-Commentcomment
Question 3yes/no
Question 3-commentcomment

 

Desire Result after unpivot:

Record IDAttribueValueNameAttribute 1Value
AQuestion 1yesErikQuestion 1-comment 
AQuestion 2noErikQuestion 2-commenthello world
AQuestion 3noErikQuestion 3-comment 
BQuestion 1yesAdamQuestion 1-commenttest comment
BQuestion 2noAdamQuestion 2-comment 
BQuestion 3noAdamQuestion 3-commentcomment 1
CQuestion 1yesAdamQuestion 1-comment 
CQuestion 2yesAdamQuestion 2-comment 
CQuestion 3noAdamQuestion 3-commentcomment2
DQuestion 1noErikQuestion 1-comment 
DQuestion 2yesErikQuestion 2-comment 
DQuestion 3noErikQuestion 3-comment 
1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Record ID", type text}, {"Question 1", type text}, {"Question 1-comment", type text}, {"Name", type text}, {"Question 2", type text}, {"Question 2-Comment", type text}, {"Question 3", type text}, {"Question 3-comment", type text}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Record ID", "Name"}, "Attribute", "Value"),
    #"Sorted Rows" = Table.Sort(#"Unpivoted Other Columns",{{"Record ID", Order.Ascending}, {"Name", Order.Ascending}, {"Attribute", Order.Ascending}}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Sorted Rows", "Attribute", "Attribute - Copy"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Duplicated Column", "Attribute - Copy", Splitter.SplitTextByEachDelimiter({"-"}, QuoteStyle.Csv, false), {"Attribute - Copy.1", "Attribute - Copy.2"}),
    #"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter",{"Attribute - Copy.2"}),
    #"Added Index" = Table.AddIndexColumn(#"Removed Columns", "Index", 0, 1),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each #"Added Index" [#"Attribute - Copy.1"] {[Index] + 1}),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Value.1", each if [#"Attribute - Copy.1"]=[Custom] then #"Added Index" [Value] {[Index] + 1} else null),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom1", each not Text.Contains([Attribute], "-")),
    #"Removed Columns1" = Table.RemoveColumns(#"Filtered Rows",{"Attribute - Copy.1", "Index", "Custom"}),
    #"Added Custom2" = Table.AddColumn(#"Removed Columns1", "Attribute 1", each [Attribute]&"-Comment"),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Custom2",{"Record ID", "Attribute", "Value", "Name", "Attribute 1", "Value.1"})
in
    #"Reordered Columns"

Hope this helps.

Untitled.png


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

View solution in original post

3 REPLIES 3
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Record ID", type text}, {"Question 1", type text}, {"Question 1-comment", type text}, {"Name", type text}, {"Question 2", type text}, {"Question 2-Comment", type text}, {"Question 3", type text}, {"Question 3-comment", type text}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Record ID", "Name"}, "Attribute", "Value"),
    #"Sorted Rows" = Table.Sort(#"Unpivoted Other Columns",{{"Record ID", Order.Ascending}, {"Name", Order.Ascending}, {"Attribute", Order.Ascending}}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Sorted Rows", "Attribute", "Attribute - Copy"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Duplicated Column", "Attribute - Copy", Splitter.SplitTextByEachDelimiter({"-"}, QuoteStyle.Csv, false), {"Attribute - Copy.1", "Attribute - Copy.2"}),
    #"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter",{"Attribute - Copy.2"}),
    #"Added Index" = Table.AddIndexColumn(#"Removed Columns", "Index", 0, 1),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each #"Added Index" [#"Attribute - Copy.1"] {[Index] + 1}),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Value.1", each if [#"Attribute - Copy.1"]=[Custom] then #"Added Index" [Value] {[Index] + 1} else null),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom1", each not Text.Contains([Attribute], "-")),
    #"Removed Columns1" = Table.RemoveColumns(#"Filtered Rows",{"Attribute - Copy.1", "Index", "Custom"}),
    #"Added Custom2" = Table.AddColumn(#"Removed Columns1", "Attribute 1", each [Attribute]&"-Comment"),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Custom2",{"Record ID", "Attribute", "Value", "Name", "Attribute 1", "Value.1"})
in
    #"Reordered Columns"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-alq-msft
Community Support
Community Support

Hi, @Anonymous 

 

You may click 'Edit Query', go to Query Editor, go to 'Transform' ribbon, click 'Merge Columns', put the question and its corresponding comment together.

e1.png

 

e2.png

 

After three 'Merge Columns' steps, here is the result.

e3.png

 

Then you may make three merged column selected and unpivot them.

e4.png

e6.png

 

Finally you need to do a 'Split Column' to Attribute and Value columns.

e8.png

 

e9.png

 

Result:

e10.png

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Thanks for the reply. This works only if the number of columns are not that much and it's managable; however, my main database table has close to 1000 columns that needs to be merge like this case. Furthermore, additional columns may be added in the future, it's hard to merge it manually one by one.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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