Forum Discussion

ckulkarni97's avatar
ckulkarni97
Frequent Visitor
2 years ago
Solved

Replace Values in Multiple Columns from Other Columns with Condition in PowerBI Power Query

I have combined two tables which have some columns matching.  Table 1 has 401 columns and 200 rows. Table 2 has 197 columns and 75 rows. The common key is column named "Company name". 17 Compani...
  • ronrsnfld's avatar
    ronrsnfld
    2 years ago

    This is a much better explanation of what you want to do.

     

    You obviously need to create a "Map". In the code below I created manually as a Zip'd list of the mapping. Depending on more details, there may or may not be a more efficient method of doing this. 

     

    The map has to represent all columns you wish to replace, whether or not the column names are different.

     

    We use the Map, and the List.Accumulate function to perform the replacements.

     

    Then we set the data types back to what they were before the replacements, as Table.ReplaceValues will change data type to any.

     

    Note that at the beginning of the code, I set all the data types, except for the Company Name, to Int64.Type. This may not be appropriate for your actual data and you will need to  edit those lines to set the data types properly in order to have things work smoothly. The automatic setting in Power Query may be OK for your purposes.

     

     

    let
    
    //Read in the two tables
        Source = Excel.CurrentWorkbook(){[Name="Table1A"]}[Content],
        Table_1 = Table.TransformColumnTypes(Source,
          {{"Company", type text}} & List.Transform(List.RemoveFirstN(Table.ColumnNames(Source),1), each {_, Int64.Type})),
    
        Source2 = Excel.CurrentWorkbook(){[Name="Table2A"]}[Content],
        Table_2 = Table.TransformColumnTypes(Source2,
            {{"Company", type text}} & List.Transform(List.RemoveFirstN(Table.ColumnNames(Source2),1), each {_, Int64.Type})),
    
    //Mapping
        #"Table 1 Cols" = {"% of Males", "%of Females"},
        #"Table 2 Cols" = {"Mal pct", "Female pct"},
        Map = List.Zip({#"Table 1 Cols", #"Table 2 Cols" }),
    
    //Join the tables using Left Outer
        #"Joined Tables" = Table.FuzzyNestedJoin(Table_1, "Company", Table_2,"Company","Joined", JoinKind.LeftOuter),
        
    //Replace Values per the Map
        #"Replace Values" = List.Accumulate(
            Map,
            #"Joined Tables",
            (s,c)=> Table.ReplaceValue(
                s,
                each Record.Field(_,c{0}),
                each if Table.IsEmpty([Joined]) then Record.Field(_,c{0}) else Table.Column([Joined],c{1}){0},
                Replacer.ReplaceValue,
                {c{0}}
            )),
    
        #"Remove Joined Table" = Table.RemoveColumns(#"Replace Values",{"Joined"}),
    
        #"Reset Data Types" = Table.TransformColumnTypes(#"Remove Joined Table",
            List.Zip(
                {Table.Schema(Table_1)[Name], 
                 List.Transform(
                     Table.Schema(Table_1)[TypeName], 
                     each Expression.Evaluate(_,#shared))}
                    )
                    )
    in
        #"Reset Data Types"

     

     

    Result