Combine Text Strings in Power BI Using DAX

By:   |   Updated: 2021-10-21   |   Comments (3)   |   Related: > Power BI


Problem

In data analysis, there are several reasons why one would want to combine (join) two or more text strings in a dataset into a single text string. This can be to cleanse the data, or to create a unique column for data modelling purposes, or for other related reasons. This tutorial aims to describe some of the different ways to achieve this in Power BI using DAX.

Solution

Let's take the table below to demonstrate a typical case where you might need to join/combine two or more text strings (columns) in a single column (text string).

For this demo we just need to join the "CalendarMonthName" column and the "CalendarYear" column to get a new column with values such as in the format "Jun 2013".

Sample table to demonstrate joining strings or columns in DAX

There are various ways to approach this while using DAX in Power BI including

  • Using the CONCATENATE function
  • Using the COMBINEVALUES function
  • Using the ampersand (&) operator

The above three approaches are the most common ways to handle this sort of tasks, but we can also handle similar tasks using the DAX CONCATENATEX function, this will be discussed in more detail in another article. I will discuss more about each approach next.

Using the DAX CONCATENATE function

The CONCATENATE function in DAX joins two text strings into a single text string. The DAX syntax for the CONCATENATE function is as shown below.

CONCATENATE(<text1>, <text2>) 

The CONCATENATE function can only accept two arguments as seen in the syntax above. The arguments can be texts, numbers, Boolean as texts or combination of all, as well as using of column reference. Without going into details on the function itself, let's see some examples of how you can use this function to join or combine text strings in DAX.

Using the table dataset provided earlier, lets combine the "CalendarMonthName" column and the "CalendarYear" column to get a new column as seen below.

With_CONCATENATE = CONCATENATE(DemoData[CalendarMonthName], DemoData[CalendarYear]) 

The output in the table is as shown below.

Joined column with CONCATENATE function

But as you can see, there is no space between the "CalendarMonthName" column and the "CalendarYear" column. This is because the CONCATENATE function only allows two arguments. Thus, we could not include a space as an argument in the formula above. The above example used a column reference combination, and in this case, it was not the best approach to use just the CONCATENATE function on its own.

There is a simple way to work around this by using nested CONCATENATE functions as seen in the DAX codes examples below.

With_CONCATENATE_V2 = CONCATENATE(DemoData[CalendarMonthName], CONCATENATE(" ", DemoData[CalendarYear])) 

The new output of the table is as seen below.

Joined column with nested CONCATENATE function 1

You could also write the nested CONCATENATE as seen in the example below to by-pass the two arguments limitation of the CONCATENATE function in DAX on its own.

With_CONCATENATE_V3 = CONCATENATE (
    CONCATENATE (
        DemoData[CalendarMonthName],
        " "
    ),
    DemoData[CalendarYear]
)

There are several scenarios where the CONCATENATE function is used that is different from the example we have seen in this article, but for the purpose of this article I have not described these in more detail. I will look to write another article looking at the CONCATENATE function in DAX in more detail.

Using the DAX COMBINEVALUES function

The COMBINEVALUES function in DAX joins two or more text strings into a single text string. Although, the primary purpose of the COMBINEVALUES function is to support multi-column relationships in Direct Query models, but it is usually commonly used to do joins/combination of text strings or columns in DAX.

The syntax of the COMBINEVALUES function is as seen below.

COMBINEVALUES(<delimiter>, <expression>, <expression>[, <expression>]…) 

The COMBINEVALUES function can accept two or more expressions as arguments unlike the CONCATENATE function. It must also contain a constant delimiter value which serves as the separator used during concatenation.

Using the table dataset provided earlier, lets combine the "CalendarMonthName" column and the "CalendarYear" column to get a new column as seen below.

With_COMBINEVALUES = COMBINEVALUES(" ", DemoData[CalendarMonthName], DemoData[CalendarYear]) 

As can be seen in the DAX code, I have used a space delimiter wrapped in a quote (I could have used any other delimiter too like comma (,) or pipe (|)). In this case, I have only joined two columns, but as seen in the syntax above, we can use more than two columns, making the COMBINEVALUES function a very flexible approach to use.

The output of the table is as seen below.

Joined column with COMBINEVALUES function

Using the ampersand (&) operator

The Ampersand (&) operator in Power BI using DAX is very similar to how it is used in Excel. It is used to join two or more text strings into a single text string. Also, unlike the CONCATENATE function, the Ampersand (&) operator can accept more than two arguments and like the CONCATENATE function it can the arguments can be texts, numbers, Boolean as texts or combination of all, as well as using of column reference.

There is no specific syntax to demonstrate this approach, but we can show examples of how it can be used to join/combine columns to create a single column in DAX. Using the dataset, we have been using in this demo, lets combine the "CalendarMonthName" column and the "CalendarYear" column to get a new column as seen below.

With_Ampersand (&) = DemoData[CalendarMonthName]& " " &DemoData[CalendarYear] 

The Dax formula along with the output table is as seen below. Note that there are three arguments in the DAX formula used in this example – the "CalendarMonthName" column, the space delimiter enclosed in double quotes, and the "CalendarYear" column.

DAX expression showing the Ampersand operator in formula bar
Joined column with Ampersand operator in DAX

In summary, the CONCATENATE and the COMBINEVALUES DAX functions are two powerful DAX functions that can be used in joining or combining text strings in more complex ways than what have been described in this article. To better understand more details about how these DAX functions work and in what scenarios they can be used, a more dedicated article would be written on them separately.

Next Steps
  • Learn more about CONCATENATE DAX function here.
  • Learn more about COMBINEVALUES DAX function here.
  • Checkout this article by EnterpriseDNA to understand more about COMBINEVALUES here.
  • Try this tip out in your own data as business requires.

Learn more about Power BI in this 3 hour training course.


Click here to start the Power BI course






sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Kenneth A. Omorodion Kenneth A. Omorodion is a Business Intelligence Developer with over eight years of experience. He holds both a bachelor’s and master’s degree (Middlesex University in London). Kenneth has the MCSA, Microsoft Data Analyst - Power BI and Azure Fundamentals certifications. Kenneth is a Microsoft Certified Trainer and has delivered corporate training on Power BI, SQL Server, Excel and SSRS.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips


Article Last Updated: 2021-10-21

Comments For This Article




Thursday, March 10, 2022 - 6:59:33 PM - David Back To Top (89870)
Thanks so much Ken!
It works. Bookmarked your writings.

Wednesday, March 9, 2022 - 5:31:41 PM - Aigbe Kenneth Omorodion Back To Top (89866)
Hi David,

I have another article (https://www.mssqltips.com/sqlservertip/7100/power-bi-concatenate-strings-power-query-m-language/) that looks at concatenating values in Power Query. Take a look through and am sure most of your questions would be answered.

Otherwise you can do the following:

1. Create a new column using Custom Column in Power Query
2. Rename the column accordingly
3. Use this M code to create the concatenation (Text.Start([LastName], 4)&"-"&Text.Start([FirstName], 4)&"-"&Date.ToText([BirthDate]))

Note you can modify the M code accordingly by changing the "-" delimeters to what you would prefer. Let me know if that works for you.

Thanks

Wednesday, March 9, 2022 - 2:57:55 PM - David Back To Top (89863)
Hi Ken
Thanks for the tips.
I've been struggling to find a way to create a primary key in Power Query by concatenate/text join the first 4 letter of last name, first 4 of first name, and Date of Birth. I can do it in Excel but PQ is different.
Any help would be appreciated.














get free sql tips
agree to terms