Forum Discussion
Replace null values with contents from another column
- 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]
- 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.
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!
Hey everyone!
Thank you for the video which was very helpful - I was just curious if anyone knew a way to replace a null value with a new value specific on a different column but not matching the other column? e.g. If 'PRODUCT NAME' includes 'Barbie' or 'Playdough' replace with 'Toys', If 'PRODUCT NAME' includes 'Tea', 'Biscuits', 'Noodles' replace with 'Consumables', etc.
Essentially I have several hundred thousand rows each with a unique sales value. Most have a category allocated already, but some have been left blank. I want to replace the nul value with one of 5 categories depending on the product brand in the name.
No clue if this is even a remote possibility but thought it was worth asking as I am stumped.
- TheOriginal10 months agoNew Member
Something like this should work I think
// Replace null values in a ExistingColumnWithNulls based on new conditions
ReplaceNulls = Table.ReplaceValue(
#"PreviousStep",
null,
each if Text.Contains(_[PRODUCT], "Barbie") or Text.Contains(_[PRODUCT], "Playdough") then "Toys"
else if Text.Contains(_[PRODUCT], "Tea") or Text.Contains(_[PRODUCT], "Biscuits") or Text.Contains(_[PRODUCT], "Noodles") then "Consumables"
else null,
Replacer.ReplaceValue,
{"ExistingColumnWithNulls"}
)
in
ReplaceNulls