Power Query: Simply Conditional
19 August 2020
Welcome to our Power Query blog. This week I look at a method to conditionally replace values efficiently.
I have the following tent data:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/194/image1.png/e774d10cbbb9450fc45efbe51abdf434.jpg)
I want to add a prefix to the data in my Awning column, which is dependent on the value in that column, viz.
- Blank: N/A
- Standard or Dining: Fixed
- Budget: (No prefix)
- Package or Kids: Variable.
I begin by extracting my data to Power Query using ‘From Table’ on the ‘New Query’ section of the ‘Get & Transform’ section on the Data tab.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/194/image2.png/f32e5a15e2cf9c3e4d2d058458ce054d.jpg)
I accept the defaults.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/194/image3.png/f1140ff857fc3b6f5f97a6a24f4a6fc7.jpg)
I could use a custom column to achieve my required result:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/194/image4.png/72aa864d2854c6fefb1083fba0ab5792.jpg)
The M code I have used is:
if [Awning] is null then "N/A "
else if [Awning] = "Package" or [Awning]= "Kids" then "Variable " & [Awning]
else if [Awning] = "Standard" or [Awning] = "Dining" then "Fixed " & [Awning]
else [Awning]
When I click ‘OK’, I get my new column:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/194/image5.png/36776d1da4d05b45bb5a5d09375f407c.jpg)
Taking the ‘if’ statement I used, I can do this another way, by using 'Replace values’.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/194/image6.png/23912d3b1671861e02bebcd5183f1607.jpg)
I am going to start by creating a simple ‘Replace values’ statement by replacing null with ‘N/A’:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/194/image7.png/6f49c288a0d88a66b427eaf4ece923d6.jpg)
When I click ‘OK’, the null values are replaced.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/194/image8.png/b9ee28d90e6b5bc92ea4aeafdad51628.jpg)
More importantly, the M code for this step has been generated:
= Table.ReplaceValue(#"Changed Type",null,"N/A",Replacer.ReplaceValue,{"Awning"})
I need to change this so that I can apply a change to every row, so instead of null, I am going to use ‘each [Awning]’:
= Table.ReplaceValue(#"Changed Type",each [Awning], "N/A",Replacer.ReplaceValue,{"Awning"})
I am now going to replace ‘N/A’ with the ‘if’ statement I used earlier, prefixed by ‘each’ as I am applying it to each row.
= Table.ReplaceValue(#"Changed Type",each [Awning], each if [Awning] is null then "N/A "
else if [Awning] = "Package" or [Awning]= "Kids" then "Variable " & [Awning]
else if [Awning] = "Standard" or [Awning] = "Dining" then "Fixed " & [Awning]
else [Awning]
,Replacer.ReplaceValue,{"Awning"})
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/194/image9.png/0485ccbc83bdeec1d741bad442a1ea5f.jpg)
When I click the tick next to my amended step, I can see the results.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/194/image10.png/daf8c4f0259ce428269c0d3d4badd32b.jpg)
I have applied a conditional change in my column without the need for an extra column.
Come back next time for more ways to use Power Query!