Power Query: Missing in Transformation
10 June 2020
Welcome to our Power Query blog. This week, I look at a method to make column transformations more dynamic.
John, one of my imaginary salespeople, has been busy submitting his expenses. I used this same data in Power Query: Dynamic Removals, but this time, I am using another method to dynamically transform my columns.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/184/image1.png/e774d10cbbb9450fc45efbe51abdf434.jpg)
I extract my data to Power Query using ‘From Table’ in the ‘Get & Transform’ section of the Data tab.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/184/image2.png/f32e5a15e2cf9c3e4d2d058458ce054d.jpg)
I change the data type on Amount, Card, Cash, Mike and Cheque to ‘Currency’.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/184/image3.png/f1140ff857fc3b6f5f97a6a24f4a6fc7.jpg)
I do this by selecting my columns and setting the Datatype on the Transform tab (I could have used this option on the Home tab too).
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/184/image4.png/72aa864d2854c6fefb1083fba0ab5792.jpg)
I rename this step ‘Change to Currency’, as I am going to show what happens if John doesn’t have a column for Mike. I close and load this query and go back to my original worksheet:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/184/image5.png/36776d1da4d05b45bb5a5d09375f407c.jpg)
I remove the Mike column and return to my query.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/184/image6.png/23912d3b1671861e02bebcd5183f1607.jpg)
As expected, I get an error. This is because the M code in the ‘Change to Currency’ step refers to Mike:
= Table.TransformColumnTypes(#"Source",{{"Amount", Currency.Type}, {"Card", Currency.Type}, {"Cash", Currency.Type}, {"Mike", Currency.Type}, {"Cheque", Currency.Type}})
I am going to create a function that will only transform columns if they exist. To do this, I create a new blank query. I can do this from the Excel workbook by choosing ‘Blank Query’ in the ‘From Other Sources’ section of ‘New Query’ in the ‘Get & Transform’ grouping of the Data tab.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/184/image7.png/6f49c288a0d88a66b427eaf4ece923d6.jpg)
In the Advance Editor, I add the following M code:
(table as table,
typeTransformations as list,
optional culture as nullable text) as table =>
List.Accumulate(
typeTransformations,
{},
(x, y) => try Table.TransformColumnTypes(table, y, culture)
otherwise x
)
This means that I only try and transform the column if it exists. This will work for any column transformation I wish to apply.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/184/image8.png/b9ee28d90e6b5bc92ea4aeafdad51628.jpg)
I call the function DynamicTransform.
I go back to the query which gave an error for the missing Mike column and adjust the M code in the ‘Change to Currency’ step:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/184/image9.png/0485ccbc83bdeec1d741bad442a1ea5f.jpg)
Instead of using Table.TransformColumnTypes, I use my new function:
= DynamicTransform(#"Source",{{"Amount", Currency.Type}, {"Card", Currency.Type}, {"Cash", Currency.Type}, {"Mike", Currency.Type}, {"Cheque", Currency.Type}})
And now, the error does not occur:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2020/power-query/184/image10.png/daf8c4f0259ce428269c0d3d4badd32b.jpg)
I now have a dynamic way to apply changes to John’s columns even if some of them are missing.
Come back next time for more ways to use Power Query!