We have 4GB csv file which is the source for power query in an Excel document. It takes some time to set up all of the transformations, and we would like to be able to reuse the steps when creating other documents which need to import into the data model files of the same format.
Is there a way to save the query and reuse it in another document? I've seen some references to copying the query text from the Advanced Editor, but it seems like there should be a better way of doing it.
I assume, you opened your Excel data file and did all PowerQuery transformations within it. In order to separate them, you could either go for Peter's solution or you make two copies of that file, one for the data (e.g. "data.xlsx") and the other for the transformations (e.g. "PQ_transformations.xlsx"). Either way, you will have to do some adjustments.
= Excel.CurrentWorkbook(){[Name="Table1"]}[Content]
) by = Excel.Workbook(File.Contents("[PATH]\data.xlsx"), null, true)
with [PATH] being a placeholder for your file's location.The following setup assumes that you organize your data files in different folders. You can then copy your PQ file into each of these folders, open it and click on "Data"/"Update all" to apply your transformations to the data file in the given folder.
Notes:
I assume that all data files have the same structure and name.
I define the folder in Excel and not in PowerQuery to allow users that have no knowledge of PQ to manually change the folder by overwriting the formula in case they do not want to copy the file all the time.
Add a tab called "Paths".
Select A1
and enter Current folder
.
Select A2
and enter =MID(CELL("filename"),1,FIND("[",CELL("filename"))-1)
. This formula provides you with the folder of the current file as soon as it has been saved.
Select range A1:A2
and bring it into PQ editor by selecting the "Data" ribbon and choosing "From table/area" out of the "Request and transform data" section.
A new query is generated, showing you the current folder.
Open the "Advanced Editor" ("Start"/"Advanced Editor"), change the name of the second step to "SetTypes" and add the additional lines. The result should look similiar to this:
let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
SetTypes = Table.TransformColumnTypes(Source ,{{"Current folder", type text}}),
GetPathAsValue = SetTypes{0}[Current folder],
ShowFilesInPath = Folder.Files(GetPathAsValue),
FilterForDataFile = Table.SelectRows(ShowFilesInPath, each ([Name] = "Data.xlsx"))
in
FilterForDataFile
= Excel.CurrentWorkbook(){[Name="Table1"]}[Content]
) by = GetFile
.