Search code examples
powerquerym

Is there a simple way to count occurences of one text string within another text string?


Is there a simple way, using PowerQuery, to count the occurences of a text string within another text string?

For instance, if I have the string, "The quick brown fox jumps over the lazy dog and the lazy dog doesn't notice," how would I easily determine that the words lazy and dog occur twice?

I know I can use Text.Contains to determine whether lazy and dog occur within the string, but I don't know a simple way to determine how many times they occur.


Solution

  • You can split the text, using the search word as delimiter. The number of list items minus 1, is the number of occurrences.

    let
        String = "The quick brown fox jumps over the lazy dog and the lazy dog doesn't notice,",
        Count = List.Count(Text.Split(String,"dog"))-1
    in
        Count