Forum Discussion

SarahAnn's avatar
SarahAnn
New Member
4 years ago
Solved

Unpivot in Power Query

Hi Everyone,   I am pretty new to Power Query and my data is in a format that makes it very hard to use. I want to unpivot it but cannot quite figure out how to do what I am needing to do. The data...
  • HotChilli's avatar
    4 years ago

    Move the Review Date column to the left (in 2nd position).

    Select the 1st four columns and Unpivot Other Columns.

    Split the Attribute column (from non-digit to digit).

    Select the Attribute1 column and Pivot (with Value in Values) and Don't Aggregate from the Advanced.

     

    --

    Let me know how it goes

     

  • AlexisOlson's avatar
    4 years ago

    Unpivot every column ending in a digit, split the digit off of the end (Home > Transform > Spit Column > By Non-Digit to Digit), and then re-pivot.

     

    After pivoting and splitting:

     

    Full code you can paste into the Advanced Editor:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lc+xCsIwEMbxVymZCzWX5AFqVRAUxNopdAg1gyIWqoOPb7LI5bjBm/LP1w6/eK+0qtVpme9xeldtat3oBlaQ56Ev7114hmsot2P83Ka53PavJcRHuUmyupyHbTp37aHPJ7n+/hxrrwDx16mB8IHhA8MHho82SUr4BvG71IbwDcM3DN8wfLRJUsK3iL9JbQnfMnzL8C3DR5skKZ88o+A7xM8fHeE7hu8YvmP4aJPk3/zxCw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Project Number" = _t, ProjectName = _t, Modified = _t, LaunchCountryCode1 = _t, LaunchDate1 = _t, LaunchCountryCode2 = _t, LaunchDate2 = _t, LaunchCountryCode3 = _t, LaunchDate3 = _t, LaunchCountryCode4 = _t, LaunchDate4 = _t, PrelaunchDate1 = _t, PrelaunchDate2 = _t, PrelaunchDate3 = _t, PrelaunchDate4 = _t, PlantoLaunch1 = _t, PlantoLaunch2 = _t, PlantoLaunch3 = _t, PlantoLaunch4 = _t, #"Review Date" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Project Number", Int64.Type}, {"ProjectName", type text}, {"Modified", type date}, {"LaunchCountryCode1", type text}, {"LaunchDate1", type date}, {"LaunchCountryCode2", type text}, {"LaunchDate2", type date}, {"LaunchCountryCode3", type text}, {"LaunchDate3", type date}, {"LaunchCountryCode4", type text}, {"LaunchDate4", type date}, {"PrelaunchDate1", type date}, {"PrelaunchDate2", type date}, {"PrelaunchDate3", type date}, {"PrelaunchDate4", type date}, {"PlantoLaunch1", type logical}, {"PlantoLaunch2", type logical}, {"PlantoLaunch3", type logical}, {"PlantoLaunch4", type logical}, {"Review Date", type date}}),
        #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Project Number", "ProjectName", "Modified", "Review Date"}, "Attribute", "Value"),
        #"Split Column by Character Transition" = Table.SplitColumn(#"Unpivoted Columns", "Attribute", Splitter.SplitTextByCharacterTransition((c) => not List.Contains({"0".."9"}, c), {"0".."9"}), {"Attribute", "Index"}),
        #"Pivoted Column" = Table.Pivot(#"Split Column by Character Transition", List.Distinct(#"Split Column by Character Transition"[Attribute]), "Attribute", "Value"),
        #"Changed Type1" = Table.TransformColumnTypes(#"Pivoted Column",{{"LaunchCountryCode", type text}, {"LaunchDate", type date}, {"PrelaunchDate", type date}, {"PlantoLaunch", type logical}})
    in
        #"Changed Type1"