Forum Discussion

Magnus Vegem Dahle's avatar
Magnus Vegem Dahle
Brass Contributor
Jun 20, 2018
Solved

Power Query Source from Relative Paths

Hi All,

 

I am making an analysis tool that uses two Excel file sources via Power Query. The tool work well, but as soon as the file and source files are moved to another location it stops working as the Queries sources from aboslute paths.

 

Can I modify my Sourcing so that as long as the Analysis tool file and the source files are in the same folder the Query will find and use the correct files? So that the Query sources from a path relative to the Analysis Tool file.

 

Thanks!

Magnus

  • 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>

     

  • saadiriaz's avatar
    saadiriaz
    Copper Contributor

    Hi All

     

    My source doesn't end with null and true. Rather it looks like the below.

     

    Source = Csv.Document(File.Contents("E:\PQ\testing\home.txt"),[Delimiter="	", Columns=36, Encoding=1252, QuoteStyle=QuoteStyle.None]),

     

    When I do all the steps mentioned above, I get an error "4 arguments were passed to a function which expects between 1 and 3."

     

    Would appreciate the help.

     

    Thanks!

     

  • PeterHam's avatar
    PeterHam
    Copper Contributor

    Hi Sergei.

    Thx this works for me aswell...

    An amendment would be to get files from a relative folder location instead of a specific file.

     

    This is how I did this

     

    let
    WBPath = Excel.CurrentWorkbook(){[Name="WBPath"]}[Content]{0}[Column1],
    FullPathToFile1 = WBPath & "subfolder\subfolder2\",
    Source = Folder.Files(FullPathToFile1),

  • 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>

     

    • ArA2055's avatar
      ArA2055
      Copper Contributor

      SergeiBaklan 

       

      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!

       

      • ArA2055 

        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.

    • JeffersonValim's avatar
      JeffersonValim
      Copper Contributor

      SergeiBaklan 

       

      Maybe this vba code helps:

       


      Function FilePath()
      Application.Volatile

      FilePath = Application.ActiveWorkbook.Path & "\base.xlsx"

      End Function

    • Clemleb's avatar
      Clemleb
      Copper Contributor
      Hello 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
      • Clemleb 

        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.

Resources