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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
zaforir2002
Helper I
Helper I

Calculated Column

Is there any other easy way to write the following DAX expression...

 

EthnicitySummarised:=
IF (
  FIND ( "Asian", [Ethnicity], 1, BLANK () ),
  "Asian",
  IF (
    FIND ( "Black", [Ethnicity], 1, BLANK () ),
    "Black",
    IF (
      FIND ( "White", [Ethnicity], 1, BLANK () ),
      "White",
      IF (
        FIND ( "Unknown", [Ethnicity], 1, BLANK () ),
        "Unknown",
        IF ( FIND ( "Chinese", [Ethnicity], 1, BLANK () ), "Chinese", "Other" )
      )
    )
  )
)

BI Analyst and Developer @ Queen Mary University of London, UK.
1 ACCEPTED SOLUTION
dataX
Frequent Visitor

@zaforir2002, I think you can make it more clean by using Switch and Search. Search is also case insensitive and I think is a better approach.

 

Ethnicitycol= switch(
TRUE(),
SEARCH("ASIAN",'Sheet3'[ETHNICITY],1,blank())>0,"ASIAN",
SEARCH("LATINO",'Sheet3'[ETHNICITY],1,blank())>0, "LATINO",
"UNKN0WN"

)

View solution in original post

11 REPLIES 11
dataX
Frequent Visitor

@zaforir2002, I think you can make it more clean by using Switch and Search. Search is also case insensitive and I think is a better approach.

 

Ethnicitycol= switch(
TRUE(),
SEARCH("ASIAN",'Sheet3'[ETHNICITY],1,blank())>0,"ASIAN",
SEARCH("LATINO",'Sheet3'[ETHNICITY],1,blank())>0, "LATINO",
"UNKN0WN"

)

Anonymous
Not applicable

Hi @zaforir2002, it looks like you are trying to return the [Ethnicity] value if it's part of a certain subset of values, and classify anything outside that subsite as "Other". If that's accurate, then you could use the IN operator:

 

EthnicitySummarised = IF([Ethnicity] IN {"Asian","Black","White","Unknown","Chinese"}, [Ethnicity], "Other")

 

Hope that helps! 🙂

Hi JaredK, thanks for your reply, but my situation is bit different.... I'm finding the specific words out of a long sentence to construct the new calculated column....
BI Analyst and Developer @ Queen Mary University of London, UK.

I'm wondering the FIND function is being used because he is looking for the existance of that text within a longer string.  I think the IN function needs an exact match. Just need @zaforir2002 to confirm.


To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

Phil_Searmark is right... I'm finding those specific words out of a long string... my existing DAX expression is working... but thought if is there any other way to write that expression as it it bit long and massy.... 🙂
BI Analyst and Developer @ Queen Mary University of London, UK.

Did you tried adding conditional column? In that case  you don't need to write DAX and it is much cleaner and not messy, as you mentioned.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Other though, why not add conditional column rather than writing DAX? 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Hi parry2k, the tabular model I'm working on has many partitions, thereforeign if I add an extra column to one partition then I need to do that for other partitions too, which I don't want...
BI Analyst and Developer @ Queen Mary University of London, UK.

When I said add conditional column, i mean to add in Power BI using "Edit Queries". 

 

- Click Edit Queries

- Goto Add Columns Tab

- There is option called "Conditional Column" and this is what I'm talking about.

 

Thanks,

P

 

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Sorry parry2K, bit of confusion here... although I'm in a power BI community... I'm not using the power BI yet... this is for the existing solution that the organisation has where I'm working... they are planing to use power BI within next couple of months... until then they asked me to add this additional column to their existing tabular model... hence I'm here... although my written DAX expression working as it men to... but I thought if is there any other way to write it then I would...
BI Analyst and Developer @ Queen Mary University of London, UK.

Aha , that make sense, thanks for clarifying. I believe in that case your expression seems to be the way to go.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.