Forum Discussion

snaise's avatar
snaise
Frequent Visitor
3 years ago
Solved

Join all sheets in one excel after transforming them individually

have an excel file with multiple sheets which I want first to transform them individually (remove 3 top rows, promote headers, remove top row, unpivot columns that are not column1=), and then annex t...
  • AlienSx's avatar
    3 years ago

    hi, snaise 

    1. Create a custom function with all your transformations steps.

    2. Read your file with Excel.Workbook(File.Contents("C:\Book1.xlsx"), null, true)

    3. Filter [Kind] column to get sheets only. 

    4. Apply your function to [Data] column: Table.TransformColumns(Source, each {"Data", my_function})

    5. Table.Combine [Data] column

    So 

     

    let
        Source = Excel.Workbook(File.Contents("C:\Book1.xlsx"), null, true),
        filter_sheets = Table.SelectRows(Source, each ([Kind] = "Sheet")),
        my_function = (t as table) =>
            let 
                a = Table.RemoveFirstN(t, 3),
                b = Table.PromoteHeaders(a),
                c = Table.RemoveFirstN(b, 1),
                d = Table.UnpivotOtherColumns(c, {"Column1"}, "Attribute", "Value")
            in d,
        transform = Table.TransformColumns(filter_sheets, {"Data", my_function}),
        combine = Table.Combine(transform[Data])
    in
        combine