Forum Discussion

DebbieE's avatar
DebbieE
Community Champion
6 years ago
Solved

Conditional Column in M using another table as a reference

Basically, If this description column contains anything in a seperate reference table I have e.g.

 

Reference Lookup table

TYE

COP

HAE

NOP 

WEP

 

Then True Else False

 

So basically I dont want to hard code these in, I want them to come from this reference table. Is there any way I can do that with the Conditional Column?

  • DebbieE see attached, there are two tables, "search and replace" table is the one used for dynamic replace, it will search the text in "search" column (regardless of length) and replace the value from "replace" column

     

    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!

    Visit us at https://perytus.com, your one-stop shop for Power BI related projects/training/consultancy.

     

11 Replies

  • DebbieE yes it can be done but the solution depends if the value you want to search is at a fixed location in the text or it can be anywhere in the text. I guess the length of 3 characters is fixed?

    • DebbieE's avatar
      DebbieE
      Community Champion

      Yes, it can be anywhere in the text. Im looking for Specific Values from the reference table. Its a possibility they are all length of 3 but that may not always be the case. i dont want to hard code them because they can change

      • parry2k's avatar
        parry2k
        Super User

        DebbieE ok sounds good. Let me try to put together something, not sure where to start but it will be something interesting to solve, I think it is much easier in DAX. Is that an option?

  • v-lionel-msft's avatar
    v-lionel-msft
    Community Support

    Hi DebbieE ,

     

    Try to use DAX to create a calculated column, you can use IF() or SWITCH() function.

     

    Column = 
    IF(
        table[column] in {"TYE", "COP", "HAE" ,"NOP", "WEP"},
        TRUE(),
        FALSE() 
    )

     

    Or you can do like this.

     

    Best regards,
    Lionel Chen

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

    • DebbieE's avatar
      DebbieE
      Community Champion

      The hardcoded version is exactly what im trying to avoid doing

      • parry2k's avatar
        parry2k
        Super User

        DebbieE see attached, there are two tables, "search and replace" table is the one used for dynamic replace, it will search the text in "search" column (regardless of length) and replace the value from "replace" column

         

        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!

        Visit us at https://perytus.com, your one-stop shop for Power BI related projects/training/consultancy.

         

  • Smauro's avatar
    Smauro
    Solution Sage

    Hi DebbieE 

     

    parry2k's solution is great if you also want to do some replacements.

    If you'd only like a true/false output, then you could add a function on your code directly and then call it while adding a new column. It will return true as soon as it finds a value from the lookup or false if it goes through all of them and finds nothing:

        fnSearch =
            let search = (t as text, l as list, x as logical) =>
                if List.Count(l) > 0 and x = false then
                    @search(t, List.Skip(l, 1), Text.Contains(t, l{0}, Comparer.OrdinalIgnoreCase))
                else x
            in search,
        col = Table.AddColumn(PreviousStep, "search", each fnSearch([data], #"ref table"[Column], false), type logical)

    Where [data] is the column you want to search on and #"ref table"[Column] is your reference lookup table's column containing the values you wish to search dynamically.

     

    It is not case sensitive: if you want case sestitivity you should remove the Comparer.OrdinalIgnoreCase.

     

    Cheers