Substring in DAX: How to get Part of String Field in Power BI Using DAX Expression

Substring is one of the most common functions in many languages, However, there is no function named Substring DAX. There is a very simple way of doing it, which I am going to explain in this post. Substring means getting part of a string, for example from “Reza Rad”, if I want to get the start starting from index 2, for 4 characters, it should return “za R”. Considering that the first character is index 0. Let’s see how this is possible.

Sample Data

I have a sample customer table as below;

Substring

Substring means saying from character indexed N, extract M characters: Substring (N,M)

This can be implemented in DAX in different ways, this is one of the methods:

Substring = LEFT(
    RIGHT(
        DimCustomer[EmailAddress],
        LEN(DimCustomer[EmailAddress])-1
        ),
        3)

This added as a column to this table,

will produce characters starting from index 1 for the length of 3.

1, in the expression above, is the starting index. if you want to start from the beginning of the text, use zero here.

3, in the expression above, is the length of the output from the starting index.

here is another example:

There is an easier way to do substring too, using MID function;

MID = MID(DimCustomer[EmailAddress],5,7)

Using the MID function, you just specify the starting index, and the length of characters to extract, similar to substring in many other languages.

Reverse Substring

Sometimes you want substring to start from the end of the text. For example, ReverseSubString (N, M) means to start from N which is the index from the right end of the string and extract M characters. For example, for “Reza Rad”, with ReverseSubstring(3,2), means “Ra”. You can implement the Reverse substring as below:

Reverse Substring = LEFT(
    RIGHT(DimCustomer[FullName],3)
    ,2)

the result is:

This method is usually more useful when the value you want to extract is closer to the end of the string rather than the start.

I hope this little script can help in your DAX expressions, if you have any questions, feel free to write a comment below.

Download Sample Power BI File

Download the sample Power BI report here:

    Enter Your Email to download the file (required)

    Video

    Reza Rad on FacebookReza Rad on LinkedinReza Rad on TwitterReza Rad on Youtube
    Reza Rad
    Trainer, Consultant, Mentor
    Reza Rad is a Microsoft Regional Director, an Author, Trainer, Speaker and Consultant. He has a BSc in Computer engineering; he has more than 20 years’ experience in data analysis, BI, databases, programming, and development mostly on Microsoft technologies. He is a Microsoft Data Platform MVP for 12 continuous years (from 2011 till now) for his dedication in Microsoft BI. Reza is an active blogger and co-founder of RADACAD. Reza is also co-founder and co-organizer of Difinity conference in New Zealand, Power BI Summit, and Data Insight Summit.
    Reza is author of more than 14 books on Microsoft Business Intelligence, most of these books are published under Power BI category. Among these are books such as Power BI DAX Simplified, Pro Power BI Architecture, Power BI from Rookie to Rock Star, Power Query books series, Row-Level Security in Power BI and etc.
    He is an International Speaker in Microsoft Ignite, Microsoft Business Applications Summit, Data Insight Summit, PASS Summit, SQL Saturday and SQL user groups. And He is a Microsoft Certified Trainer.
    Reza’s passion is to help you find the best data solution, he is Data enthusiast.
    His articles on different aspects of technologies, especially on MS BI, can be found on his blog: https://radacad.com/blog.

    Leave a Reply