Skip to main content

Notifications

Community site session details

Community site session details

Session Id : x5BR/jZ8gxvbbNHMzHQOxn
Power Apps - Building Power Apps
Answered

Find Element in Array and get Position ID

Like (0) ShareShare
ReportReport
Posted on 8 Mar 2022 12:53:09 by 63

Hello, community,

 

in Power Automate, I'm looking for a simple way to find a word (or element in general) in my array and get its position ID. 

 

So let's say, I have this array:

 

[

  "A",

  "B",

  "C",

  "D",

  "E"

]

 

I want to search for "C" and get its position ID, so "2". Is there a simple way to achieve that?

 

 

Best regards,

Dog

  • Aolo Profile Picture
    3 on 25 Jan 2024 at 11:11:42
    Re: Find Element in Array and get Position ID

    It worked for me
    Thanks

  • JayaBothra Profile Picture
    2 on 29 Mar 2023 at 08:50:13
    Re: Find Element in Array and get Position ID

    Hi, Can you please share the snippet of the flow with me. I have a similiar situation and stuck since long

  • Verified answer
    DamoBird365 Profile Picture
    8,942 Microsoft Employee on 20 Jul 2022 at 19:58:43
    Re: Find Element in Array and get Position ID

    Hi all,

     

    I saw this problem being referenced on Reddit and thought I could provide you with a solution.  I've started with the array, followed by a select that uses range starting at 0, using length of the array for the number of values, 0,1,2,3,4.  Note that the index for an array starts at 0.  I create a string key based on the position from range outputs('compose')?[item()] and position based on the range item(). 

     

    DamoBird365_0-1658343101981.png

    This is what the flow run for select creates:

    DamoBird365_3-1658343733853.png

     

    Using filter array, I can search for the string value "Pie" based on equalling item()['String'] i.e. the key created in the select. This will return one object in the array and by using first, we can return the position first(body('Filter_array'))?['Position']

     

    DamoBird365_1-1658343317028.png

    The result is position 4 (as indexes run 0,1,2,3,4).  

    DamoBird365_2-1658343460590.png


    Cheers
    Damien

    Please take a look and subscribe to my YouTube Channel for more Power Platform ideas and concepts. Thanks

     

     

  • v-yujincui-msft Profile Picture
    on 17 Mar 2022 at 08:27:18
    Re: Find Element in Array and get Position ID

    Hi @DogWithRedSocks ,


    Glad to see you solved the problem.
    You could mark your answer as a 'solution' to close the case.

     

    In addition, it's easier to get more assistance by posting in the corresponding forum.

    https://powerusers.microsoft.com/t5/Microsoft-Power-Automate/ct-p/MPACommunity

     

     

    Best Regards,
    Charlie Choi

     

  • HenriDesmet59 Profile Picture
    295 on 08 Mar 2022 at 14:02:17
    Re: Find Element in Array and get Position ID

    aaah I'm sorry, didn't saw It was for PowerAutomate ! Yes an apply to each is a good way to get the Index of the Element ! Cool !

  • DogWithRedSocks Profile Picture
    63 on 08 Mar 2022 at 13:59:31
    Re: Find Element in Array and get Position ID

    Hi HenriDesmet59, thank you for your answer, I appreciate your help!

     

    From what I see, you're using your solution in CanvasApps, whereas I need a solution in Power Automate 🙂

     

    However, in the meantime, I found a workaround. I'm using a Apply to Each action on the array output, using two conditions to look for my words, using three Integer variables (StartID, EndID, RunningID), and incrementing the RunningID by 1 for every element in the array. Once I stumble upon the word in the array, I write the value from RunningID to my StartID variable and EndID variable.

  • HenriDesmet59 Profile Picture
    295 on 08 Mar 2022 at 13:51:39
    Re: Find Element in Array and get Position ID

    Hello,

     

    A way to achieve that is to use a temp collection "CountCollection" :

     

    HenriDesmet59_0-1646747150869.png

    ClearCollect(CountCollection;{Count:true;Value: 0});;
    
    ForAll(
     test_col;
     If(
     Last(CountCollection).Count;
     If(
     ThisRecord.Value = "C";
     Collect(CountCollection;{Count: false;Value: Last(CountCollection).Value + 1});
     Collect(CountCollection;{Count: true;Value: Last(CountCollection).Value + 1})
     )
     )
    )
    
    
    //then use Last(CountCollection).Value to get the index 

    We will put in it a column "Count" to true at the start, and a field "Value" to count the iteration. 

    Inside the ForAll, If the record is not the element we need to find, we add an entry in our "CountCollection" with the column "count" to true and "Value" to last Value + 1.

     

    When we find the element, we collect a last entry in "CountCollection" with "count". 

    With the "If condition" at the start of the ForAll, Once the last entry of the "CountCollection" count column is set to false they will be no more entry.

     

    At the end to get the index you can take the value of the last element of the "CountCollection".7

     

    (Sorry I'm french and maybe my english is not that good, and take care to replace ";" by "," in the code) 

     

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,645 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 65,997 Most Valuable Professional

Leaderboard