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
hazwoper
Frequent Visitor

Unpivot 2 Paired Columns together

Hello-- I would like to unpivot each paired Action/Category columns if there is any text in any of the action categories. This is the format of my data:

 

AccountRegionManagerAction1Category1Action2Category2Action3Category3
ABCWestJohn SmithSign DocumentOffice  Empty BinWaste
XYZNorthJohn Doe  Make CopiesOffice  
123SouthJane Doe  Send EmailOfficeRecycle boxesWaste

 

Not all action columns will have text in it; if they do, then the category column will always be filled in next to it. (IE. a category column will be empty unless there is text in the action category to the left of it). 

 

I would like to extract each action/category pairing into its own row if there is any text in the action categories, so it is formatted like this:

 

 

AccountRegion ManagerActionCategory
ABC West John Smith Sign DocumentOffice
ABCWestJohn SmithEmpty BinWaste
XYZNorth John DoeMake CopiesOffice
123SouthJane DoeSend Email

Office

123SouthJane DoeRecycle Boxes

Office

 

I've tried combinations of selecting unpivot columns but nothing is working for me. Any ideas?

 

 

 

 

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

Unpivot all columns except the first three columns,

AlexisOlson_0-1678219794483.png

 

Filter blank values and split off the number suffix:

AlexisOlson_1-1678219850386.png

 

Pivot the attribute column:

AlexisOlson_2-1678219901568.png

 

Full sample query you can paste into the Advanced Editor of a new blank query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc7NCsIwDADgVwk976I+gfu5CCrYgz9lh1ozV7Y2w3bg3t627jD0EJIQ8iVCsG1esIyd0fmQdtRa4Eb7NjRcPy2UpEaDNg6PTaMVhgLmqMzgJ8i1jYB0HlmdCXa53kJ/oFdCkljScm0vO4SCBo3uX43Car2J52n8CtLij8DRPqAyUvdL4IRqUj3Cnd5Jnl+qPw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Account = _t, Region = _t, Manager = _t, Action1 = _t, Category1 = _t, Action2 = _t, Category2 = _t, Action3 = _t, Category3 = _t]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Account", "Region", "Manager"}, "Attribute", "Value"),
    #"Filtered Rows" = Table.SelectRows(#"Unpivoted Other Columns", each ([Value] <> " ")),
    #"Split Column by Positions" = Table.SplitColumn(#"Filtered Rows", "Attribute", Splitter.SplitTextByPositions({0,1}, true), {"Attribute", "Index"}),
    #"Pivoted Column" = Table.Pivot(#"Split Column by Positions", List.Distinct(#"Split Column by Positions"[Attribute]), "Attribute", "Value")
in
    #"Pivoted Column"

 

View solution in original post

2 REPLIES 2
hazwoper
Frequent Visitor

@AlexisOlson Thank you! It worked. 

AlexisOlson
Super User
Super User

Unpivot all columns except the first three columns,

AlexisOlson_0-1678219794483.png

 

Filter blank values and split off the number suffix:

AlexisOlson_1-1678219850386.png

 

Pivot the attribute column:

AlexisOlson_2-1678219901568.png

 

Full sample query you can paste into the Advanced Editor of a new blank query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc7NCsIwDADgVwk976I+gfu5CCrYgz9lh1ozV7Y2w3bg3t627jD0EJIQ8iVCsG1esIyd0fmQdtRa4Eb7NjRcPy2UpEaDNg6PTaMVhgLmqMzgJ8i1jYB0HlmdCXa53kJ/oFdCkljScm0vO4SCBo3uX43Car2J52n8CtLij8DRPqAyUvdL4IRqUj3Cnd5Jnl+qPw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Account = _t, Region = _t, Manager = _t, Action1 = _t, Category1 = _t, Action2 = _t, Category2 = _t, Action3 = _t, Category3 = _t]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Account", "Region", "Manager"}, "Attribute", "Value"),
    #"Filtered Rows" = Table.SelectRows(#"Unpivoted Other Columns", each ([Value] <> " ")),
    #"Split Column by Positions" = Table.SplitColumn(#"Filtered Rows", "Attribute", Splitter.SplitTextByPositions({0,1}, true), {"Attribute", "Index"}),
    #"Pivoted Column" = Table.Pivot(#"Split Column by Positions", List.Distinct(#"Split Column by Positions"[Attribute]), "Attribute", "Value")
in
    #"Pivoted Column"

 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors
Top Kudoed Authors