Forum Discussion

Niels_T's avatar
Niels_T
Icon for Post Patron rankPost Patron
5 years ago
Solved

How to fill blanks in column through Power Query

Hello,   I have a column "Countries" and a column "telephone". In the "countries" column I have blank data but, I want to fill this up through Power Query with the help of the "telephone" column. ...
  • PhilipTreacy's avatar
    PhilipTreacy
    5 years ago

    All good Niels_T 

    I've modified the code slightly to take into account the true format of the phone numbers (text) we're dealing with.  Replace the code from (and including) the #"Added Custom2" step with this

     

        #"Added Custom2" = Table.AddColumn(#"Replaced Value23", "Custom", each if Text.Contains(Text.Start([telephone],3), "+44") or Text.Contains(Text.Start([telephone],4), "0044") then "Great Britain" 
    
            else if Text.Contains(Text.Start([telephone], 4) , "0032") then "Belgium" 
    
            else if Text.Contains(Text.Start([telephone], 3), "001") then "United States" 
    
            else if Text.Contains(Text.Start([telephone], 4) , "0061") then "Australia" 
    
            else null),
    
        #"Removed Columns2" = Table.RemoveColumns(#"Added Custom2",{"Countries"}),
        #"Renamed Columns2" = Table.RenameColumns(#"Removed Columns",{{"Custom", "Countries"}})
    in
        #"Renamed Columns2"

     

     

    Regarding any blanks, I've written code to take account of UK and Belgian numbers, and added a coupleof extra else if statements in to illustrate how to modify the code for other countries.

    else if Text.Contains(Text.Start([telephone], 3), "001") then "United States" 

    You can see that the code is checking the first 3 characters of the phone number (using the Text.Start function) to see if it matches (Text.Contains) the string "001".  If it does then that phone number is from the United States.

    For Australian numbers the code has to check the first 4 characters for "0061".

    Hopefulyl that makes it clear how to add code for other countries but if you get stuckjust post back.

    regards

    Phil

  • PhilipTreacy's avatar
    PhilipTreacy
    5 years ago

    Hi Niels_T 

    Sorry, long day, doing code in my head ....

    #"Renamed Columns2" = Table.RenameColumns(#"Removed Columns2",{{"Custom", "Countries"}})

    Regards

    Phil