Forum Discussion
Unpivot columns based on header name
Hey all, I've found some variations of this question but somehow can't seem to apply the answers to my specific case.
Basically I'm working with a survey response data set where I have two types of data that I'm trying to split out conditionally. Very simply put I have a "rating" question followed by a "comment" question. So for example:
Q1: Safety rating
Q1C: Safety comments
So far I've manually inpivoted rating questions separately and comment questions separately. However, this is done on specific columns. I'd like to instead have a conditional unpivot. Semantically it would look like:
if [Any value in list of column headers] contains "comments" then unpivot
That way I'd future proof it when adding new sources that may contain new questions.
Hopefully this makes sense and it's possible!
THanks
Use the below code to make it dynamic.
See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfLKz8iDUsX5IBZI0Ce/qDRXIbOguDSXKJFYnWglI7ApqShmmWLoJCwCMssYKBaSmYtilgmGTsIiILNMsHmSXMNMqehJMyyeNMLQSVgkNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Response ID" = _t, Name = _t, Project = _t, #"Quality rating" = _t, #"Quality comments" = _t, #"Safety rating" = _t, #"Safety comments" = _t, #"Communication rating" = _t, #"Communication comments" = _t]), #"Unpivoted Columns" = Table.Unpivot(Source, List.Select(Table.ColumnNames(Source), each Text.Contains(_,"Rating",Comparer.OrdinalIgnoreCase)), "Attribute", "Value") in #"Unpivoted Columns"
9 Replies
- Vijay_A_VermaMost Valuable Professional
Please post some sample date
How to get your questions answered quickly -- How to provide sample data
- emdnzHelper I
Thank you Vijay. Below is an example of what my starting data would look like. Underneath that is the result I'm trying to achieve. So far I've only been able to achieve this my manually selecting the columns I want to unpivot but I'm trying to future-proof it by making it conditional so that any columns that get added will automatically be unpivoted if they contain the word 'rating'.
Response ID Name Project Quality rating Quality comments Safety rating Safety comments Communication rating Communication comments 1 John Johnson 1 Lorum ipsum 1 Lorum ipsum 1 Lorum ipsum 2 Joe Johnson 5 Lorum ipsum 5 Lorum ipsum 5 Lorum ipsum 3 Tim Johnson 4 Lorum ipsum 4 Lorum ipsum 4 Lorum ipsum 4 John Johnson 4 Lorum ipsum 4 Lorum ipsum 4 Lorum ipsum 5 Joe Johnson 5 Lorum ipsum 5 Lorum ipsum 5 Lorum ipsum 6 Tim Johnson 2 Lorum ipsum 2 Lorum ipsum 2 Lorum ipsum Response ID Name Project Quality comments Safety comments Communication comments Attribute Value 1 John Johnson Lorum ipsum Lorum ipsum Lorum ipsum Quality rating 1 1 John Johnson Lorum ipsum Lorum ipsum Lorum ipsum Safety rating 1 1 John Johnson Lorum ipsum Lorum ipsum Lorum ipsum Communication rating 1 2 Joe Johnson Lorum ipsum Lorum ipsum Lorum ipsum Quality rating 5 2 Joe Johnson Lorum ipsum Lorum ipsum Lorum ipsum Safety rating 5 2 Joe Johnson Lorum ipsum Lorum ipsum Lorum ipsum Communication rating 5 3 Tim Johnson Lorum ipsum Lorum ipsum Lorum ipsum Quality rating 4 3 Tim Johnson Lorum ipsum Lorum ipsum Lorum ipsum Safety rating 4 3 Tim Johnson Lorum ipsum Lorum ipsum Lorum ipsum Communication rating 4 4 John Johnson Lorum ipsum Lorum ipsum Lorum ipsum Quality rating 4 4 John Johnson Lorum ipsum Lorum ipsum Lorum ipsum Safety rating 4 4 John Johnson Lorum ipsum Lorum ipsum Lorum ipsum Communication rating 4 5 Joe Johnson Lorum ipsum Lorum ipsum Lorum ipsum Quality rating 5 5 Joe Johnson Lorum ipsum Lorum ipsum Lorum ipsum Safety rating 5 5 Joe Johnson Lorum ipsum Lorum ipsum Lorum ipsum Communication rating 5 6 Tim Johnson Lorum ipsum Lorum ipsum Lorum ipsum Quality rating 2 6 Tim Johnson Lorum ipsum Lorum ipsum Lorum ipsum Safety rating 2 6 Tim Johnson Lorum ipsum Lorum ipsum Lorum ipsum Communication rating 2 - Vijay_A_VermaMost Valuable Professional
Use the below code to make it dynamic.
See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfLKz8iDUsX5IBZI0Ce/qDRXIbOguDSXKJFYnWglI7ApqShmmWLoJCwCMssYKBaSmYtilgmGTsIiILNMsHmSXMNMqehJMyyeNMLQSVgkNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Response ID" = _t, Name = _t, Project = _t, #"Quality rating" = _t, #"Quality comments" = _t, #"Safety rating" = _t, #"Safety comments" = _t, #"Communication rating" = _t, #"Communication comments" = _t]), #"Unpivoted Columns" = Table.Unpivot(Source, List.Select(Table.ColumnNames(Source), each Text.Contains(_,"Rating",Comparer.OrdinalIgnoreCase)), "Attribute", "Value") in #"Unpivoted Columns"