Case Statement with multiple conditions and excluding null values
I am trying to create a case statement where I have a new column that replaces a value in column1 with a value from column 2 but only if column 2 is not null. If it is null, then the value in column1 needs to remain the same.
Sample table:
I need it to look like this:
So "Account A" is replaced with "name" from column 2 when it is available.
I have tried this but it is not working:
(CASE
when
`Column2`is not null
and `Column1`= 'Account A'
then `Column2`
else `Column1`
End)
What am I doing wrong?
Best Answer
-
If you're only wanting this to process for Account A, I would a nested case statement like this:
(CASE WHEN TRIM(`column1`) = 'Account A' THEN (CASE WHEN TRIM(`column2`) = '' THEN `column1` ELSE `column2` END) ELSE `column1` END)
**Check out my Domo Tips & Tricks Videos
**Make sure to any users posts that helped you.
**Please mark as accepted the ones who solved your issue.2
Answers
-
Try just doing this:
IFNULL(column2,column1)
**Check out my Domo Tips & Tricks Videos
**Make sure to any users posts that helped you.
**Please mark as accepted the ones who solved your issue.0 -
Thank you. That does not seem to give me the correct result because if column 2 is blank, then it is showing blank in the new column.
0 -
Blanks and nulls are not the same, so the ifnull function would not help with this data. I would try this to look for blanks:
(CASE WHEN TRIM(`column2`) = '' THEN `column1` ELSE `column2` END)
The trim function removes any spaces that may be in the data and then you can evaluate it to see if it is blank.
Hope this helps.
**Check out my Domo Tips & Tricks Videos
**Make sure to any users posts that helped you.
**Please mark as accepted the ones who solved your issue.0 -
Thank you! I am getting closer...the first two rows are displaying correctly now, but now Account B is also being replaced with the name field, so how do I prevent that from happening?
0 -
If you're only wanting this to process for Account A, I would a nested case statement like this:
(CASE WHEN TRIM(`column1`) = 'Account A' THEN (CASE WHEN TRIM(`column2`) = '' THEN `column1` ELSE `column2` END) ELSE `column1` END)
**Check out my Domo Tips & Tricks Videos
**Make sure to any users posts that helped you.
**Please mark as accepted the ones who solved your issue.2 -
That worked, thank you!
0
Categories
- All Categories
- 1.8K Product Ideas
- 1.8K Ideas Exchange
- 1.6K Connect
- 1.2K Connectors
- 300 Workbench
- 6 Cloud Amplifier
- 9 Federated
- 2.9K Transform
- 102 SQL DataFlows
- 626 Datasets
- 2.2K Magic ETL
- 3.9K Visualize
- 2.5K Charting
- 755 Beast Mode
- 61 App Studio
- 41 Variables
- 693 Automate
- 178 Apps
- 456 APIs & Domo Developer
- 49 Workflows
- 10 DomoAI
- 38 Predict
- 16 Jupyter Workspaces
- 22 R & Python Tiles
- 398 Distribute
- 115 Domo Everywhere
- 276 Scheduled Reports
- 7 Software Integrations
- 130 Manage
- 127 Governance & Security
- 8 Domo Community Gallery
- 38 Product Releases
- 11 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 110 Community Announcements
- 4.8K Archive