In Excel 2016, after creating a query with Get & Transform, when I'm in the Query Editor, done editing the query, and click "Close & Load" > "Close & Load To", I get the "Import Data" dialog as expected. On that dialog, I check the "Add this data to the
Data Model" checkbox to add the data to the data model, and click OK. All seems well so far.
But when I inspect the data in the worksheet table it just created, items that had trailing spaces no longer have those trailing spaces! It doesn't delete leading spaces, nor internal spaces. It just deletes trailing spaces.
When I go back to the query, the trailing spaces are still there, even in the very last step of the transform (to verify that, I right-click any one of them in the Query Editor, choose Copy, and then paste into Notepad, where I see that the trailing spaces
are included when I paste). I see no indication anywhere that I've asked it to remove trailing spaces. I see no indication that it's going to do so. But in the table that query loads to, those trailing spaces are gone (I see that by selecting one of the cells in
the worksheet table and inspecting the item in Excel's formula bar).
If I do everything the same except just don't check the checkbox to add it to the Data Model, the problem doesn't happen -- the trailing spaces are still there in the worksheet table it loads to. But I want this data in the Data Model.
I need those trailing spaces, and I want this data in the Data Model. How do I make it stop removing the trailing spaces?
UPDATE:
Although I originally saw this issue when querying on a SQL Server database, I've reproduced the problem using only Excel, creating a small sample Excel workbook and querying on it. Here are the complete repro steps using only Excel:
- In Excel 2016, create a new workbook.
- In cell A1, enter "MyField" (without quotes).
- In cell A2, enter " abc 123 " (without quotes) (that's two spaces, then "abc", then two more spaces, then "123", then two more spaces, for a total of 6 spaces.
- Save and close the workbook.
- Create another new workbook in Excel 2016.
- Choose Data tab > Get & Transform section > New Query > From File > From Workbook.
- Browse to the workbook you had just created and click Import. That brings up the Navigator dialog.
- In the Navigator dialog, on the left-hand side, select the worksheet name. You'll see your data appear on the right. On the bottom of the dialog, click Edit. That brings up the Query Editor.
- In the Query Editor, choose Home tab > Transform section > Use First Row As Headers. That promotes the cell with MyField from data row to header.
- Next, verify that the trailing spaces are still there in the " abc 123 ". Right-click that data cell and choose Copy. Then open Notepad and choose Paste. Result: all 6 spaces are there.
- Back in the Query Editor, choose Home tab > Close section > Close & Load > Close & Load To. That dismisses the Query Editor and brings up the Load To dialog.
- In the Load To dialog, check the "Add this data to the Data Model" checkbox, and click Load. That loads the query result to a worksheet.
- In the query result on the worksheet, select the cell with the " abc 123 ". Now click in Excel's formula bar. Note that the last two spaces are gone! It has become " abc 123". No trailing spaces.
- On the right-hand side, right-click the query and choose Edit. That brings up the Query Editor. Verify that the trailing spaces are still there using the method above. When done. click Close & Load.
- After re-loading in the last step, verify that in the worksheet, those two trailing spaces are NOT there, by inspecting it again in the formula bar.
- So the trailing spaces are there in the query, but not in the query result on the worksheet. Now let's see what happens when we DON'T add the query to the Data Model:
- Repeat steps 6-12 above to create another, identical query on the same workbook. Except when you get to the part about checking the "Add this data to the Data Model" checkbox on the Load To dialog, DON'T check it. Leave it unchecked.
- Then repeat step 13 to check the query result on the worksheet to see if the trailing spaces are there. Result: this time the trailing spaces are still there! The only difference is that this time you did NOT check the "Add this data to the Data Model"
checkbox on the Load To dialog. If you check that checkbox, you lose trailing spaces in the query result. If you don't check it, you keep the trailing spaces.