Select to view content in your preferred language

Field Calculator for not null fields

6322
11
Jump to solution
11-08-2018 10:38 AM
AndrewIngall1
Occasional Contributor

I am trying to use the field calculator to concatenate various fields into one new field.  The ArcGIS Arcade Guide gives the following example

Concatenate(['red', 'blue', 'green'], '/')

which gives red/blue/green.  This works fine when all of the fields contain a value, but how can i concatenate only the fields that are not null

I am trying to concatenate address details , so for example 

field1 = building name

field2 = building number

field3 = road name

field4 = area

field5 = town

in my example the building name or area could be null, so i end up with:-

,12,A Road,,Town - so i need to remove the additional field separators ','

Thanks for any help

Tags (1)
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Just for future reference, you could use something like this in Arcade next time:

// replace the field names according to your data
var field1 = $feature["field1"];
var field2 = $feature["field2"];
var field3 = $feature["field3"];
var field4 = $feature["field4"];
var field5 = $feature["field5"];

var fields_in = [field1, field2, field3, field4, field5];
var fields_out = [];
for (var i = 0; i < Count(fields_in); i++) { 
    if (fields_in[i] != Null) {
        var j = Count(fields_out);
        fields_out[j] = fields_in[i];
    }
}

var label = Concatenate(fields_out, "/");
return label;

This will only omit Null values and not empty strings. 

I know Dan, this is much longer than the Python snippet you provided... 

View solution in original post

11 Replies
DanPatterson_Retired
MVP Emeritus

Arcade... no, someone else can translate this example from python

# ---- pretend data, representing field values

field1 = 'building name'
field2 =  None
field3 = 'road name'
field4 = None
field5 = 'town'

# --- put them into a list... you could put the whole list in, I just simplied it

flds = [field1, field2, field3, field4, field5]

", ".join([i for i in flds if i is not None])

# ---- result

'building name, road name, town'
MichaelVolz
Esteemed Contributor

Dan:

In your python example, is None actually Null or some other way of showing empty data?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

SQL Null gets mapped to Python None in cursors and the Field Calculator, so one can use Python None to test how SQL Null will behave with a code snippet.

0 Kudos
KoryKramer
Esri Community Moderator

Andrew Ingall‌ Did Dan's reply give you what you needed?  If so, could you mark this as answered - maybe share back how you did this in Arcade?  

If not, we should try to get an answer posted here.  I know Xander Bakker‌ is handy with Arcade!

AndrewIngall1
Occasional Contributor

@Kory Kramer - I actually went down the FME route to concatenate the field together - I could not get my head around how to do it in ArcGIS Pro with Arcade.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Just for future reference, you could use something like this in Arcade next time:

// replace the field names according to your data
var field1 = $feature["field1"];
var field2 = $feature["field2"];
var field3 = $feature["field3"];
var field4 = $feature["field4"];
var field5 = $feature["field5"];

var fields_in = [field1, field2, field3, field4, field5];
var fields_out = [];
for (var i = 0; i < Count(fields_in); i++) { 
    if (fields_in[i] != Null) {
        var j = Count(fields_out);
        fields_out[j] = fields_in[i];
    }
}

var label = Concatenate(fields_out, "/");
return label;

This will only omit Null values and not empty strings. 

I know Dan, this is much longer than the Python snippet you provided... 

DanPatterson_Retired
MVP Emeritus

And longer than in Avenue too

Andra
by
Emerging Contributor

Hi, Xander

Thanks for the solution to this concatenation task. When i use your code in ArcGIS pro 2.9.5 field calculator tool in model builder, i am getting an error 'identifier expected' on line 17:

 

var field1 = $feature.utm_check;
var field2 = $feature.township_match;
var field3 = $feature.range_match;
var field4 = $feature.section_match;
var field5 = $feature.DPA_match;
var field6 = $feature.county_match;

var fields_in = [field1, field2, field3, field4, field5, field6];
var fields_out = [];
for (var i = 0; i < Count(fields_in); i++) { 
    if (fields_in[i] != Null) {
        var j = Count(fields_out);
        fields_out[j] = fields_in[i];
    }
}

var label = Concatenate(fields_out, ",");
return label;‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

 

Can you provide any guidance on this syntax error please? 

thanks, a

 

Andra
by
Emerging Contributor

Update - I was able to use Dan's python suggestion to concatenate the fields, but i am still curious about the above error if there is a simple solution. thanks!

 

 

', '.join([str(i) for i in [ !utm_check!, !township_match!, !range_match!, !section_match!, !DPA_match!, !county_match!] if i])

 

 

-a

0 Kudos