Skip to main content

Case When

Assign values to a column based on conditional logic.

Parameters​

NameDescriptionAccepted ValuesDefaultRequired
I/O
outputName of the output column.string—Yes
Options
casesList of conditions and corresponding values.array—Yes
defaultValue to assign if no conditions are met. Default None.string, number, integer, boolean, nullnullNo
Conditions
ifCondition that determines whether the wrangle runs as a whole. Recipe variables may be referenced with ${variable}.string—No
whereFilter rows before applying the wrangle using SQL-like criteria, such as column1 = 123 OR column2 = 'abc'.string—No
where_paramsValues 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'
StudentGrade
Billy62
Sarah91
Timmy88
Tammy74
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'
StudentGradeAttendance
Billy62Poor
Sarah91Poor
Timmy88Good
Tammy74Good
Letter Grade
F
F
B
C
Access
RequirementValue
AI-poweredNo
Requires WrangleWorks accountNo
Requires subscriptionNo
Requires external API keyNo
Technical details
FieldValue
Catalog ID3
Catalog keycompute.case_when
Recipe keycompute.case_when
Catalog statusactive
Lifecycle statusactive
Recipe Writer eligibleYes
Namespacecompute
Documentation groupcompute
AliasesNone
Runtime symbolwrangles.recipe_wrangles.compute.case_when
Legacy UUID9a9662e4-53d1-4932-8adf-bc3e7aa364ad

Sources