Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Alisea_MI
Resolver II
Resolver II

Extract string with MID without specifying the number of characters

Hi,

 

I am new to Dax and I need to search for a text strings starting from the fourth character to the end of the string and if the text is there, return some other text. However, my string lengths differ, and when I am trying to use the MID function, I can't see how I can specify the number of characters to return since I don't know exactly where the string my text is. 

 

What I need to do is:

If string contains "ST" starting from the fourth character and to the end of the string, return "ST",

If string contains "LL", starting from the fourth character and to the end of the string, return "LL",

If string contains "X". staring from the forth character and to the end of the sring, return the last two characters of the string.

 

The expected outcome:

"SSDF RETEST Case NO.234" returns "ST"

"WF3 Stems" returns "ST",

"SW TF 6 X" returns "X"

 

Would really appreciate any help!

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@Alisea_MI , Try a new column like

 

New column =
var _1 = mid([column],4 , len([column])
return
Switch(true(),
search("ST",_1,,0)>0, "ST",
search("LL",_1,,0)>0, "ST",
search("X",_1,,0)>0, right([column],2)
)

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

4 REPLIES 4
AlexisOlson
Super User
Super User

CONTAINSSTRING is useful here.

 

Column =
VAR Substring =
    RIGHT ( Table1[Column1], LEN ( Table1[Column1] ) - 4 )
RETURN
    SWITCH (
        TRUE (),
        CONTAINSSTRING ( Substring, "ST" ), "ST",
        CONTAINSSTRING ( Substring, "LL" ), "LL",
        CONTAINSSTRING ( Substring, "X" ), RIGHT ( Substring, 2 )
    )

@AlexisOlson Unfortunately this gives me an error for the variable. Do you know what problem can be? My column is a text type. 

Screenshot 2021-04-15 223806.jpg

Ah. You have some strings that are shorter than 4 characters.

 

The MID version of taking the substring is more robust to this issue.

amitchandak
Super User
Super User

@Alisea_MI , Try a new column like

 

New column =
var _1 = mid([column],4 , len([column])
return
Switch(true(),
search("ST",_1,,0)>0, "ST",
search("LL",_1,,0)>0, "ST",
search("X",_1,,0)>0, right([column],2)
)

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.