Forum Discussion

nok's avatar
nok
Advocate II
3 months ago
Solved

Split and transform columns into rows

Hi! I have a table in Excel that follows this format: Co.[01/01/2026]   Co. [01/02/2026]   Co. [01/03/2026]   Co. [01/04/2026]   Co. [01/05/2026]   Co. [01/06/2026]   Co. [01/07/...
  • pcoley's avatar
    3 months ago

    nok Please try with this PQ script:

    (change the text YOURPATH, YOURBOOKNAME and SHEETNAME of the first two steps accordling to your file path, excelbookname and sheetname) 

    let
      Source = Excel.Workbook(File.Contents("YOURPATH\YOURBOOKNAME.xlsx"), null, true), 
      SheetWithData = Source{[Item = "SHEETNAME", Kind = "Sheet"]}[Data], 
      #"Filtered Rows" = Table.SelectRows(SheetWithData, each [Column1] <> null and [Column1] <> ""), 
      #"Transposed Table" = Table.Transpose(#"Filtered Rows"), 
      #"Trimmed Text" = Table.TransformColumns(#"Transposed Table", {{"Column1", Text.Trim, type text}}), 
      #"Cleaned Text" = Table.TransformColumns(#"Trimmed Text", {{"Column1", Text.Clean, type text}}), 
      #"Replaced Value" = Table.ReplaceValue(
        #"Cleaned Text", 
        "[", 
        "", 
        Replacer.ReplaceText, 
        {"Column2"}
      ), 
      #"Replaced Value1" = Table.ReplaceValue(
        #"Replaced Value", 
        "]", 
        "", 
        Replacer.ReplaceText, 
        {"Column2"}
      ), 
      #"Changed Type" = Table.TransformColumnTypes(
        #"Replaced Value1", 
        {{"Column2", type date}, {"Column3", Int64.Type}}
      ), 
      #"Pivoted Column" = Table.Pivot(
        #"Changed Type", 
        List.Distinct(#"Changed Type"[Column1]), 
        "Column1", 
        "Column3", 
        List.Sum
      )
    in