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
It worked for me
Thanks
Hi, Can you please share the snippet of the flow with me. I have a similiar situation and stuck since long
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().
This is what the flow run for select creates:
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']
The result is position 4 (as indexes run 0,1,2,3,4).
Cheers
Damien
Please take a look and subscribe to my YouTube Channel for more Power Platform ideas and concepts. Thanks
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
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 !
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.
Hello,
A way to achieve that is to use a temp collection "CountCollection" :
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)
WarrenBelz
146,645
Most Valuable Professional
RandyHayes
76,287
Super User 2024 Season 1
Pstork1
65,997
Most Valuable Professional