Select to view content in your preferred language

ArcGIS Pro 3.0.3 - Invalid Arcade Syntax for Attribute Rule?

1368
7
Jump to solution
02-21-2023 08:43 AM
VincentLaunstorfer
Regular Contributor

Hi,

In ArcGIS Pro, I am trying to implement a simple Attribute Rule calculation in Arcade and I am stuck with syntax! I am filtering the current features with an SQL type query and Arcade returns an invalid where clause...

In practice, this Filter function should return a unique feature with which I am performing a growth calculation on the current feature.

Any guess?

Invalid_Arcade_expression_Builder2.png

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

What field types are date and census?

Try this:

 

// invert your condition and return the default value early.
// that makes the following code easier to follow, because
// you don't need the extra else block
if($feature.Date == 2019){
    return '0'
}
var census = $feature.census; // take the "pure" value instead of Text()
var startDate = 2019;
var features = Filter($featureSet, "date = @startDate AND census = @census");
var featureFirst = First(features);
if(featureFirst == null){
    return '0'
}
var growth = (($feature.Totals_by_Tract_by_Sector/featureFirst.Totals_by_Tract_by_Sector)-1)*100;
return growth

 


Have a great day!
Johannes

View solution in original post

7 Replies
JohannesLindner
MVP Frequent Contributor

To post code:

JohannesLindner_0-1674589814049.png

JohannesLindner_1-1674589827880.png

 

While many programming languages use the operator && as logical and, SQL uses "AND". So your query in line 4 should be

"date = @startDate AND census = @census"

 

Also, you're missing a null check after line 4. If the Filter function returns an empty featureset (nothing found with this query), First() will return null, and then you'll get an error in line 6, when you call null.SomeField

After line 5, insert something like this:

if(dic == null) { return '0' }  // I guess this is your default value, judging from line 9

Have a great day!
Johannes
0 Kudos
VincentLaunstorfer
Regular Contributor

Thanks. I already tried the proper 'SQL' like AND, but still no luck!

I included your 'null' logic to safeguard my Arcade code

 

if($feature.Date != '2019'){
  var census = Text($feature.census);
  var startDate = '2019';
  var features = Filter($featureSet, "date = @startDate AND census = @census");
  var featureFirst = First(features);
  if(dic == null){
    return '0'} else {
    var growth = (($feature.Totals_by_Tract_by_Sector/featureFirst.Totals_by_Tract_by_Sector)-1)*100;
  }
} else {
  return '0'
}

 

Invalid_Arcade_expression_Builder_AND.png

0 Kudos
JohannesLindner
MVP Frequent Contributor

What field types are date and census?

Try this:

 

// invert your condition and return the default value early.
// that makes the following code easier to follow, because
// you don't need the extra else block
if($feature.Date == 2019){
    return '0'
}
var census = $feature.census; // take the "pure" value instead of Text()
var startDate = 2019;
var features = Filter($featureSet, "date = @startDate AND census = @census");
var featureFirst = First(features);
if(featureFirst == null){
    return '0'
}
var growth = (($feature.Totals_by_Tract_by_Sector/featureFirst.Totals_by_Tract_by_Sector)-1)*100;
return growth

 


Have a great day!
Johannes
JohannesLindner
MVP Frequent Contributor

In your other answer, Arcade is complaining about census being empty. To solve that, replace line 4 in the script above:

 

if($feature.Date == 2019 || IsEmpty($feature.census)){
    return '0'
}

 


Have a great day!
Johannes
0 Kudos
VincentLaunstorfer
Regular Contributor

Hi,

Really nice, it works and I like the 'inverted' logic to avoid the else statment.

Moreover, I have a similar Arcade code and it never go to the else statment!

 

if($feature.Date != '2019'){
  var census = ($feature.census);
  var startDate = 2019;
  var features = Filter($featureSet, "date = " + startDate + " AND census = " + census);
  var featureFirst = First(features);
  if(features != null){
    var growth = (($feature.Totals_by_Tract_by_Sector/featureFirst.Totals_by_Tract_by_Sector)-1)*100;
  }
  return growth;
} else {
  return '99';
}

Strange

0 Kudos
JohannesLindner
MVP Frequent Contributor

You said that $feature.Date is a double. You compare it to a string. Your condition will always be true, so the else block will never be entered.


Have a great day!
Johannes
JoshuaBixby
MVP Esteemed Contributor

You can't pass an Arcade operator, &&, in a SQL statement because whatever is processing the SQL has no clue how to handle &&.

0 Kudos