Case When
Assign values to a column based on conditional logic.
Parameters
| Name | Description | Accepted Values | Default | Required |
|---|---|---|---|---|
| I/O | ||||
output | Name of the output column. | string | — | Yes |
| Options | ||||
cases | List of conditions and corresponding values. | array | — | Yes |
default | Value to assign if no conditions are met. Default None. | string, number, integer, boolean, null | null | No |
| Conditions | ||||
if | Condition that determines whether the wrangle runs as a whole. Recipe variables may be referenced with ${variable}. | string | — | No |
where | Filter rows before applying the wrangle using SQL-like criteria, such as column1 = 123 OR column2 = 'abc'. | string | — | No |
where_params | Values used with where for parameterized criteria. Uses SQLite placeholder syntax such as ? or :name. | array, object | — | No |
Examples
wrangles:
- compute.case_when:
output: Letter Grade
cases:
- condition: Grade > 89
value: 'A'
- condition: 90 > Grade > 79
value: 'B'
- condition: 80 > Grade > 69
value: 'C'
- condition: 70 > Grade
value: 'F'
| Student | Grade |
|---|---|
| Billy | 62 |
| Sarah | 91 |
| Timmy | 88 |
| Tammy | 74 |
| Letter Grade |
|---|
| F |
| A |
| B |
| C |
Conditions can be combined so multiple criteria must be met before assigning a value.
wrangles:
- compute.case_when:
output: Letter Grade
cases:
- condition: (Grade > .89) & (Attendance == 'Good')
value: 'A'
- condition: (.90 > Grade > .79) & (Attendance == 'Good')
value: 'B'
- condition: (.80 > Grade > .69) & (Attendance == 'Good')
value: 'C'
- condition: (.70 > Grade) or (Attendance == 'Poor')
value: 'F'
| Student | Grade | Attendance |
|---|---|---|
| Billy | 62 | Poor |
| Sarah | 91 | Poor |
| Timmy | 88 | Good |
| Tammy | 74 | Good |
| Letter Grade |
|---|
| F |
| F |
| B |
| C |
Access
| Requirement | Value |
|---|---|
| AI-powered | No |
| Requires WrangleWorks account | No |
| Requires subscription | No |
| Requires external API key | No |
Technical details
| Field | Value |
|---|---|
| Catalog ID | 3 |
| Catalog key | compute.case_when |
| Recipe key | compute.case_when |
| Catalog status | active |
| Lifecycle status | active |
| Recipe Writer eligible | Yes |
| Namespace | compute |
| Documentation group | compute |
| Aliases | None |
| Runtime symbol | wrangles.recipe_wrangles.compute.case_when |
| Legacy UUID | 9a9662e4-53d1-4932-8adf-bc3e7aa364ad |
Sources