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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
PowerBINewbie7
New Member

Extracting substrings: FIND throwing error despite being skipped by IF Statement

Hi all!

 

I'm trying to extract a column containing information (in this example: the numbers) from a column which looks something like this:

 

INFORMATION01
INFORMATION02
ABCDEFG Info: 03 HIJKLMNOPQR
I felt like putting something else here. Info: 04 And some more text!
This row contains a typo. Ifo: 05 Lorem ipsum dolor

 

My general idea is to create a DAX statement along the lines of:

 

"IF(CONTAINSSTRING(Table[column], "INFORMATION"), RIGHT(Table[column], 2),
IF(CONTAINSSTRING(Table[column], "Info: "), MID(Table[column], FIND("Info: ", Table[column])+7, 2), "Info not found")

 

Unfortunately, this causes the FIND function to throw an error for the whole column because not all rows contain "Info" and therefore, it could not return anything in those rows even if it wouldn't have to because CONTAINSSTRING would return false. Is there some way around this? (Eventually, I'd like to also account for typos with another IF-Statement)

 

1 ACCEPTED SOLUTION

Hi @PowerBINewbie7 ,

You can update the formula of calculated column as below:

Column = 
VAR _information =
    SEARCH ( "INFORMATION", 'Table'[Text], 1, 0 )
VAR _info =
    SEARCH ( "Info: ", 'Table'[Text], 1, 0 )
VAR _info1 =
    SEARCH ( "Ifo: ", 'Table'[Text], 1, 0 )
RETURN
    SWITCH (
        TRUE (),
        _information > 0, RIGHT ( 'Table'[Text], 2 ),
        _info > 0, MID ( 'Table'[Text], VALUE ( _info ) + 6, 2 ),
        _info1 > 1, MID ( 'Table'[Text], VALUE ( _info1 ) + 5, 2 ),
        "Info not found"
    )

vyiruanmsft_0-1708932953198.pngBest Regards

Community Support Team _ Rena
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

8 REPLIES 8
timalbers
Advocate V
Advocate V

Ok this is strange.. I just simply copied your DAX logic and I am not able to reproduce the error.

 

timalbers_0-1708696444857.png

Yup, my bad! I did not try my example code on this example, because I assumed the error would occur as it does in the table I'm actually working on... Sorry for wasting your time! I'll go back to figuring out what makes the table I'm working on different from my example.

Not a problem at all

If there are still issues, just come back to this thread. 
I will try to help you best as I can

Thanks a lot!

As an update: For some reason, using SEARCH instead of FIND fixed it. I played around with my original table and "FIND"ing some strings worked fine, but not the ones I needed. The only explanation I can think of is that maybe FIND reacts to special characters (specifically: dots) in some way I was unaware of and SEARCH doesn't.

Hi @PowerBINewbie7 ,

You can update the formula of calculated column as below:

Column = 
VAR _information =
    SEARCH ( "INFORMATION", 'Table'[Text], 1, 0 )
VAR _info =
    SEARCH ( "Info: ", 'Table'[Text], 1, 0 )
VAR _info1 =
    SEARCH ( "Ifo: ", 'Table'[Text], 1, 0 )
RETURN
    SWITCH (
        TRUE (),
        _information > 0, RIGHT ( 'Table'[Text], 2 ),
        _info > 0, MID ( 'Table'[Text], VALUE ( _info ) + 6, 2 ),
        _info1 > 1, MID ( 'Table'[Text], VALUE ( _info1 ) + 5, 2 ),
        "Info not found"
    )

vyiruanmsft_0-1708932953198.pngBest Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Glad, you found a way!
Only difference I know: FIND is case sensitive, SEARCH is not.
However, have a nice weekend!

timalbers
Advocate V
Advocate V

Hi @PowerBINewbie7,
could you please share a screenshot of the error message? I cannot reproduce the error.

One thing to mention besides that: If your tabele name really is "Table", you'll have to put it like this: 'Table'[column], because "Table" is a reserved word. Table[column] would return an error.

Hi @timalbers,

The error message is just this: 

PowerBINewbie7_1-1708695724547.png

with the column displaying "#ERROR" in each row.

My table is not actually named table, so that can't be the problem.

I've also tried replacing the MID-FIND part with just "Info found" and that works perfectly. 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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