When we need to concatenate two strings into one in Power BI, the first option that comes to mind is CONCATENATE or CONCATENATEX. While CONCATENATE works with columnar based structure, CONCATENATEX is an iterator function, needing a table to iterate row by row. But actually, we have a new player in the game since 2017 and it is called COMBINEVALUES. Hence it's a relatively new function, it's not used as much as the others, and that's why I have decided to write a post about it. First let's try to concatenate without using any function, only with the ampersand operator.
And this is the output :
As you see it's pretty simple, there is no room for surprise. Before doing the same thing with CONCATENATE, let's check the syntax first:
CONCATENATE(<text1>, <text2>)
As you see, it accepts only two arguments. To concatenate multiple columns we can either write the function with the ampersand operator (&) again and again like this one :
Or we can create the same formula using nested functions :
All of them have the same output but look very different than each other. Especially the last one is unnecessarily long, also difficult to read and edit later. It's hard to believe you need such a long code to write something that simple. So let's check what does Microsoft says for our new player, which is introduced as COMBINEVALUES.
COMBINEVALUES(<delimiter>, <expression>, <expression>[, <expression>]…)
The first difference, the delimiter is written only once in the beginning, meaning you don't need to repeat it between every expression. I think it is a pretty good improvement. The second is you can concatenate an unlimited number of strings, which saves you writing the same function again and again for the same result. It means, it definitely improves the readability of your code. It can be used in measures as well as in calculated columns, like composite keys, etc. The thing that you should be aware of, it doesn't accept nothing as a delimiter, gives error on that occasion. If you need to concatenate without having a delimiter, this one is not going to work for you, using one of the other options that I mentioned is needed. And here it comes:
I think this one would win the competition of which code looks the most readable But when I used the performance analyzer to test which one is the fastest, the champion was CONCATENATE. And the moral of the story is: There are many ways to do the same thing in DAX. The answer to "Which one is the best ?" depends on the context and performance issues. Before I finish I would like to add that the equivalent of COMBINEVALUES in SQL is CONCAT_WS which is also introduced in 2017. Last but not least, in Excel, you can use CONCATENATE function to join multiple strings up to 255 arguments.
Comments