Folder.Contents
Function to load only main folder files, effectively excluding subfolders.When managing data in Excel, especially from shared sources like SharePoint, it’s crucial to streamline your data queries for efficiency and clarity. One common requirement is to fetch data exclusively from the main folder, excluding any files residing in subfolders. This ensures that your dataset remains focused and manageable, preventing potential confusion or data redundancy.
Begin by loading the folder data into Power Query. This forms the foundation of your data manipulation process.
Navigate to the Data Tab: Open your Excel workbook and go to the Data tab on the ribbon.
Get Data from Folder: Click on Get Data > From File > From Folder.
Select the Main Folder: Browse to the main folder path, such as /sites/controlling/Shared%20Documents/Kniha%20j%C3%ADzd
, and click OK.
Open Power Query Editor: Once the data is loaded, the Power Query Editor will launch, displaying a table of all files and subfolders within the selected directory.
After loading the data, the next step is to filter out any files that reside within subfolders.
Identify the Folder Path Column: In the Power Query Editor, locate the column named Folder Path. This column contains the full path for each file, including those in subdirectories.
Apply a Path Filter: Click on the filter dropdown in the Folder Path column. Deselect any rows that contain additional folder names beyond the main folder. This can be done by identifying paths that include slashes ("/") beyond the primary directory.
Alternative Filtering Method: If the filter dropdown method is insufficient, add a custom column to facilitate more precise filtering:
Text.Contains([Folder Path], "subfolder_name")
to identify subfolders. Replace "subfolder_name"
with the actual subfolder name or pattern relevant to your dataset.TRUE
, effectively removing any files from subdirectories.For a more robust solution, especially when dealing with dynamic folder structures, modifying the M code directly can offer greater control.
Access the Advanced Editor: In the Power Query Editor, navigate to the Home tab and click on Advanced Editor.
Understand the Default Function: Typically, the default M code uses Folder.Files
, which retrieves all files from the specified folder and its subfolders.
Replace with Folder.Contents
Function: Modify the M code by replacing Folder.Files
with Folder.Contents
. This function lists only the items (both files and folders) directly within the specified directory, excluding any nested subfolders.
let
Source = Folder.Contents("https://maproczech.sharepoint.com/sites/controlling/Shared%20Documents/Kniha%20j%C3%ADzd"),
#"Filtered Rows" = Table.SelectRows(Source, each ([Extension] <> null))
in
#"Filtered Rows"
In this example, the Folder.Contents
function replaces Folder.Files
, and a subsequent filter is applied to select only files by ensuring the Extension
column is not null.
Even after using Folder.Contents
, there might be cases where folders themselves appear in the query results. To ensure that only files are displayed:
Filter by File Type: Add a filter to display only rows where the Kind column equals File
.
Remove Folders: Alternatively, you can filter out any rows where the Extension column is null, as folders typically do not have extensions.
After applying all necessary filters and ensuring that only main folder files are present:
Close & Load: Click on Close & Load in the Power Query Editor to import the filtered data into your Excel workbook.
Verify the Data: Ensure that the imported data only contains files from the main folder and excludes any subfolder contents.
Power Query offers two primary functions for accessing folder data:
Function | Description | Use Case |
---|---|---|
Folder.Files |
Retrieves all files from the specified folder and its subfolders. | When you need a comprehensive list of all files within a directory hierarchy. |
Folder.Contents |
Lists only the items (files and folders) directly within the specified folder, excluding any nested subfolders' contents. | When you intend to work exclusively with files in the main folder without delving into subdirectories. |
Choosing the appropriate function is pivotal in controlling the scope of your data query. For scenarios where subfolder data should be excluded, Folder.Contents
is the preferred choice.
Beyond the basic filtering methods, custom filtering allows for more granular control over your data query:
Text.Contains
or Text.StartsWith
to identify and exclude unwanted file paths.Let’s walk through an example workflow to solidify the concepts discussed:
You have a SharePoint folder located at /sites/controlling/Shared%20Documents/Kniha%20j%C3%ADzd
containing various files and multiple subfolders. Your objective is to load only the files from the main folder into Excel using Power Query.
Navigate to the Data tab and select Get Data > From File > From Folder.
Enter the main folder path: /sites/controlling/Shared%20Documents/Kniha%20j%C3%ADzd
and click OK.
In the Power Query Editor, click on Advanced Editor and replace Folder.Files
with Folder.Contents
as follows:
let
Source = Folder.Contents("https://maproczech.sharepoint.com/sites/controlling/Shared%20Documents/Kniha%20j%C3%ADzd"),
#"Filtered Files" = Table.SelectRows(Source, each ([Extension] <> null))
in
#"Filtered Files"
This modification ensures that only items directly within the main folder are listed.
With the data loaded, apply a filter on the Extension column to exclude any rows where the extension is null. This typically removes folder entries.
Ensure that the filtered data now displays only files from the main folder.
After verifying the filtered data, click Close & Load to import the data into your Excel worksheet.
Your Excel workbook now contains a list of files solely from the main folder, devoid of any subfolder contents.
For users dealing with multiple folders or frequently changing directory structures, dynamic handling of folder paths can enhance the flexibility of your data queries.
Ensuring the integrity of your data queries involves implementing robust error handling and validation mechanisms.
Large datasets can slow down your Excel workbook. Optimize your Power Query steps to enhance performance:
If after applying the above steps, files from subfolders still appear in your query results:
Folder.Files
has been successfully replaced with Folder.Contents
in the Advanced Editor.Accessing folders on SharePoint may sometimes be restricted due to permissions:
Errors in the M code can prevent successful data loading:
Effectively managing data queries in Excel, particularly when interfacing with SharePoint folders, is essential for maintaining streamlined and focused datasets. By leveraging Power Query's Folder.Contents
function, modifying M code, and applying precise filtering techniques, users can effortlessly exclude subfolders from their data queries. This not only enhances data clarity but also optimizes performance, ensuring that your Excel workbooks remain efficient and easy to navigate.
Implementing these strategies requires a combination of understanding Power Query functions, meticulous filtering, and proactive troubleshooting. With these tools at your disposal, managing large and complex folder structures becomes a manageable task, empowering you to harness the full potential of Excel for your data analysis needs.