Pro Arcade Expression using When

2933
4
Jump to solution
04-02-2018 08:52 AM
MichaelVolz
Esteemed Contributor

I am trying to calculate a field in Pro based off another field using the When function in Arcade.  The field I am calculating off (TYPE) is numeric with values such as 1,2,3,4.  The field I am calculating is text.

I use Calculate field and select Arcade for Expression Type and add the following text:

var type = $feature.TYPE;
When([type = 1, 'Primary', type = 2, 'Secondary', type = 3, 'Tertiary'], 'N/A')

Unfortunately, when I verify the expression I get the following error message

"Invalid Expression

Error 0 on line 2.

Close square brace expected."

I thought I was following the sample in the documentation, so I'm confused that I would get an error.

Any help to solve this issue is greatly appreciated.

0 Kudos
1 Solution

Accepted Solutions
BalajiVeera
Occasional Contributor

Use this:

var $feature.TYPE;
When(type == 1, 'Primary', type == 2, 'Secondary', type == 3, 'Tertiary', 'N/A')

View solution in original post

4 Replies
DanPatterson_Retired
MVP Emeritus

When(density < 50, 'low', density >=50 && density < 100, 'medium', density >= 100, 'high', 'n/a');

from here

https://developers.arcgis.com/arcade/function-reference/logical_functions/

I think the square braces was being taken too literally in your expression

0 Kudos
MichaelVolz
Esteemed Contributor

That is the documentation I was reading when attempting to use the When expression.  I thought I would be able to use = in my expression as the documentation does not indicate you cannot use =, but that seems to be the problem.

When I changed my syntax to:

When(type < 2, 'Primary', type > 1 && type< 3, 'Secondary', type > 2, 'Tertiary', 'N/A')

I was able to verify the expression and successfully calculate the field.

If I was to use the square braces in my expression I get an error, so the documentation seems misleading in that regard.

0 Kudos
DanPatterson_Retired
MVP Emeritus

I think the square brackets are referring to a list of optional parameters, not an actual list

BalajiVeera
Occasional Contributor

Use this:

var $feature.TYPE;
When(type == 1, 'Primary', type == 2, 'Secondary', type == 3, 'Tertiary', 'N/A')