Forum Discussion
Alisea_MI
Resolver II
5 years agoExtract 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 length...
- 5 years ago
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)
)
AlexisOlson
Super User
5 years agoCONTAINSSTRING 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 )
)Alisea_MI
Resolver II
5 years agoAlexisOlson Unfortunately this gives me an error for the variable. Do you know what problem can be? My column is a text type.
- AlexisOlson5 years ago
Super User
Ah. You have some strings that are shorter than 4 characters.
The MID version of taking the substring is more robust to this issue.