2 Ways to Get a Word Count in Power Automate

Do you need to count the number of words in a text value in Power Automate?

You might need to obtain a word count for a given text. However, there is no action or function available to do this.

The process of getting a word count will rely on counting the spaces between words. When you know this number, you can then add 1 to infer the number of words.

This approach has its limitations, as it doesn’t account for punctuation surrounded by spaces, or multiple spaces. But in most cases, it will give a functional word count in your flows.

This article will explore expression using the length, split, and replace functions to obtain a word count.

String Functions for Word Count

In this section, you will see how to get the word count using space character count as an indicator for the number of words.

Syntax of the Length Function

The length function is used to measure the total number of characters in a given string.

length(<text>)

Where <text> is the string in which you want to count characters.

Syntax of the Replace Function

The replace function is used to replace specific characters in a text string.

replace(<text>, <oldText>, <newText>)

<text> is the string you want to modify, <oldText> is the substring you want to replace, and <newText> is the replacement string.

This will replace all the occurrences of the <oldText> values within the <text>.

Syntax of the Split Function

The split function is used to divide a string into an array of substrings, using a specified delimiter such as a space character.

split(<text>, <delimiter>)

<text> is the string you want to split and <delimiter> is the value you want to split the string based on.

Use Length and Replace Functions to Get Word Count

The expression will follow these basic steps to get the word count based on a space character via the length and replace functions.

  1. Calculate the length of the original string with length(outputs('Compose')).
  2. Use the replace function to remove all spaces from the original string and calculate the length of the modified string.
  3. Subtract the length of the modified string from the length of the original string to get the number of spaces.
  4. Add 1 to the number of spaces to obtain the word count.
add(
	sub(
		length(outputs('Compose')),
		length(
			replace(outputs('Compose'),
			' ',
			''
			)
		)
	),
	1
)

These expressions can be combined into a single expression that returns the word count of the text inside a Compose action as seen above.

This expression is complex and requires using the add and sub functions to calculate the desired result.

The sub function is used to subtract the modified text length from the original text length and the add function is needed to add 1 to the difference.

Use Length and Split Functions to Get Word Count

Alternatively, you can use the length and split functions to count the number of words.

Using the split function with a space character as the delimiter you will return an array where each item in the array is a word from the text.

Then all you need to do is count the items in the array to obtain the word count.

The length function will count characters in a string but it will also count items in an array.

length(split(outputs('Compose'),' '))

The above expression will return the word count in the Compose action.

This a much more elegant solution!

Conclusions

In Power Automate, you can create a word count expression by combining several functions.

One method is to count spaces and add one. This is achieved by replacing all spaces in the text with empty strings. Then, subtract the length of the modified text from the original text’s length and add one to get the word count.

Another approach is to split the text based on the space delimiter and count the resulting array elements.

Have you needed a word count before in your flow? How did you do this? Let me know in the comments!

About the Author

John MacDougall

John MacDougall

John is a Microsoft MVP and freelance consultant and trainer specializing in Excel, Power BI, Power Automate, Power Apps and SharePoint. You can find other interesting articles from John on his blog or YouTube channel.

Related Articles

Comments

0 Comments

Get the Latest Tech Tips

Write For Us

Are you a tech enthusiast with a talent for writing great content? Come write for us!

Follow Us

Follow us on social media to stay up to date with the latest in tech!