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
rnehrboss
Helper II
Helper II

Diffculty solving - unpivot

Hi,  

I'm having trouble solving a data transform unpivot/pivot issue.

I have the following table:

IDDateQ1:How did you like it?Q1:Code 1Q1:Code 2Q2:Were they late?Q2:Code 1Q2:Code 2
11/1/2022I liked it12They didn't come46
22/2/2022I hated it23They were late68

 

I'd like to get to the following:

IDDateQuestionQuestion TextQ1:How did you like it?Code 1Code 2
11/1/2022Q1How did you like it?I liked it12
11/1/2022Q2Were they late?They didn't come46
22/2/2022Q1How did you like it?I hated it23
22/2/2022Q2Were they late?68

 

I know it's something like an unpivot / split / repivot, but having troubles gettin there.

Can anyone help?   

 

Thanks

 

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

1. Unpivot all except the ID and Date columns.

AlexisOlson_2-1666648233036.png

 

2. Split the Attribute column on the colon.

AlexisOlson_3-1666648255872.png

 

3. Separate into code rows and question rows.

AlexisOlson_4-1666648283488.png

AlexisOlson_5-1666648296495.png

 

4. Pivot the code rows.

AlexisOlson_6-1666648321301.png

 

5. Merge the code rows onto the question rows.

AlexisOlson_7-1666648350166.png

 

Fully sample query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTLUN9Q3MjAyAjI9FXIys1NTFDJLQOJADBIMyUitVEjJTMmLKTUwMDIvUUjOz00FipsAsZlSrE40WJWRvhHClIzEEpgpIAFjmCnlqUWpCjlASbBWHSULpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Date = _t, #"Q1:How did you like it?" = _t, #"Q1:Code 1" = _t, #"Q1:Code 2" = _t, #"Q2:Were they late?" = _t, #"Q2:Code 1" = _t, #"Q2:Code 2" = _t]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"ID", "Date"}, "Attribute", "Value"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByEachDelimiter({":"}, QuoteStyle.Csv, false), {"Question", "Text"}),
    #"Code Rows" = Table.SelectRows(#"Split Column by Delimiter", each Text.StartsWith([Text], "Code")),
    #"Pivoted Column" = Table.Pivot(#"Code Rows", List.Distinct(#"Code Rows"[Text]), "Text", "Value"),
    #"Question Rows" = Table.SelectRows(#"Split Column by Delimiter", each not Text.StartsWith([Text], "Code")),
    #"Renamed Columns" = Table.RenameColumns(#"Question Rows",{{"Text", "Question Text"}, {"Value", "Answer"}}),
    #"Merged Queries" = Table.NestedJoin(#"Renamed Columns", {"ID", "Date", "Question"}, #"Pivoted Column", {"ID", "Date", "Question"}, "Renamed Columns", JoinKind.LeftOuter),
    #"Expanded Renamed Columns" = Table.ExpandTableColumn(#"Merged Queries", "Renamed Columns", {"Code 1", "Code 2"}, {"Code 1", "Code 2"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Renamed Columns",{{"ID", Int64.Type}, {"Date", type date}, {"Question", type text}, {"Question Text", type text}, {"Answer", type text}, {"Code 1", Int64.Type}, {"Code 2", Int64.Type}})
in
    #"Changed Type"

View solution in original post

2 REPLIES 2
AlexisOlson
Super User
Super User

1. Unpivot all except the ID and Date columns.

AlexisOlson_2-1666648233036.png

 

2. Split the Attribute column on the colon.

AlexisOlson_3-1666648255872.png

 

3. Separate into code rows and question rows.

AlexisOlson_4-1666648283488.png

AlexisOlson_5-1666648296495.png

 

4. Pivot the code rows.

AlexisOlson_6-1666648321301.png

 

5. Merge the code rows onto the question rows.

AlexisOlson_7-1666648350166.png

 

Fully sample query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTLUN9Q3MjAyAjI9FXIys1NTFDJLQOJADBIMyUitVEjJTMmLKTUwMDIvUUjOz00FipsAsZlSrE40WJWRvhHClIzEEpgpIAFjmCnlqUWpCjlASbBWHSULpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Date = _t, #"Q1:How did you like it?" = _t, #"Q1:Code 1" = _t, #"Q1:Code 2" = _t, #"Q2:Were they late?" = _t, #"Q2:Code 1" = _t, #"Q2:Code 2" = _t]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"ID", "Date"}, "Attribute", "Value"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByEachDelimiter({":"}, QuoteStyle.Csv, false), {"Question", "Text"}),
    #"Code Rows" = Table.SelectRows(#"Split Column by Delimiter", each Text.StartsWith([Text], "Code")),
    #"Pivoted Column" = Table.Pivot(#"Code Rows", List.Distinct(#"Code Rows"[Text]), "Text", "Value"),
    #"Question Rows" = Table.SelectRows(#"Split Column by Delimiter", each not Text.StartsWith([Text], "Code")),
    #"Renamed Columns" = Table.RenameColumns(#"Question Rows",{{"Text", "Question Text"}, {"Value", "Answer"}}),
    #"Merged Queries" = Table.NestedJoin(#"Renamed Columns", {"ID", "Date", "Question"}, #"Pivoted Column", {"ID", "Date", "Question"}, "Renamed Columns", JoinKind.LeftOuter),
    #"Expanded Renamed Columns" = Table.ExpandTableColumn(#"Merged Queries", "Renamed Columns", {"Code 1", "Code 2"}, {"Code 1", "Code 2"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Renamed Columns",{{"ID", Int64.Type}, {"Date", type date}, {"Question", type text}, {"Question Text", type text}, {"Answer", type text}, {"Code 1", Int64.Type}, {"Code 2", Int64.Type}})
in
    #"Changed Type"
ppm1
Solution Sage
Solution Sage

Please see this video on how to handle this pattern. You can also adapt the M code in the sample file in the description to your data.

(1) Faster Data Transformations with List/Record M Functions - YouTube

 

Pat

Microsoft Employee

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.

Top Solution Authors
Top Kudoed Authors