Keeping the file path whatever the current workbook file path is

tmacg7

New member
Joined
May 2, 2016
Messages
9
Reaction score
0
Points
0
Excel Version(s)
Office 365 / V1803
Getting really frustrated with myself for not being able to figure this out...

I have a PowerQuery to the Current Workbook (querying the same workbook the query is set up in) and I want to be able to move the file to a different folder or rename the file without the query breaking. There has got to be a simple way to do this... I'm new to PowerQuery and don't really know what I'm doing or how to use the advanced editor. I'm reading all Ken's posts about parameters tables, and I just got the Data Monkey book and am skipping to chapter 23 about dynamic parameters tables, but I don't get it (I know I should read the whole book first, but I just need to figure this one thing out as soon as possible). All the examples I'm seeing are trying to do more than I want to do -- I don't need to load "all the files from a folder" -- I just need to query a worksheet in the same workbook and not have it break when I move or rename the file.

I can see that if I choose to query a proper Excel table within the current workbook (by choosing "From Table"), I can then rename the file or move it to a different folder and the query automatically updates and works. But if I'm querying a whole worksheet (not table format) in the current workbook, I have to use the "From File" method instead of the "From Table" method, and I have to navigate to the file itself to set up the file path. Then if I change the file name or move the file, the query breaks and says it can't fine the file.

I see that when I go to the advanced editor and look at the code for a query that was set up "From Table", it has a "Excel.CurrentWorkbook" in the source. But when I look at the source for a "From File" query, it has the actual file path:
From Table:
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
From File:
Source = Excel.Workbook(File.Contents("C:\Users\Smith\Desktop\Folder 2\File2a.xlsx"), null, true),​

So isn't there a way to replace the file path wording with the "CurrentWorkbook" method? Or something that works like that?

I'm using Excel 2013, if that's important.

Thank you!
 
OK, it looks like I've found the solution... 10 points for "M is for (DATA) Monkey", -10 points for me for trying to skip straight to chapter 23 as soon as I got it today instead of reading chapter 1 first. :nono:

The only way I know how to explain it would be basically plagiarizing Ken's book, which I won't do. I'll just say it's in the section about connecting to named ranges on pages 16-18. Yes, I had to read a whopping 18 pages to find the answer. No need for the parameters tables in Ch 23 for this. It still seems like it would be a good enhancement to PowerQuery/Excel to make this simpler and not require a named range and the associated steps -- just a connection option that is basically "a worksheet in this workbook".

I forgot to mention that what I also wanted to accomplish with this solution was not having to save the file every time before refreshing the queries. I was having to do that when I queried on the workbook itself by setting up the query with the "From File" method. In testing the solution below, I can change the file name and/or location, and I can make changes and refresh without saving the file first. Hallelujah!! :biggrin1: Big thanks to Ken for what's already a great book by the end of chapter 1!
 
tmacg7 said:
...10 points for "M is for (DATA) Monkey", -10 points for me for trying to skip straight to chapter 23 as soon as I got it today instead of reading chapter 1 first. :nono:
LOL! So if it makes you feel any better, that's EXACTLY what I would have done if I hadn't written it. ;)

tmacg7 said:
Yes, I had to read a whopping 18 pages to find the answer.
That is seriously one of the best testimonials I could ask for. Thanks for this!

tmacg7 said:
It still seems like it would be a good enhancement to PowerQuery/Excel to make this simpler and not require a named range and the associated steps -- just a connection option that is basically "a worksheet in this workbook".
Agreed. Chapter 5 works through this is a bit more detail, but the overall mileage on this topic is a bit underwhelming. There is no "Worksheet" object for the active workbook in Power Query. You can get "Worksheet" objects from a different file, and you can even point that to the workbook you're in, but that is deadly dangerous as it reads from the saved copy, not the active copy.

tmacg7 said:
Big thanks to Ken for what's already a great book by the end of chapter 1!
Awesome, glad you're enjoying it!
 
Back
Top