cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Toraloralei
Level: Powered On

How to count # of occurrences of a character inside a string in Flow?

Hi everyone, 

 

I am trying to make a fun what-personality-are-you-quiz by using Forms, SharePoint and Flow and I've run into a problem. 

 

Each question in Forms has four options and every option is equivalent to a letter value in a SharePoint List (e.g. "a", "b", "c" or "d") and after using an ODATA filter to get the right letters depending on answers from Forms and a Compose i get a string with the values:

 

a, a, b, a, c, a, c, d,

 

And noooooow, I would like be able to count how many occurances "a", "b", "c" and "d" has, to then check which letter has the most occurances. I am looking for a function like CountIf or something similar but can't find it in Flow?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: How to count # of occurrences of a character inside a string in Flow?

Hi @Toraloralei 

 

I should have mentioned earlier that you need length - 1. So in your case for a,a,b it is going to split as follows.

 

[
"",
",",
",b"
]
 
Total three array cells.  Try to use
sub(length(split(variables('Text'),'a')), 1)
 
If there are no matches this will return zero.
 

If this reply has answered your question or solved your issue, please mark this question as answered. Answered questions helps users in the future who may have the same issue or question quickly find a resolution via search. If you liked my response, please consider giving it a thumbs up.

 

Thanks

 


Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!



View solution in original post

4 REPLIES 4
Super User
Super User

Re: How to count # of occurrences of a character inside a string in Flow?

Hi @Toraloralei 

 

There is no such count function in Flow expressions. You could try using the split expression and find the count using the length.

 

See below expression.

 

length(split(variables('Text'),'a'))
 

If this reply has answered your question or solved your issue, please mark this question as answered. Answered questions helps users in the future who may have the same issue or question quickly find a resolution via search. If you liked my response, please consider giving it a thumbs up.

 

Thanks



Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!



Toraloralei
Level: Powered On

Re: How to count # of occurrences of a character inside a string in Flow?

Hi @abm,

 

Thank you for your swift response! I tried it but seem to get 1 to many? For example "a,a,b" shows "3". Any clue why? 🙂

 

Thank you!

Super User
Super User

Re: How to count # of occurrences of a character inside a string in Flow?

Hi @Toraloralei 

 

I should have mentioned earlier that you need length - 1. So in your case for a,a,b it is going to split as follows.

 

[
"",
",",
",b"
]
 
Total three array cells.  Try to use
sub(length(split(variables('Text'),'a')), 1)
 
If there are no matches this will return zero.
 

If this reply has answered your question or solved your issue, please mark this question as answered. Answered questions helps users in the future who may have the same issue or question quickly find a resolution via search. If you liked my response, please consider giving it a thumbs up.

 

Thanks

 


Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!



View solution in original post

Toraloralei
Level: Powered On

Re: How to count # of occurrences of a character inside a string in Flow?

Hi again @abm,

 

Thank you - it works like a charm! Well done 🙂 However, I just can seem to understand what exactly the lenght() function is counting? Smiley LOL I have tried breaking it done function by function, but still don't understand how works.

 

Anyhow, thanks again!

Helpful resources

Announcements
thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

firstImage

Incoming: New and improved badges!

We've given our badges an overhaul and also added some brand new ones!

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

sixthImage

Power Platform World Tour

Find out where you can attend!

seventhimage

Webinars & Video Gallery

Watch & learn from the Power Automate Community Video Gallery!

Top Kudoed Authors
Users Online
Currently online: 164 members 5,820 guests
Please welcome our newest community members: