I'm not sure if this is the right forum to ask this question, but here goes. I create a series of steps for a table in a workbook to transform the data that was in a table. The steps are in the attached image. How can I reuse those steps in various other files, similar to storing a macro in a Personal.XLSB folder to use in any workbook?
One way is to save the query in your
My Data Sources folder which will be somewhere under your user directory. If you use OneDrive, it will be under
C:\Users\USERID\OneDrive\Documents. To do this, from the
Queries and Connections pane in Excel, Right Click on the query and select
Export Connection File....
It will save the file file with a filename
Query - QueryName.odc in your
My Data Sources folder. Now, when you need it in another file, just go to
Data -> Existing Connections, and you'll see a dialog box like this:
The
GDP shortcut above is to a another folder that I keep certain Queries in. It's annoying that they all start with "Query - ", but I tried to stop that and it's just way to much trouble.
When you select a Query and then click
Open, you'll be presented with the standard Load window, however no matter what state the query was in when it was saved, it will ALWAYS want to load as a
Table in the Current Workbook and
Add to the Data Model.
That is if you KNOW you'll want to use the query on a regular basis, and makes getting it a breeze, but it is a bit of a pain, and the list of Queries becomes obnoxious when it has a LOT of queries.
For the one off or maybe you'll use it again situation, it's easier to copy the Query in the
PQ Editor and paste it into the new Workbook. In the source Workbook PQ Editor, Right Click on the query and select Copy
Close the PQ Editor, go to the Workbook you want the Query, select
Data -> Get Data -> Launch Power Query Editor..., and
Right Click in the Queries pane on the left and select
Paste.
NOTE: With BOTH methods, if there are Queries dependent on the selected Query, they will be included in both the ODC query and the Copy/Past query. While at first that sounds like a bad idea, if you have a Query built using Merge as New, the two Queries needed for the Merge will be included.
Of course there's always using the Advanced Editor, Copying the code, then creating a new Blank Query and Pasting the code, but that's so 1990!
Note that the Export/Import method is REALLY USEFUL for Custom Functions. I had a project where I had to parse large Log files (text) that had a LOT of spaces used to align columns, and I needed to get rid of all the extra spaces. Once I had a function that could remove all extra spaces between words, I was able to Export it, and now have it at the ready whenever I need it. In fact the top 2 queries in the screenshot above are to create Data Model Calendars with a LOT of columns, and once imported only the start date (year - it will always start on Jan 1) and the number of years in the future are needed, and even has a place to set up non standard Financial Quarters.
Hope that helps!
p.s. Yeah, this was the right forum!