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
asadnaeem
New Member

Extract Text based on conditions

Can someone please help me write this Visual Basic function in DAX, I need to create a new column to extract this text from an exisitng column in the Dataset.

Function GetDescription(TEXT3 As String, CLASSNAME As String)
Dim Description As String

Description = ""

If FindWord(TEXT3, 1) = "*GMP*" Then
If InStr(1, FindWord(TEXT3, 2), "AGR") > 0 Then
If IsNumeric(FindWord(TEXT3, 3)) Then
Description = Mid(TEXT3, InStr(1, TEXT3, FindWord(TEXT3, 3)) + Len(FindWord(TEXT3, 3)), Len(TEXT3) - InStr(1, TEXT3, FindWord(TEXT3, 3)) + Len(FindWord(TEXT3, 3)))

Else
Description = Mid(TEXT3, InStr(1, TEXT3, FindWord(TEXT3, 2)) + Len(FindWord(TEXT3, 2)), Len(TEXT3) - InStr(1, TEXT3, FindWord(TEXT3, 2)) + Len(FindWord(TEXT3, 2)))
End If
ElseIf InStr(1, FindWord(TEXT3, 2), "ESA") > 0 Then
Description = Mid(TEXT3, InStr(1, TEXT3, FindWord(TEXT3, 2)) + Len(FindWord(TEXT3, 2)), Len(TEXT3) - InStr(1, TEXT3, FindWord(TEXT3, 2)) + Len(FindWord(TEXT3, 2)))
Else
Description = Mid(TEXT3, InStr(1, TEXT3, FindWord(TEXT3, 1)) + Len(FindWord(TEXT3, 1)), Len(TEXT3) - InStr(1, TEXT3, FindWord(TEXT3, 1)) + Len(FindWord(TEXT3, 1)))
End If
Else
Description = TEXT3
End If

If InStr(1, Description, " - " & CLASSNAME) > 0 Then
GetDescription = Mid(Description, 1, InStr(1, Description, " - " & CLASSNAME) - 1)
Else
GetDescription = Description
End If
End Function




1 ACCEPTED SOLUTION
v-junyant-msft
Community Support
Community Support

Hi @asadnaeem ,

Based on the Visual Basic function you provided, it appears that the function looks for specific keywords in the TEXT3 string, it handles different string operations depending on the presence of certain content, and outputs either a TEXT3 substring or the entire string.
But without sample data it may be more difficult to correctly modify the Visual Basic function to a DAX, I tried to make the modification, if that DAX is not correct, could you please provide me with your sample data and expected results.

GetDescription = 
VAR TEXT3 = YourTable[YourColumn]
VAR CLASSNAME = YourClassNameColumn
VAR Word1Position = IFERROR(SEARCH("*GMP*", TEXT3), -1)
VAR Word2Segment = MID(TEXT3, Word1Position + 5, LEN(TEXT3))
VAR AgrPosition = IFERROR(SEARCH("AGR", Word2Segment), -1)
VAR Description = 
    SWITCH(
        TRUE(),
        Word1Position > -1 && AgrPosition > -1, MID(TEXT3, AgrPosition, LEN(TEXT3) - AgrPosition),
        // Add more cases as necessary
        TEXT3
    )
VAR ClassNamePos = SEARCH(" - " & CLASSNAME, Description, 1, LEN(Description))  // Handle error if necessary

RETURN
    IF(ClassNamePos > 0, MID(Description, 1, ClassNamePos - 1), Description)

 
Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-junyant-msft
Community Support
Community Support

Hi @asadnaeem ,

Based on the Visual Basic function you provided, it appears that the function looks for specific keywords in the TEXT3 string, it handles different string operations depending on the presence of certain content, and outputs either a TEXT3 substring or the entire string.
But without sample data it may be more difficult to correctly modify the Visual Basic function to a DAX, I tried to make the modification, if that DAX is not correct, could you please provide me with your sample data and expected results.

GetDescription = 
VAR TEXT3 = YourTable[YourColumn]
VAR CLASSNAME = YourClassNameColumn
VAR Word1Position = IFERROR(SEARCH("*GMP*", TEXT3), -1)
VAR Word2Segment = MID(TEXT3, Word1Position + 5, LEN(TEXT3))
VAR AgrPosition = IFERROR(SEARCH("AGR", Word2Segment), -1)
VAR Description = 
    SWITCH(
        TRUE(),
        Word1Position > -1 && AgrPosition > -1, MID(TEXT3, AgrPosition, LEN(TEXT3) - AgrPosition),
        // Add more cases as necessary
        TEXT3
    )
VAR ClassNamePos = SEARCH(" - " & CLASSNAME, Description, 1, LEN(Description))  // Handle error if necessary

RETURN
    IF(ClassNamePos > 0, MID(Description, 1, ClassNamePos - 1), Description)

 
Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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.