Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
8 years ago
Solved

Sum column range

Hi there,    I want to create a custom column in which i sum a range of columns from the dataset. It needs to be a range, as the it will be a generic report which needs to be able to handle differe...
  • v-yulgu-msft's avatar
    8 years ago

    Hi Anonymous,

     

    Please try below steps.

     

    1. Select [TimeStamp] column then Unpivot other columns.

    2. Add a custom column.

       

        

    3. Pivot table to convert it to original structure.

        

     

    Power Query reference:

    let
        Source = Excel.Workbook(File.Contents("C:\Users\xxxx\Desktop\Sample Data.xlsx"), null, true),
        Test4_Sheet = Source{[Item="Test4",Kind="Sheet"]}[Data],
        #"Promoted Headers" = Table.PromoteHeaders(Test4_Sheet, [PromoteAllScalars=true]),
        #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"TimeStamp", type time}, {"column A", Int64.Type}, {"column B", Int64.Type}, {"column C", Int64.Type}, {"column D", Int64.Type}}),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"TimeStamp"}, "Attribute", "Value"),
        #"Renamed Columns" = Table.RenameColumns(#"Unpivoted Other Columns",{{"Attribute", "ColumnName"}}),
        #"Added Custom" = Table.AddColumn(#"Renamed Columns", "Sum", (This) => List.Sum(Table.SelectRows(#"Renamed Columns",each [TimeStamp] = This[TimeStamp])[Value])),
        #"Pivoted Column" = Table.Pivot(#"Added Custom", List.Distinct(#"Added Custom"[ColumnName]), "ColumnName", "Value")
    in
        #"Pivoted Column"

     

    Result.

     

    Best regards,

    Yuliana Gu