I am using advanced formatting in an indicator to perform a calculation.
My $datapoint has a filter where FIELD1 = X
And my $reference refers to the same layer where FIELD1 = Y.
When using a category selector where the FIELD2 = A, $datapoint is always empty/no values are returned. This is known and expected. I'm trying to set up my Arcade so that it ignores empty values and still runs the calculation on $reference. Is this possible? So far I'm only getting No Data results when FIELD2 = A.
Here is my code:
Could you try the following Arcade code
// Calculate reference value
var value1 = $reference.SUM_FIELD3 * 25;
// Handle empty datapoint
var value2 = IIF(IsEmpty($datapoint.SUM_FIELD3) || $datapoint.SUM_FIELD3 == null, 0, $datapoint.SUM_FIELD3);
// Final calculation
var total = value1 + value2;
// Format output
return {
topText: "Total: " + Text(total, "#,###.##"),
noValue: false
};
The IsEmpty() may not catch all the null cases but combining it with ==null should help this. Then setting the noValue: false should override the default behaviour that hides the indicator when $datapoint is empty.
I hope this helps you out.
Thank you for your response and your help! Sadly, I'm getting the same results, where no data returns when the $datapoint is empty. It works perfectly well otherwise!
Even though you are using IIF(IsEmpty(....) ==null, 0, ...) The expression might still fail to evaluate if $datapoint itself is undefined or missing entirely. I have included below a refined approach that adds a defensive check for $datapoint itself before trying to access its fields
var value1 = $reference.SUM_FEILD3 * 25;
var value2 = 0;
if (!IsEmpty($datapoint) && HasKey($datapoint, "SUM_FIELD3") && !IsEmpty($datapoint.SUM_FIELD3)) {
value2 = $datapoint.SUM_FIELD3;
}
var total = value1 + value2;
return {
topText: 'Total',
middleText: Text(total, "#,###.##"),
middleTextMaxSize: 'large',
noValue: false
}
There a few reasons this can work better
1. IsEmpty($datapoint) check if the entire object is missing
2. HasKey($datapoint, "SUM_FIELD3") ensures the field exists before accessing it
3. IsEmpty($datapoint.SUM_FIELD3) handles the actual value check
This should hopefully ensure that your indicator always returns a value even if $datapoint is undefined or partially populated.
I hope this help in some way.
Alas, same result with that script. Thanks for sticking with me!
I'm exploring using a data expression for the source data. Is there a way I could force it to create a row for each Field1 category and Field2 category combination, but if empty the field3_sum = 0?
results in:
A | Site 1 | 5 | 35 |
A | Site 1 | 11 | 20 |
A | Site 1 | 0 | 1723 |
A | Site 1 | 6 | 721 |
A | Site 1 | 0 | 741 |
A | Site 1 | 44 | 27 |
A | Site 2 | 273 | 1 |
A | Site 2 | 56 | 183 |
A | Site 2 | 709 | 10 |
A | Site 2 | 32 | 163 |
B | Site 2 | 512 | 114 |
B | Site 2 | 587 | 104 |