Search code examples
fileerror-handlingpathpowerbipowerquery

PowerBi - Powerquery: If Folder/File we are loading from a path doesn't exist, change path


I'm loading files from every client folder with a parameter. Some of the folders don't contain the files that are needed to load on powerquery. Because those files may or may not exist.

I tried to use: "try - otherwise" to change the path to a "standard folder" if the files are not found.

Is there any solution? or another way to solve it? Thanks!


Solution

  • The following query would define two sources, one the target and one the default. If the target doesn't return any files, then it'd load the default.

    let
        DefaultSource = Folder.Files("C:\DefaultPath"),
        TargetSource = Folder.Files("C:\TargetPath"),
        FileCounter = Table.RowCount(Source),
        DoSomething = if FileCounter > 0 then TargetSource else DefaultSource
    in
        DoSomething
    

    Let me know if it works