Forum Discussion

jaideepnema's avatar
jaideepnema
Solution Sage
6 years ago
Solved

Validate a String Pattern in a Column using DAX

I have a requirement to find whether a string given in a row contains a certain pattern. I have data shown below  ID String Pattern 1 ABCDE 2 ABBCCDDE 3 ABDE 4 ACDE 5 ABBBCCD...
  • parry2k's avatar
    parry2k
    6 years ago

    jaideepnema ok, here it is, add it as a column, although I would prefer to add this in Power Query because you can create a function for repetitive tasks.

     

    Anyhow, you can replace string with "Yes" and "No"

     

     

    Pattern  = 
    VAR __aPos = SEARCH ( "A", pos[String Pattern], , -1 )
    VAR __ePos = SEARCH ( "E", pos[String Pattern], , -1 )
    VAR __bPos = SEARCH ( "B", pos[String Pattern], , -1 )
    VAR __cPos = SEARCH ( "C", pos[String Pattern], , -1 )
    VAR __dPos = SEARCH ( "D", pos[String Pattern], , -1 )
    RETURN
    SWITCH ( 
        TRUE(),
        __aPos = -1 || __ePos = -1, "A and E not found",   //no value
        __bPos = -1 || __cPos = -1 || __dPos = -1, "BCD not found", //no value
        ( __bPos > __aPos && __bPos < __cPos ) &&
        ( __cPos > __bPos && __cPos < __dPos ) &&
        ( __dPos > __cPos && __dPos < __ePos ), "Found", //yes value
        "BCD Not between A and E or BCD not in the right order" //no value
    )

     

     

    I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!