power-automate

How do I filter an array of strings using substrings within another array as the filtering criteria in Power Automate?


In Power Automate, when using Filter Array, I have been capable of successfully filtering an array of strings (filenames) to remove the currently selected/created filename from the list, while taking the substrings of current filename, and producing another filtered array of said substrings.

What I'm hoping to do next, is take the main, larger array of filename strings, and filter it so that an array of filenames containing similar substrings is leftover.

Using the strings below as an example - I'm certain this is possible, but I'm hitting a wall.

Array of Strings:
{
  "con2s19_2501_v1.5.1_model1_custname.zip",
  "con2s19_2412_v1.5.1.0_model1_custname.zip",
  "con2s19_2412_v1.5.1_model2_othercustname.zip",
  "con2s19_2412_v1.5.1_model1_custname_V1.zip",
  "con2s19_2501_v1.5.1_model1_custname_v2.zip"
}

___________________________

New Filename to be parsed:
con2s19_2501_v1.5.1_model1_custname_v3.zip

Array of substrings from New Filename (which often change, and thus must remain dynamic):
{
  "con2s19",
  "v1.5.1",
  "model1",
  "custname"
}

How on earth do I filter the first array of strings using only the Filter Array action without using an Apply to Each action? Below are some screenshots from my flow:

Filtered Array to produce the array of strings from current filename

Body output:

Example Body of first filter (slightly redacted)

Filtered Array to produce the array of FileNames from list of files

Body output:

Example Body of first filter

Below - "From" is the array of existing filenames, the Query's left side is the Array of the Current FileName substrings, and the right side is the function statement item(). This produces no body. Final Filter to leave only filenames that contain the strings from current filename


Solution

  • You can leverage the intersection function.

    Similar to:

    // Left expression:
    length(
      intersection(
        split( replace( toLower( item() ), '.zip', ''), '_'),
        outputs('aSubstrings')
      )
    )
    
    // right expression
    length(outputs('aSubstrings'))
    

    The above will filter only those that have all the parts matching. Otherwise simply replace the right expression with the number you want to minimally match is greater than or equal 1.

    Example for all parts matching:

    Result