Forum Discussion

nok's avatar
nok
Advocate II
3 months ago
Solved

Split and rearrange columns

Hi! I have a table in Excel that follows this format: ID    Project   Cz[01/01/2026]   Cz[01/02/2026]   Cz[01/03/2026]   Fz[01/01/2026]   Fz[01/02/2026]   Fz[01/03/2026]   Fz[01/04/20...
  • MFelix's avatar
    3 months ago

    Hi nok ,

     

    In power query do the following steps:

    • Select the ID and Project Column
    • Transform - Unpivot columns - Unpivot Others

     

    • Select the attribute column
    • Split Column by delimeter

    • Use the [ as delimiter on the split

     

    • On attribute 2 just replace the ] by blank
    • Then rename columns and format has needed

     

    • Select text column
    • Pivot -> Value -> Don't aggregate

     

    Check the full code below:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TY27DoAgDEV/hXRmgJaHjPoF7oTJuLiYGP8/tncgMhyaPs7tnSJ52p/7Oo/XrW4+7UoIygRmsDVFRBlDBBkUMIEZLHZkZREavhP/YjbT2ixXk5qI4YGGoWELqdkCpULJS8Unc103x/gA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"ID   " = _t, #"Project  " = _t, #"Cz[01/01/2026]  " = _t, #"Cz[01/02/2026]  " = _t, #"Cz[01/03/2026]  " = _t, #"Fz[01/01/2026]  " = _t, #"Fz[01/02/2026]  " = _t, #"Fz[01/03/2026]  " = _t, #"Fz[01/04/2026]  " = _t, #"Fz[01/05/2026]  " = _t, #"Fz[01/06/2026]  " = _t, #"Fz[01/07/2026]  " = _t, #"Fz[01/08/2026]  " = _t, #"Az[01/01/2026]  " = _t, #"Az[01/02/2026]  " = _t]),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"ID   ", "Project  "}, "Attribute", "Value"),
        #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByDelimiter("[", QuoteStyle.Csv), {"Attribute.1", "Attribute.2"}),
        #"Replaced Value" = Table.ReplaceValue(#"Split Column by Delimiter","]","",Replacer.ReplaceText,{"Attribute.2"}),
        #"Changed Type" = Table.TransformColumnTypes(#"Replaced Value",{{"Attribute.2", type date}}),
        #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Attribute.2", "Date"}, {"Attribute.1", "Text"}}),
        #"Pivoted Column" = Table.Pivot(#"Renamed Columns", List.Distinct(#"Renamed Columns"[Text]), "Text", "Value"),
        #"Changed Type1" = Table.TransformColumnTypes(#"Pivoted Column",{{"Cz", Int64.Type}, {"Fz", Int64.Type}, {"Az", Int64.Type}})
    in
        #"Changed Type1"