Forum Discussion

rlaughli's avatar
rlaughli
Frequent Visitor
6 years ago
Solved

Multi-Select files within folder

Hey everyone,    Is anyone aware of a way to dynamically control which files within a folder to import in a query? So instead of importing all files from the folder at the same time, the idea would...
  • rlaughli's avatar
    rlaughli
    6 years ago

    Hey @v-lionel-msft ,

    Thank you for the reply and the resource. The link provided was not initially useful for my use case, but an additional link within the comments gave me an idea of how to create a viable solution for this issue. I'm not sure if it's the most elegant way to handle this, but for my purposes, it does the job. Maybe someone else will be able to build on it.

    Here's what I did:

    - Let's say you had a parent folder and three subfolders that relate to the monthly data for 2020. I connected to the parent and then created three separate query parameters (only 3 because the end user would only like to look at any 3 months at a time, and mix and match from there) that each lists the subfolders as their values PLUS a fourth subfolder which I call "Blank" that has no sheet/data inside it.

    - Once the parameters were set, I went to my main query and adjusted the M code in the source step to be something like this:

    Folder.Files("C:\Folder\Folder_1\ParentFolder\"&Parameter) & Folder.Files("C:\Folder\Folder_1\ParentFolder\"&Parameter2) & Folder.Files("C:\Folder\Folder_1\ParentFolder\"&Parameter3)

    - With this in place, I can set the respective parameters to include the files I would like to include in the main query. If I only want 2 of the 3, I set 2 of the parameters to the desired paths and set the third one to the "Blank" subfolder, which is then not included within the output. If I want all 3 files (all/any three months of the desired data), then I adjust the parameters to return what is needed and do not set any of them to "Blank".

    As I said, this isn't the most elegant solution, but it works for what I need right now. If anyone comes up with anything better than this, I'm all ears! Thanks again for the entry.