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
PeterD20
New Member

Create PowerBI Dax Custom Column similar to SQL Case When not in list and wildcards

In PowerBI when creating a custom column how do I reconstruct the SQL case-when statement below similar to a multiple clause if-then-else statement or switch statement. This also contain AND, OR and checking whether strings exist in some list (in this case currency names)

 

Case
When Account = 2 and Currency in ('EUR','GBP') then 'Europe'
When Account = 3 and Currency NOT in ('USD','JPY') then 'Emerging Market'
When Account = 4 then 'Account 4'
When code like '123%' then 1
When code like '%222' then 2
Else Country_Name
End as my_custom_column

 

1 ACCEPTED SOLUTION
v-kongfanf-msft
Community Support
Community Support

Hi @PeterD20 ,

 

My results may not be quite the same as @Daniel29195 ; for values that don't satisfy the condition, the value corresponding to the Country_Name field should be returned.


Refer to the following formula:

my_custom_column =
SWITCH (
    TRUE (),
    'YourTable'[Account] = 2
        && CONTAINSSTRING ( "EUR,GBP", 'YourTable'[Currency] ), "Europe",
    'YourTable'[Account] = 3
        && NOT CONTAINSSTRING ( "USD,JPY", 'YourTable'[Currency] ), "Emerging Market",
    'YourTable'[Account] = 4, "Account 4",
    CONTAINSSTRING ( 'YourTable'[code], "123%" ), 1,
    CONTAINSSTRING ( 'YourTable'[code], "222%" ), 2,
    TRUE (), 'YourTable'[Country_Name]
) & ""

vkongfanfmsft_0-1709792247681.png

 

Best Regards,
Adamk Kong

 

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

2 REPLIES 2
v-kongfanf-msft
Community Support
Community Support

Hi @PeterD20 ,

 

My results may not be quite the same as @Daniel29195 ; for values that don't satisfy the condition, the value corresponding to the Country_Name field should be returned.


Refer to the following formula:

my_custom_column =
SWITCH (
    TRUE (),
    'YourTable'[Account] = 2
        && CONTAINSSTRING ( "EUR,GBP", 'YourTable'[Currency] ), "Europe",
    'YourTable'[Account] = 3
        && NOT CONTAINSSTRING ( "USD,JPY", 'YourTable'[Currency] ), "Emerging Market",
    'YourTable'[Account] = 4, "Account 4",
    CONTAINSSTRING ( 'YourTable'[code], "123%" ), 1,
    CONTAINSSTRING ( 'YourTable'[code], "222%" ), 2,
    TRUE (), 'YourTable'[Country_Name]
) & ""

vkongfanfmsft_0-1709792247681.png

 

Best Regards,
Adamk Kong

 

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

Daniel29195
Super User
Super User

@PeterD20 

 

ouptut

Daniel29195_0-1709757034258.png

 

 

Column = 
SWITCH (
    TRUE (),
    AND ( tbl[account] = 2, OR ( tbl[Currency] = "EUR", tbl[Currency] = "GBP" ) ), "Europe",
    AND ( tbl[account] = 3, NOT ( OR ( tbl[Currency] = "USD", tbl[Currency] = "JPY" ) ) ), "Emerging Market",
    tbl[account] = 4, "Account 4",
    LEFT ( tbl[code], 3 ) = "123", "1",
    RIGHT ( tbl[code], 3 ) = "222", "2", 
    tbl[country_name]
)

 

 

let me know if this helps /

 

 

If my answer helped sort things out for you, i would appreciate a thumbs up 👍 and mark it as the solution
It makes a difference and might help someone else too. Thanks for spreading the good vibes! 🤠:

 

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.