Forum Discussion

smpa01's avatar
smpa01
Community Champion
7 years ago
Solved

Ignore missing column while summing column

Hi,   My source data consists of following 3 columns C1, C2 and C3. I add these 3 columns to get an extra column called Sum.     The problem is the source data may or may not contain all 3...
  • smpa01's avatar
    smpa01
    7 years ago

    Hey Stachu,

     

    I found a solution courtesy to the following two links

    https://blog.crossjoin.co.uk/2015/02/26/handling-added-or-missing-columns-in-power-query/

    https://community.powerbi.com/t5/Desktop/Unpivot-removes-rows-with-no-null-values-how-to-keep-them/m-p/361837#M163405

     

    My Base headers are following - Table 0 (2)

     

    let
        Source = Web.Page(Web.Contents("https://community.powerbi.com/t5/Desktop/Ignore-missing-column-while-summing-column/m-p/552485")),
        Data0 = Source{0}[Data],
        #"Promoted Headers" = Table.PromoteHeaders(Data0, [PromoteAllScalars=true]),
        #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Project", type text}, {"C1", Int64.Type}, {"C2", Int64.Type}, {"C3", Int64.Type}}),
        #"Removed Bottom Rows" = Table.RemoveLastN(#"Changed Type",3),
        Custom1 = Table.ColumnNames(#"Removed Bottom Rows")
    in
        Custom1

    I create a another query - Table 0 (3) to find which column is missing from Base headers to the current table

     

     

     

    let
        Source = Web.Page(Web.Contents("https://community.powerbi.com/t5/Desktop/Ignore-missing-column-while-summing-column/m-p/552485")),
        Data0 = Source{0}[Data],
        #"Promoted Headers" = Table.PromoteHeaders(Data0, [PromoteAllScalars=true]),
        #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Project", type text}, {"C1", Int64.Type}, {"C2", Int64.Type}, {"C3", Int64.Type}}),
        #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"C3"}),
        PresentColumns = Table.ColumnNames(#"Removed Columns"),
        Source1 = Web.Page(Web.Contents("https://community.powerbi.com/t5/Desktop/Ignore-missing-column-while-summing-column/m-p/552485")),
        Data01 = Source1{0}[Data],
        #"Promoted Headers1" = Table.PromoteHeaders(Data01, [PromoteAllScalars=true]),
        #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers1",{{"Project", type text}, {"C1", Int64.Type}, {"C2", Int64.Type}, {"C3", Int64.Type}}),
        #"Removed Bottom Rows" = Table.RemoveLastN(#"Changed Type1",3),
        ExpectedColumns = Table.ColumnNames(#"Removed Bottom Rows"),
        Custom1 = List.Difference(ExpectedColumns, PresentColumns),
        #"Converted to Table" = Table.FromList(Custom1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Promoted Headers2" = Table.PromoteHeaders(#"Converted to Table", [PromoteAllScalars=true]),
        #"Changed Type2" = Table.TransformColumnTypes(#"Promoted Headers2",{{"C3", type any}})
    in
        #"Changed Type2"

    Finally - Table 0 - to add the missing column to the current table, Replace all null with 0 and then add the columns

     

     

     

    let
        Source = Web.Page(Web.Contents("https://community.powerbi.com/t5/Desktop/Ignore-missing-column-while-summing-column/m-p/552485")),
        Data0 = Source{0}[Data],
        #"Promoted Headers" = Table.PromoteHeaders(Data0, [PromoteAllScalars=true]),
        #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Project", type text}, {"C1", Int64.Type}, {"C2", Int64.Type}, {"C3", Int64.Type}}),
        #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"C3"}),
        #"Appended Query" = Table.Combine({#"Removed Columns", #"Table 0 (3)"}),
        Custom1 = Table.TransformColumns(#"Appended Query",{},(x) => Replacer.ReplaceValue(x,null,0)),
        #"Inserted Sum" = Table.AddColumn(Custom1, "Addition", each List.Sum({[C1], [C2], [C3]}), type number)
    in
        #"Inserted Sum"