Forum Discussion
Power Query Source from Relative Paths
- Jun 20, 2018
Hi Magnus,
You may find the current file path putting in any cell of your workbook the formula
=LEFT(CELL("filename",$A$1),FIND("[",CELL("filename",$A$1),1)-1)
name this cell somehow, let say "FilePath" and generate absolute path in query like
<prev step> FilePath = Excel.CurrentWorkbook(){[Name="FilePath"]}[Content]{0}[Column1], FullPathToFile1 = FilePath & "Name1.xlsx" <next step>
Hi Magnus,
You may find the current file path putting in any cell of your workbook the formula
=LEFT(CELL("filename",$A$1),FIND("[",CELL("filename",$A$1),1)-1)
name this cell somehow, let say "FilePath" and generate absolute path in query like
<prev step> FilePath = Excel.CurrentWorkbook(){[Name="FilePath"]}[Content]{0}[Column1], FullPathToFile1 = FilePath & "Name1.xlsx" <next step>
- ArA2055Jul 18, 2024Copper Contributor
Hey Sergei,
Could you please help me? My query sheet source is a folder, not a file, so every time a file is copied to the folder, the sheet is supposed to update. However, this only works on my device. I want to make the path relative so it works on any device. I followed similar steps for a single file, with some modifications to make it work for a folder, but it's not working.
In my sample file, my source looks like this in the Advanced Editor:
Power Query
Source = Excel.Workbook(Parameter1, null, true),In the actual file, my query source looks like this:
Power Query
Source = Folder.Files("C:\adfads\adsfa\adfd"),How do I make the relative path work on any device?
Thank you!
- dvsjayMay 07, 2025Copper Contributor
Hey ArA2055,
Not sure if you ever got this answered elsewhere. But in the event someone else ends up here, it's done like this:
In your current excel sheet, copy this formula into any cell to get the current path where this sheet is located:
=LEFT(CELL("filename",$A$1),FIND("[",CELL("filename",$A$1),1)-1)
Name that cell whatever you want (which will change it to a named range) in this e.g. FilePath.
In the query you made with the source Folder.Files, open the advanced editor and drop this code just after the opening of your let formula.
FilePath = Excel.CurrentWorkbook(){[Name="FilePath"]}[Content]{0}[Column1],
Then in the line which says Source, you'll use the previous step (FilePath) as the path. So in your e.g. you should have this
let
FilePath = Excel.CurrentWorkbook(){[Name="FilePath"]}[Content]{0}[Column1],
Source = Folder.Files(FilePath)
in
SourceThis way, it grabs all the files in the same folder as the current excel document.
If you wanted to grab one specific file then it would be:
let
FilePath = Excel.CurrentWorkbook(){[Name="FilePath"]}[Content]{0}[Column1],
Source = Excel.Workbook(File.Contents(FilePath & "Workbook name.xlsx"), null, true)
in
SourceSo essentially you replace "C:\adfads\adsfa\adfd" with FilePath
Hope this helps. Also, if you want to put the files in a subfolder instead, then in the cell you can concatenate the formula with the subfolder e.g.:
=LEFT(CELL("filename",$A$1),FIND("[",CELL("filename",$A$1),1)-1)&"\mysubfolder"
- SergeiBaklanJul 18, 2024Diamond Contributor
If we speak about local drive it's enough to use instead of
Source = Folder.Files("C:\adfads\adsfa\adfd")
parameter which defines the folder
Source = Folder.Files(pFolder)
Entire parameter could be defined / calculated in within Excel grid and queried. Perhaps it could be done relative, but it's not clear relative to what? We may find in which folder this file is located (e.g.=INFO("DIRECTORY"), but it shall be some logic where is the folder related to that file.
- JeffersonValimFeb 02, 2023Copper Contributor
Maybe this vba code helps:
Function FilePath()
Application.Volatile
FilePath = Application.ActiveWorkbook.Path & "\base.xlsx"
End Function- SergeiBaklanFeb 13, 2023Diamond Contributor
Actually the question is how to pass that into Power Query, doesn't matter how filepath was generated.
- ClemlebApr 07, 2021Copper ContributorHello Sergei,
Thank you it's very useful for local files but how to make it work with a one drive folder?
the CELL fomula gives an html path.
Thank you very much- SergeiBaklanApr 08, 2021Diamond Contributor
I guess we speak about the files synced with OneDrive. CELL("filename") returns in this case URL. To query the files you may use
= SharePoint.Files("https://tenant-my.sharepoint.com/personal/account", [ApiVersion = 15])
which returns the list of files with FilePath column. Parse what is returned by CELL() and filter on that above query.
- Hanna_TaskaievaMay 04, 2023Copper ContributorSergeiBaklan, good day. Maybe you will be the right person who can help me. I have tried 2 options to open the list of files in the folder after copying of root folder on One Drive.
On PC in the file I have insert a formula
=LEFT(CELL("filename"),FIND("[",CELL("filename"))-1)&C4
and named this cell "Path_Directions". It returns
E:\work\crossing forecast\Directions
on PC the next code in PQ works well:
folder = Table.SelectRows(Excel.CurrentWorkbook(), each ([Name] = "Path_Directions")){0}[Content]{0}[Column1],
Source = Folder.Files(folder),
But when I put the folder "Work" on One drive, formula of path have given:
https://fcbc1-my.sharepoint.com/personal/account/Documents/work/crossing forecast/Directions
And PQ stopped. So, I have changed the part of the Source code in two ways:
1st option:
folder = Table.SelectRows(Excel.CurrentWorkbook(), each ([Name] = "Path_Directions")){0}[Content]{0}[Column1],
Source = if Text.Start(folder,4) = "http"
then Web.Contents (folder)
else Folder.Contents (folder),
2nd Option:
Source = SharePoint.Files("https://fcbc1-my.sharepoint.com/personal/account", [ApiVersion = 15])
Both call the window: "We couldn't authenticate with the credentials provided. Please try again."
All levels have Anonymous access but it doesn't help. I cannot avoid this. Please help.
- Pastornater2020Feb 10, 2021Copper ContributorI am doing something similar but I want to do a folder.files like action and see all the files in the subfolders of the folder this excel document is in. My files are on one drive and when I do this workaround it gives me errors. Any help would be awesome
- DeletedAug 16, 2018
Hi All,
I have the Power Query add-in on Excel 2013 and Windows 7. I tried the following:
I named cell A1 of the workbook "WBPath" and entered this formula in it:
G:\Ibrahim Omar\Flooring\Sales Analysis\
And with this, the cell A1 now shows the path of the workbook correctly.Then I started my query with the following code:
Let
WBPath = Excel.CurrentWorkbook(){[Name="WBPath"]}[Content]{0}[Column1],
FullPathToFile1 = WBPath & "file.xlsx",
Source = Excel.Workbook(File.Contents(FullPathToFile1), null, true),
<next step>I immediately and without even closing the editor got this error below:
"Token Eof expected."When I clicked show error, it highlighted the first instance of "WBPath" in my code.
So, what is this error? And how can I fix it?
Thanks a lot!- Magnus Vegem DahleAug 17, 2018Brass Contributor
I ahd some of the same problem, for me, the error was the "Let" in the start of the code was with a Capital letter. When I switched to all lowercase letters it worked fine.
So it should maybe be:
let
WBPath = Excel.CurrentWorkbook(){[Name="WBPath"]}[Content]{0}[Column1],
FullPathToFile1 = WBPath & "file.xlsx",
Source = Excel.Workbook(File.Contents(FullPathToFile1), null, true),
<next step>- SergeiBaklanAug 17, 2018Diamond Contributor
Hi Magnus,
Correct, M script (PQ language) is case sensitive. "Let" and "let" are different objects (first one doesn't exist), as well as any other ones.
- SergeiBaklanAug 16, 2018Diamond Contributor
I copy/pasted your code into PQ
let WBPath = Excel.CurrentWorkbook(){[Name="WBPath"]}[Content]{0}[Column1], FullPathToFile1 = WBPath & "file.xlsx", Source = Excel.Workbook(File.Contents(FullPathToFile1), null, true) in Source
There are no errors at all
- Rup231470Jan 09, 2023Copper Contributor
Hi
I am new to power query , and I want to convert my absolute path to relative path,
SergeiBaklan I used the same code and did not get any errors but,
It was giving me the null value as output
I gave cell A1 as the name WBPath and this is the code I am using
let
WBPath = Excel.CurrentWorkbook(){[Name="WBPath"]}[Content]{0}[Column1],
FullPathToFile1 = WBPath & "file.xlsx",
Source = Excel.Workbook(File.Contents(FullPathToFile1), null, true),
- Magnus Vegem DahleJun 21, 2018Brass Contributor
Thanks,
This really helped.
For other beginners like me, this was my end result:
let
FilePath = Excel.CurrentWorkbook(){[Name="FilePath"]}[Content]{0}[Column1],
FullPathToFile1 = FilePath & "Name1.xlsx",
Source = Excel.Workbook(File.Contents(FullPathToFile1), null, true)in
Source