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?
Solved! Go to Solution.
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
To post code:
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
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'
}
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
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'
}
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
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.
You can't pass an Arcade operator, &&, in a SQL statement because whatever is processing the SQL has no clue how to handle &&.