Forum Discussion

thomaskelly's avatar
thomaskelly
Icon for Helper I rankHelper I
9 years ago
Solved

Replace null values with contents from another column

Good Morning   Is it possible in query editor to replace the null values of one column with the adjcent contents of another column?   Many Thanks   Tom
  • MattAllington's avatar
    9 years ago

    Yes. Select Add Column and then write an if statement something like this (it is case sensitive)

     

    = if [column 1] = null then [column 2] else [column 1] 

  • bdymit's avatar
    9 years ago

    Saw this old post and found another solution, so I thought I would share.

     

    You can modify the "M" code if you are not looking to add a column/ delete the old one.

     

    =Table.ReplaceValue(#"Last Step",null, each _[Values Column],Replacer.ReplaceValue,{"Null Column"})

     

    #"Last Step" being the previous step in your query

    [Values Column] being the column that has the values in it to replace the nulls

    "Null Column" being the column with the null values

     

    Be sure to use the " each _[Values Column]" syntax with the spaces before and after "each", otherwise you will get an error.

     

    Here is the original video from Miguel Escobar.

    https://www.poweredsolutions.co/2015/05/08/power-query-for-excel-replace-values-using-values-from-another-column/

     

    Cheers!

     

    **NOTE: When I have used this, it changed all the data types in my query to "Any". I asked Miguel, and he reached out to MS to see if it is a bug or if it is intentional. If you are using it early in your query before you change your data types, might still be useful. Otherwise you can change your data types back. Just a fair warning!