Forum Discussion
Unpivot Multiple Columns with different formats
- 5 years ago
Hi,
v-xuding-msft CNENFRNL Jimmy801 AlB
Thanks a lot for your aissists,
I have solved the issue in my "artificial" way:
I have imported the same table four times and edited them according the need:
Fist table - I have left there the basic columns and then first columns for 1st question and 1st score.
then I have did the same with the second table leaving there basics and columns for 2nd questions ans scores and so on..
Then I have just UNION all four tables :)))))
That's all :))
Thanks a lot for the answer. CNENFRNL
To be honest I've been lost in the Code you've provided 🙂
Can you please provide the steps in Power Quuery wise how to get the desired result. But I think the "Unpivot other columns" does not work.
Here is the orginal table (at least the original columns):
| Num | Process | Date | Survey_Code | Question_1 | Score_1 | Question_2 | Score_2 | Question_3 | Score_3 | კითხვა4 | Question_4 | Identificator | Comment |
| 148 | Process_1 | 01-Jul-19 | N148.1.7.19/PD-03 | Question_1.1 | Score_1.1 | Qestion_1.2 | Score_1.2 | Qestion_1.3 | Score_1.3 | Qestion_1.4 | Score_1.4 | 2960 | Commnet_1 |
| 149 | Process_2 | 01-Jul-19 | N149.1.7.19/PD-03 | Question_2.1 | Score_2.1 | Qestion_2.2 | Score_2.2 | Qestion_2.3 | Score_2.3 | Qestion_2.4 | Score_2.4 | 2838 | Commnet_2 |
| 150 | Process_1 | 10-Jul-19 | N150.1.7.19/PD-03 | Question_3.1 | Score_3.1 | Question_3.2 | Score_3.2 | Question_3.3 | Score_3.3 | Question_3.4 | Score_3.4 | 2477 | Comment_3 |
Which I want to be tranferred to the following one:
| Num | Process | Date | Survey_Code | Identificator | Comment | Question | Score |
| 148 | Process_1 | 01-Jul-19 | N148.1.7.19/PD-03 | 2960 | Commnet_1 | Question_1.1 | Score_1.1 |
| 148 | Process_1 | 01-Jul-19 | N148.1.7.19/PD-03 | 2960 | Commnet_1 | Qestion_1.2 | Score_1.2 |
| 148 | Process_1 | 01-Jul-19 | N148.1.7.19/PD-03 | 2960 | Commnet_1 | Qestion_1.3 | Score_1.3 |
| 148 | Process_1 | 01-Jul-19 | N148.1.7.19/PD-03 | 2960 | Commnet_1 | Qestion_1.4 | Score_1.4 |
| 149 | Process_2 | 01-Jul-19 | N149.1.7.19/PD-03 | 2838 | Commnet_2 | Question_2.1 | Score_2.1 |
| 149 | Process_2 | 01-Jul-19 | N149.1.7.19/PD-03 | 2838 | Commnet_2 | Qestion_2.2 | Score_2.2 |
| 149 | Process_2 | 01-Jul-19 | N149.1.7.19/PD-03 | 2838 | Commnet_2 | Qestion_2.3 | Score_2.3 |
| 149 | Process_2 | 01-Jul-19 | N149.1.7.19/PD-03 | 2838 | Commnet_2 | Qestion_2.4 | Score_2.4 |
| 150 | Process_1 | 10-Jul-19 | N150.1.7.19/PD-03 | 2477 | Comment_3 | Question_3.1 | Score_3.1 |
| 150 | Process_1 | 10-Jul-19 | N150.1.7.19/PD-03 | 2477 | Comment_3 | Question_3.2 | Score_3.2 |
| 150 | Process_1 | 10-Jul-19 | N150.1.7.19/PD-03 | 2477 | Comment_3 | Question_3.3 | Score_3.3 |
| 150 | Process_1 | 10-Jul-19 | N150.1.7.19/PD-03 | 2477 | Comment_3 | Question_3.4 | Score_3.4 |
Thanks in advance,
Hello George1973
here another approach. But FYI... this kind of transformation is almost not possible to do with the GUI alone. So you have to dive into M-language. You can replace the first two steps with your data source
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddC9CoMwGIXhe8msab4vsZq5nToUpaOIg2QoVAP+3H9jLeXE4uTBN4SH1LUgU4hElKPv3DS1FLai9La8UrJh30OWJHNJ9lReU6XDv2px0/z0Q0tyPf7o/Oi+u/oVhsJR0VB0VAyUdbM9q/C5+L4f3BxsTbJ6LXj532sPvQxejrwMXo68DF6OvAzebXOhC/Dy5s3U7n1JoTdTh14N3m1DYkgcJw1pd6GB9CGbPP+S3TC3WjTNGw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Num = _t, Process = _t, Date = _t, Survey_Code = _t, Question_1 = _t, Score_1 = _t, Question_2 = _t, Score_2 = _t, Question_3 = _t, Score_3 = _t, კითხვა4 = _t, Question_4 = _t, Identificator = _t, Comment = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Num", Int64.Type}, {"Process", type text}, {"Date", type date}, {"Survey_Code", type text}, {"Question_1", type text}, {"Score_1", type text}, {"Question_2", type text}, {"Score_2", type text}, {"Question_3", type text}, {"Score_3", type text}, {"კითხვა4", type text}, {"Question_4", type text}, {"Identificator", Int64.Type}, {"Comment", type text}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Question_4", "Score_4"}, {"კითხვა4", "Question_4"}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Renamed Columns", {"Num", "Process", "Date", "Survey_Code", "Identificator", "Comment"}, "Attribute", "Value"),
TransformAttribute = Table.TransformColumns
(
#"Unpivoted Other Columns",
{
{
"Attribute",
each if Text.StartsWith(_, "Score") then "Score" else "Question"
}
}
),
#"Pivoted Column" = Table.Pivot(TransformAttribute, List.Distinct(TransformAttribute[Attribute]), "Attribute", "Value", each _),
Add = Table.AddColumn
(
#"Pivoted Column",
"Table",
each Table.FromColumns({[Question], [Score]}, {"Question", "Score"})
),
#"Removed Columns" = Table.RemoveColumns(Add,{"Question", "Score"}),
#"Expanded Table" = Table.ExpandTableColumn(#"Removed Columns", "Table", {"Question", "Score"}, {"Question", "Score"})
in
#"Expanded Table"
Copy paste this code to the advanced editor in a new blank query to see how the solution works.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy