Forum Discussion
Transforming and transposing column values to column headers
Hello,
I would love to transform data from this form,
| Question | Number | Text |
| Q1(text response only) | one point one | |
| Q2 | two point two | |
| Q3 | 3.3 | |
| Q4 | 4.4 |
|
| Q1 | one point two | |
| Q2 | two point three | |
| Q3 | 3.4 | |
| Q4 | 4.5 | |
to this form,
| Q1 | Q2 | Q3 | Q4 |
| one point one | two point two | 3.3 | 4.4 |
| one point two | two point three | 3.4 | 4.5 |
I have tried using the transform function but it spits out this output,
| Q1 | Q2 | Q3 | Q4 | Q1 | Q2 | Q3 | Q4 |
| null | null | 3.3 | 4.4 | null | null | 3.4 | 4.5 |
| one point one | two point two | null | null | one point two | two point three | null | null |
The challenge is this data is coming from an automatic connection from a survey tool straight to powerbi in the form shown in the first table above. It spits text responses into a different column for example which makes it difficult to transform and get the desired second table above. It would be a different story if I was working with excel at some point but I would love to maintain the automatic flow without jumping out to excel.
Any help would be appreciated. This data is a small sample to show the nature of the data I am dealing with.
Hi Manara
Try this:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCjTUKEmtKFEoSi0uyM8rTlXIz8up1FTSUVIA4vy8VIWC/My8EqBoqlKsDlC5EVSqpDwfKgVkQaSMgcLGesZgBWABEyDTRA9EgvkgGspUQGIHGmLYBjcSi20ZRampyPaZoNtnChGIBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Question = _t, Number = _t, Text = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Question", type text}, {"Number", type number}, {"Text", type text}}), #"Extracted First Characters" = Table.TransformColumns(#"Changed Type", {{"Question", each Text.Start(_, 2), type text}}), #"Inserted Merged Column" = Table.AddColumn(#"Extracted First Characters", "Merged", each Text.Combine({Text.From([Number], "en-PH"), [Text]}, ""), type text), #"Removed Columns" = Table.RemoveColumns(#"Inserted Merged Column",{"Number", "Text"}), #"Filtered Rows" = Table.SelectRows(#"Removed Columns", each [Merged] <> null and [Merged] <> ""), Pivoted = Table.Pivot( #"Filtered Rows", List.Distinct(#"Filtered Rows"[Question]), "Question", "Merged", List.Distinct ), #"Added Custom1" = Table.AddColumn(Pivoted, "Table", each Table.FromColumns(Record.FieldValues(_), Record.FieldNames(_))), Table1 = #"Added Custom1"{0}[Table] in Table1
2 Replies
- danextian
Super User
Hi Manara
Try this:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCjTUKEmtKFEoSi0uyM8rTlXIz8up1FTSUVIA4vy8VIWC/My8EqBoqlKsDlC5EVSqpDwfKgVkQaSMgcLGesZgBWABEyDTRA9EgvkgGspUQGIHGmLYBjcSi20ZRampyPaZoNtnChGIBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Question = _t, Number = _t, Text = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Question", type text}, {"Number", type number}, {"Text", type text}}), #"Extracted First Characters" = Table.TransformColumns(#"Changed Type", {{"Question", each Text.Start(_, 2), type text}}), #"Inserted Merged Column" = Table.AddColumn(#"Extracted First Characters", "Merged", each Text.Combine({Text.From([Number], "en-PH"), [Text]}, ""), type text), #"Removed Columns" = Table.RemoveColumns(#"Inserted Merged Column",{"Number", "Text"}), #"Filtered Rows" = Table.SelectRows(#"Removed Columns", each [Merged] <> null and [Merged] <> ""), Pivoted = Table.Pivot( #"Filtered Rows", List.Distinct(#"Filtered Rows"[Question]), "Question", "Merged", List.Distinct ), #"Added Custom1" = Table.AddColumn(Pivoted, "Table", each Table.FromColumns(Record.FieldValues(_), Record.FieldNames(_))), Table1 = #"Added Custom1"{0}[Table] in Table1 - amitchandak
Super User
Manara , Add a sub Category Index/Rank column in power query. Post that you an use unpivot
Or you can use Matrix visual with Question on Columns and sub Category index on row
Power BI and Power Query- Sub Category Rank and Index: https://youtu.be/tMBviW4-s4A
Power Query UnPivot Data, Pivot Data, Transpose Data, Query Fill Up, Fill Down: https://www.youtube.com/watch?v=cN8AO3_vmlY&t=36407s