Power Query: Heading Off – Part 7
27 March 2024
Welcome to our Power Query blog. Today, I refine the solution to an issue that occurs when I load to a Table with no headers.
I plan to show a particular issue with Power Query and Tables without headers. However, first I need to create the scenario, and I will show a few methods and tips along the way. I have two Tables of data:
- contains my salespeople’s expenses (Expenses)
- determines the expenses that will be covered by each supplier (Supplier_Limit).
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image1-1707934940.png/ef269a5646089e1768b92c98624ba127.jpg)
In Part 1, I created two [2] queries, and grouped Expenses.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image2-1707934970.png/b33577bd2e64f672cfc7027595c5b984.jpg)
In Part 2, I merged Expenses with the Supplier_Limit query to create Limit_Exceeded, which tells me if any limits have been breached.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image3-1707934992.png/22b34ed6353c424e702ca5312642e9a8.jpg)
In Part 3,I loaded Limit_Exceeded to a new worksheet.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image4-1707935598.png/38d61042abde2ad560d8d96729ec3f16.jpg)
I removed the header row and changed some of the data in Expenses.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image5-1707935617.png/1dc4f4e0b0451373dc0e81129c277761.jpg)
In Part 4, I ran the query for a single salesperson.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image6-1707935650.png/ee3604fc3a88f1f1542e568e3960445a.jpg)
I extracted this cell and created a parameter P_Salesperson:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image7-1707935683.png/478d745575ee5a607eefb7a520fd8969.jpg)
I used this parameter to limit the data in Expenses:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image8-1707935705.png/e319f0db2b88b11906de4744e451f26e.jpg)
When I refreshed Limit_Exceeded, I checked the results:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image9-1707935730.png/709c92cdc01897fa9ba8178b328ee768.jpg)
The results correctly showed one row of data and no headings. However, things changed in part 5 when I selected a different Salesperson:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image10-1707935768.png/52d50e717ba4d75e76b958b4d01a3b1e.jpg)
When I refreshed Limit_Exceeded, it had moved the data from the previous selection into the header row.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image11-1707935794.png/b59051799512c07c01217538cf7f03b7.jpg)
When I deleted the top row from the Excel sheet, things got worse!
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image12-1707935812.png/32d089bb8ae61bc3f5faa17a5e8b76b3.jpg)
Last time, I changed what happens if there is no data returned by Limit_Exceeded. I created a basic query with one [1] blank row and appended it to Limit_Exceeded.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image19-1707936028.png/e54a63e5d9cb20a3d3c4b1659ef2f275.jpg)
I deleted Column1, and loaded the results to the Excel workbook:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image20-1707936050.png/8dcdf60619b2ea7f46aab696c7cdebdf.jpg)
This is looking much better! Clearly, it’s not ideal to have a blank row, but it is better than showing the wrong data. I’ll refine this approach so that I don’t add a blank row if I have data. First, let’s see what happens if I change the selected Salesperson back to ‘Mary’, and refresh Limit_Exceeded:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image16-1710761936.png/d830e7a63fb5bd8054c7b73ab583efde.jpg)
As expected, I get two rows, as indicated by the values in the ‘Queries & Connections’ pane.
Back in my query, I look at the M code in the ‘Advanced Editor’, which I may access from the Home tab:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image17-1710761979.png/0afc2b3d5759d6e87a20f8d2335d1243.jpg)
The last two steps pertain to the actions needed when the query doesn’t return any rows for step ‘Removed Columns’. Let’s look at that section:
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Flag"}),
#"Appended Query" = Table.Combine({#"Removed Columns", NoRows}),
#"Removed Columns1" = Table.RemoveColumns(#"Appended Query",{"Column1"})
in
#"Removed Columns1"
To make things clearer, I am going to rename step ‘Removed Columns’ to ‘Limits_Exceeded’.
Limits_Exceeded = Table.RemoveColumns(#"Filtered Rows",{"Flag"}),
Next, I determine whether there are any rows returned by step ‘Limits_Exceeded’. I am going to use the M function Table.IsEmpty(). This returns ‘TRUE’ or ‘FALSE’ for the query interrogated:
Table.IsEmpty(table as table) as logical
I will use this function to construct a condition for appending an empty row:
Check_NoRows = Table.IsEmpty(Limits_Exceeded),
I can include this to determine what the final two steps do. If there are rows, then I make them equal to Limited_Exceeded, which means that no changes are made:
Limits_Exceeded = Table.RemoveColumns(#"Filtered Rows",{"Flag"}),
Check_NoRows = Table.IsEmpty(Limits_Exceeded),
#"Appended Query" = if Check_NoRows then Table.Combine({Limits_Exceeded, NoRows}) else Limits_Exceeded,
#"Removed Columns1" = if Check_NoRows then Table.RemoveColumns(#"Appended Query",{"Column1"}) else Limits_Exceeded
in
#"Removed Columns1"
This gives me the complete query:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image17-1710762169.png/e15e8156282a67ca8ed9bb3ac97758ce.jpg)
I click Done, and check if this has the desired effect:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image18-1710762200.png/10a2ce6bfd3ef4d35016d6d42e24103e.jpg)
This looks fine so far; I load the query to the Excel workbook:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image19-1710762222.png/d5e03de53860b3b57c1d2c48e49fffd7.jpg)
One row has been loaded. I change the Salesperson back to ‘Newbie’ and refresh the query:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/./image20-1710762245.png/26764c2a5ba4a57a979ac1644a6eb1ba.jpg)
I have one blank row loaded, so this works as I intended. Next time, I’ll look at an alternative way to include a blank row when needed.
Come back next time for more ways to use Power Query!