web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Filter array expressio...
Power Automate
Suggested Answer

Filter array expression for folder depth (SharePoint) not working

(1) ShareShare
ReportReport
Posted on by 18

Hi everyone,

I’m currently working on a Power Automate flow where I retrieve files from SharePoint Folders using “Get files (properties only)” and I’m trying to filter the results based on the folder depth. My folder structure looks like depth_1/depth_2/depth_3/document.pdf and in some cases there are deeper levels like depth_1/depth_2/depth_3/depth_4/document.pdf. My goal is to only process files that are located directly inside depth_3 and exclude all files that are located in any subfolders below that level.

To achieve this, I added a “Filter array” action where I tried to limit the depth of the path by counting the number of “/”. The expression I used was @lessOrEquals(length(split(item()?['{Path}'],'/')),4), but this results in an error saying “Fix invalid expression(s) for the input parameter(s) of operation 'Filter_array'”. Additionally, even when adjusting the expression, I am not getting the expected filtering behavior.

My current flow is structured as seen in the screenshot: I first retrieve all files using “Get files (properties only)”, then I count the items, apply a “Filter array” step where I try to filter by path depth, then I filter out folders by checking if IsFolder is equal to false, and finally I continue processing the remaining files. However, the filtering based on the path does not seem to work at all.

I would like to understand what exactly is wrong with my expression, whether {Path} is the correct field to use in this case, and what the recommended approach is to filter files by a specific folder level in SharePoint. Is counting the number of path segments the right way to go, or is there a more reliable method to ensure that only files directly within a certain folder are included?

Thanks in advance for any help!

Screenshot 2026-06-18 065557.png
Categories:
I have the same question (0)
  • Vish WR Profile Picture
    3,748 on at
     
    Wrong field name. {Path} does not work in expressions. Use item()?['FileRef'] instead.
     
    Wrong number. FileRef is a full server-relative URL like /sites/yoursite/Shared Documents/depth_1/depth_2/depth_3/file.pdf. Splitting by / gives way more than 4 segments. Run the flow once, check a real FileRef value in the run history, count the actual segments, then update the number.
     
    Better approach. Skip the Filter array for this entirely. In the "Get files (properties only)" action, use the Filter Query field directly:
    FileDirRef eq '/sites/yoursite/Shared Documents/depth_1/depth_2/depth_3'
    This returns only files in that exact folder level and excludes anything deeper, before the data even enters your flow.
    Also move your IsFolder filter before the path depth filter, not after.
  • Suggested answer
    11manish Profile Picture
    3,333 on at
    Before changing the logic, inspect the output of Get files (properties only) and confirm the exact value of Path, {Path}, and FileDirRef.
     
    In most SharePoint scenarios, filtering on FileDirRef (exact folder match) is more reliable and easier to maintain than counting / characters in the path.
  • Jonihoni Profile Picture
    18 on at
     
    thanks for your response and suggestions. However, in my case the proposed solution does not work due to the structure of my SharePoint library. My actual path looks like this:
    /Shared Documents/General/Customer_Inquiries/Customer/Request/Doc.pdf
    and in some cases:
    /Shared Documents/General/Customer_Inquiries/Customer/Request/Info_for_Customer/Doc.pdf
    
     

    The issue is that the “Customer” level is not fixed. I have around 30 different customer folders, and each of those contains multiple “Request” folders, which again can contain files and some at least one subfolder "Info_for_Customer". Because of this dynamic structure, I think I cannot use a fixed path in the “Get files (properties only)” filter query (e.g. FileDirRef eq '...'), since that would only target a single branch and ignore all other customers, or am I wrong?

     

    I also tried your approach with FileRef and checking the last segment of the path, but this only tells me whether a file is directly inside some folder  it does not ensure that the file is specifically located at the “Request” level. In my case, I need to identify all files that are exactly one level below each “Request” folder across all customers, regardless of which customer folder they belong to.

     

    So the core problem here is not just the incorrect {Path} field, but the fact that I need to filter based on a relative depth within a dynamic folder structure, not an absolute path or a fixed number of segments.

     

    I have added several screenshots again to provide an overview. In the first screenshot, you can see the first level folder "Customer_Inquiries" along with all its customer-subfolders (my Customers). The second screenshot shows an example of a single "Customer" folder containing each request, and the final screenshot shows the structure of a "Request" folder that includes an additional "Info_for_Customer" subfolder.

    (I have translated all the folder names into English, but the specific folder names themselves do not really matter)

     

    If you have any suggestions on how to reliably detect files that are directly inside the “Anfrage” level across multiple dynamic parent folders, I would really appreciate it.

     

    Thanks again!

     

    Screenshot 2026-06-18 085540.png
    Screenshot 2026-06-18 085623.png
    Screenshot 2026-06-18 085635.png
  • Suggested answer
    Valantis Profile Picture
    6,735 on at
     
    Since Customer_Inquiries is always the same fixed folder name and only Customer/Request vary below it, anchor your depth check there instead of counting absolute segments from root. Use FileDirRef (the file's parent folder path, not FileRef which includes the filename).

    In your Filter array, use this expression:

    @equals(length(split(substring(item()?['FileDirRef'], add(indexOf(item()?['FileDirRef'], 'Customer_Inquiries/'), length('Customer_Inquiries/'))), '/')), 2)
     
    What it does:
     
    finds where "Customer_Inquiries/" sits in the path, takes everything after it (so for a file in Request1 you get "CustomerA/Request1", for one in Info_for_Customer you get "CustomerA/Request1/Info_for_Customer"), splits that by /, and checks the count is exactly 2. Files directly in Request match, anything in Info_for_Customer or deeper doesn't, regardless of which of your 30 Customer folders it's in.

    Keep your IsFolder eq false filter, just run it after this one or combine both conditions in one Filter array with @and(...).
     

     

    Best regards,

    Valantis

     

    ✅ If this helped solve your issue, please Accept as Solution so others can find it quickly.

    ❤️ If it didn’t fully solve it but was still useful, please click “Yes” on “Was this reply helpful?” or leave a Like :).

    🏷️ For follow-ups  @Valantis.

    📝 https://valantisond365.com/

    💼 LinkedIn

    ▶️ YouTube

  • Suggested answer
    Haque Profile Picture
    3,653 on at
    Hi @Hi @Jonihoni,
     
    Use the full expression wrapped in @() like this:
     
    @or(
      equals(length(split(item()?['Path'],'/')), 4),
      equals(length(split(item()?['Path'],'/')), 5)
    )
    
     
    This will include files in depth_3 and one level deeper (depth_4), but exclude files deeper than that.
     
    Note: If 'path' is not the correct property name, pelase try 'FolderPath' or check the exact property name in your flow’s run history.
     
     

    I am sure some clues I tried to give. If these clues help to resolve the issue brought you by here, please don't forget to check the box Does this answer your question? At the same time, I am pretty sure you have liked the response!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Valantis Profile Picture

Valantis 377

#2
11manish Profile Picture

11manish 279

#3
David_MA Profile Picture

David_MA 234 Super User 2026 Season 1

Last 30 days Overall leaderboard