Forum Discussion

kpost's avatar
kpost
Solution Sage
3 years ago
Solved

Text value alphabetically between two other values, resolved as TRUE/FALSE

I have a column with text values such as:

 

16101X

16102X

16103X

16C01A

16C02A
16C03A

etc.

 

I want to add a calculated column that groups these using a switch statement like so:

SWITCH(
             TRUE(),
             [value] >= "16101X" && [value] <= "16103X", "A",
             [value] >= "16C01A" && [value] <= "16C03A", "B",
            "OTHER"
)

However, the greater/less than operator does not work on text, obviously.

Any ideas?  Thanks in advance!  I will give kudos and mark your response as the solution if it works.

  • kpost My take is the opposite of what you said, if you can do it at the backend, it is always advisable to do it there then doing it in Power BI. I'm sure if you have CASE logic in SQL, you can easily translate that into DAX. Not sure if I can be of further help.

4 Replies

  • kpost My take is the opposite of what you said, if you can do it at the backend, it is always advisable to do it there then doing it in Power BI. I'm sure if you have CASE logic in SQL, you can easily translate that into DAX. Not sure if I can be of further help.

    • kpost's avatar
      kpost
      Solution Sage

      I absolutely can do it in SQL, as SQL allows comparing text using comparison operators like <= etc.

      Maybe this is the best solution after all.  Thanks.

  • kpost as you said text will not work in the above case but what is the logic for grouping, maybe that can help to derive the solution.

  • kpost's avatar
    kpost
    Solution Sage

    Unfortunately there is no consistent pattern.

    It is a massive sheet containing 10s of thousands of codes that must be grouped somewhat arbitrarily, but in the vast majority of cases they are being grouped like this, where codes that are alphabetically next to each other are grouped into the same groups.

    I could do it on the back end using SQL (I'm pulling this from a database originally) and just add a column with a bunch of CASE statements without much trouble, but I'd strongly prefer to do it in Power BI.