Power Query: Expendable Expand
18 January 2023
Welcome to our Power Query blog. This week, I investigate how to avoid expanding data after merging.
In Join or List Part 1, I merged queries to get all the data I needed in one table. I had two queries, Expenses and Permissions:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image1-1673602026.png/004917558011874d4a99997852b2ba3e.jpg)
I merged the tables to find out who has expenses that are not allowed or that require more information. I started in Expenses, and I chose ‘Merge Queries’ from the Home tab:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image2-1673602164.png/1bb4d34548a5315af68a2ebcfc94d889.jpg)
This enabled the ‘Merge Queries’ dialog:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image3-1673602189.png/d9d444d23e15b4fd69e0f008ee71fcf3.jpg)
I matched the data on Expense:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image4-1673602241.png/d911221fe62fc5349bfa4749219aa74f.jpg)
I used the icon in the Permissions column to extract the data from the tables:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image5-1673602265.png/fd1027b093e85f57f1e234dd3579e8d8.jpg)
I only needed the information in Column2.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image6-1673602285.png/bbc26d8f2fdb29a41107b06a92941f25.jpg)
I had the data I needed, and I renamed the column Allowed to Claim? However, did I need to expand the data in Permissions to achieve this? There is another way I could have extracted the data:
Let’s revisit. I am starting at the ‘Merged Queries’ step:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image7-1673602306.png/161b2beb7c43bd1d8d92c335cdd1ac7b.jpg)
I choose to add a ‘Custom Column’ from the ‘Add Column’ tab:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image8-1673602326.png/6563a370905cb531a2090cbd7c957472.jpg)
I call the new column Allowed to Claim?, as above:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image9-1673602364.png/067dd0884b861fecddf0f0bca8205e8a.jpg)
The M code for this is:
= Table.FirstValue(Table.SelectColumns([Permissions], {"Column2"}))
This is made up of two steps. Since Permissions is a column of tables, the first step is to use Table.SelectColumns() to extract only Column2 from each table. However, Table.SelectColumns() will return a table with one value in it. In order to extract the data, I use Table.FirstValue() to get the data in the first column and row.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image10-1673602397.png/7ef97198437930e40d69efc32add316a.jpg)
This merely extracts the data from the column. I could also use table functionality to create columns using conditions that compare the data in the query with the data in the table.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image11-1673602437.png/3befc85ed996fc33b047150924d7b0b5.jpg)
The M code here has been extended to:
if (Table.FirstValue(Table.SelectColumns([Permissions], {"Column2"})) = "No") then "Requires Intervention"
else null
I am creating a new column which contains ‘Requires Intervention’ if the salesperson has tried to claim a restricted item.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image12-1673602501.png/2a0edb29952bfa50e8fbfe7a7269e1c9.jpg)
In this case, I have avoided expanding the Permissions tables and created a column that uses a condition using the data in Permissions. I would of course need to set the data type for my new columns, but this technique can save me from extracting columns that I don’t need to include in the query.
Come back next time for more ways to use Power Query!