Comparers, Combiners, Replacers and Splitters in Power Query

At the end of the Power Query Formula Library Specification (which can be downloaded here) are sections on Comparer, Combiner, Replacer and Splitter functions. These functions are most often used in conjunction with other functions like Table.CombineColumns() and Table.SplitColumn, but what you may not realise from the documentation (which also has a few minor but nonetheless confusing bugs in it) is what these functions do: they are functions that return functions, and the functions that they return can be used independently just like any other function.

Take Splitter.SplitTextByDelimiter() as an example. It returns a function that splits a piece of text by a delimiter, and returns a list containing the resulting pieces. The following M code calls this function to return a function that splits comma delimited text:

let

    demo = Splitter.SplitTextByDelimiter(",")

in

    demo

 

As noted here, once you have a query that returns a function you can see that function’s signature and invoke it from the Query Editor window. Here’s what the query above shows in the Query Editor window:

image

If you click the Invoke button and enter the text

one,two,three,four

As follows:

image

So that the code for the query becomes:

let

    demo = Splitter.SplitTextByDelimiter(","),

    Invokeddemo = demo("one,two,three,four")

in

    Invokeddemo

What is returned is the list {“one”, “two”, “three”, “four”} which looks like this in the Query Editor window:

image

There are various other Splitter functions that can be used to return functions that split text in different ways. Similarly, the Combiner functions return functions that can be used to combine a list of text into a single piece of text. For example:

let

    demo = Combiner.CombineTextByDelimiter("--"),

    Invokeddemo = demo({"one","two","three","four"})

in

    Invokeddemo

Returns the text

one–two–three—four

The Replacer functions return functions for replacing values in text , while the Comparer functions return functions that can be used for comparing text using specific cultures and case sensitivities.

10 thoughts on “Comparers, Combiners, Replacers and Splitters in Power Query

  1. Hi Chris. You mentioned a few confusing bugs in the documentation Comparer, Combiner, Replacer and Splitter functions. What issues are you seeing?

    Carla Sabotta
    SQL & BI Documentation

    • OK, I’ve looked at this again and the main problem (as seen in the May 2014 version of the library spec pdf) is that a lot of the function signatures are wrong in the section on Combiners and Splitters. For example Combiner.CombineTextByDelimiter has the following incorrect signature in the docs:
      Combiner.CombineTextByDelimiter(delimiters as list, optional quoteStyle as nullable number) as
      function
      In actual fact the first parameter is text, not a list.

      Also, Splitter.SplitTextByEachDelimiter has the signature
      Splitter.SplitTextByEachDelimiter(delimiters as list, optional quoteStyle as nullable number) as
      function
      …which is missing the optional third parameter.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s