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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

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
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors
Top Kudoed Authors