Arcade for Data Expression

795
5
06-05-2024 02:12 AM
MariusVerdes
Regular Contributor

Hello,

I’m working on a new dashboard based on data from an existing survey, but I’m new to Data Expressions, and I’ve hit a roadblock.

I want to create 4 different elements.

  1. Indicators:

    • I want to create two indicators:
      • One for the count of surveys with only “Yes” answers.
      • Another for the count of surveys with no answers.
    • Null values should not impact the counting.
  2. Bar Chart:

    • Additionally, I’d like to create a bar chart showing the total number of “NO” answers per chapter and per question.

I’ve attached the layer information with all the columns.

Thanks in advance for any suggestions!

Tags (1)
0 Kudos
5 Replies
MariusVerdes
Regular Contributor

I was able to create something for the indicator but it returns JSON instead of the number as the final result

"// Define the FeatureSet including all necessary fields
var fs = FeatureSetByPortalItem(
Portal('URL of the enterprise'),
'id of e feature layer',
0,
["Count_No", 'Q1_1', 'Q1_2', 'Q1_3', 'Q1_4'],
false
);

// Calculate the number of No answers
var noCount = 0;
for (var i = 0; i < 86; i++) {
var columnName = "column_" + i; // Adjust this according to your column naming convention
noCount += Count(Filter(fs, columnName + " = 'No'"));
}

var ratioDict = {
'fields': [
{'name':'No_Count', 'type':'esriFieldTypeInteger'}
],
'geometryType': '',
'features': [{
'attributes': {
'No_Count': noCount
}
}]
};

// Transform the dictionary into a FeatureSet and return it
return FeatureSet(Integer(ratioDict));"

0 Kudos
KenBuja
MVP Esteemed Contributor

Use this as the final line

return FeatureSet(ratioDict);

 

0 Kudos
MariusVerdes
Regular Contributor

Hi @KenBuja 
Thanks for the tip, it solved the error from the Arcade editor inside the Dashboard, but after I click "Done" it says that it is unable to execute the script (see the below screenshots). Any idea why this happens?

0 Kudos
KenBuja
MVP Esteemed Contributor

This code (leaving off the calculation to No Count) give me a FeatureSet. What does it do in your dashboard?

var noCount = 0
var ratioDict = {
  'fields': [
    {'name':'No_Count', 'type':'esriFieldTypeInteger'}
  ],
  'geometryType': '',
  'features': [{
    'attributes': {
      'No_Count': noCount
     }
  }]
};

// Transform the dictionary into a FeatureSet and return it
return FeatureSet(ratioDict);

 

0 Kudos
MariusVerdes
Regular Contributor

Hi @KenBuja ,


No, it gives the following error "Execution error - Line: 15, 7: Invalid parameter"

But, I tried a workaround with an attribute rule and it gives me what I need. The code for the attribute rule is:
"

var countNo = 0;

if ($feature.Q1_1 == 'No') {
countNo += 1;
}

if ($feature.Q1_2 == 'No') {
countNo += 1;
}

if ($feature.Q1_3 == 'No') {
countNo += 1;
}

if ($feature.Q1_4 == 'No') {
countNo += 1;
}

if ($feature.Q2_1 == 'No') {
countNo += 1;
}

if ($feature.Q2_2 == 'No') {
countNo += 1;
}

if ($feature.Q3_1 == 'No') {
countNo += 1;
}

if ($feature.Q3_2 == 'No') {
countNo += 1;
}

if ($feature.Q3_3 == 'No') {
countNo += 1;
}

if ($feature.Q3_4 == 'No') {
countNo += 1;
}

if ($feature.Q3_5 == 'No') {
countNo += 1;
}

if ($feature.Q3_6 == 'No') {
countNo += 1;
}

if ($feature.Q3_7 == 'No') {
countNo += 1;
}

if ($feature.Q3_8 == 'No') {
countNo += 1;
}

if ($feature.Q3_9 == 'No') {
countNo += 1;
}

if ($feature.Q3_10 == 'No') {
countNo += 1;
}

if ($feature.Q3_11 == 'No') {
countNo += 1;
}

if ($feature.Q4_1 == 'No') {
countNo += 1;
}

if ($feature.Q4_2 == 'No') {
countNo += 1;
}

if ($feature.Q4_3 == 'No') {
countNo += 1;
}

if ($feature.Q4_4 == 'No') {
countNo += 1;
}

if ($feature.Q4_5 == 'No') {
countNo += 1;
}

if ($feature.Q4_6 == 'No') {
countNo += 1;
}

if ($feature.Q5_1 == 'No') {
countNo += 1;
}

if ($feature.Q5_2 == 'No') {
countNo += 1;
}

if ($feature.Q5_3 == 'No') {
countNo += 1;
}

if ($feature.Q5_4 == 'No') {
countNo += 1;
}

if ($feature.Q5_5 == 'No') {
countNo += 1;
}

if ($feature.Q5_6 == 'No') {
countNo += 1;
}

if ($feature.Q5_7 == 'No') {
countNo += 1;
}

if ($feature.Q6_1 == 'No') {
countNo += 1;
}

if ($feature.Q6_2 == 'No') {
countNo += 1;
}

if ($feature.Q6_3 == 'No') {
countNo += 1;
}

if ($feature.Q6_4 == 'No') {
countNo += 1;
}

if ($feature.Q6_5a == 'No') {
countNo += 1;
}

if ($feature.Q6_5b == 'No') {
countNo += 1;
}

if ($feature.Q6_5c == 'No') {
countNo += 1;
}

if ($feature.Q6_5d == 'No') {
countNo += 1;
}

if ($feature.Q6_5e == 'No') {
countNo += 1;
}

if ($feature.Q6_5f == 'No') {
countNo += 1;
}

if ($feature.Q6_5g == 'No') {
countNo += 1;
}

if ($feature.Q6_5h == 'No') {
countNo += 1;
}

if ($feature.Q6_5i == 'No') {
countNo += 1;
}

if ($feature.Q6_6 == 'No') {
countNo += 1;
}

if ($feature.Q6_7a == 'No') {
countNo += 1;
}

if ($feature.Q6_7b == 'No') {
countNo += 1;
}

if ($feature.Q6_8 == 'No') {
countNo += 1;
}

if ($feature.Q6_9 == 'No') {
countNo += 1;
}

if ($feature.Q6_10 == 'No') {
countNo += 1;
}

if ($feature.Q6_11 == 'No') {
countNo += 1;
}

if ($feature.Q6_12 == 'No') {
countNo += 1;
}

if ($feature.Q6_13 == 'No') {
countNo += 1;
}

if ($feature.Q6_14 == 'No') {
countNo += 1;
}

if ($feature.Q6_15 == 'No') {
countNo += 1;
}

if ($feature.Q7_1 == 'No') {
countNo += 1;
}

if ($feature.Q7_2 == 'No') {
countNo += 1;
}

if ($feature.Q7_3 == 'No') {
countNo += 1;
}

if ($feature.Q7_4 == 'No') {
countNo += 1;
}

if ($feature.Q7_5 == 'No') {
countNo += 1;
}

if ($feature.Q7_6 == 'No') {
countNo += 1;
}

if ($feature.Q7_7 == 'No') {
countNo += 1;
}

if ($feature.Q7_8 == 'No') {
countNo += 1;
}

if ($feature.Q7_9 == 'No') {
countNo += 1;
}

if ($feature.Q7_10 == 'No') {
countNo += 1;
}

if ($feature.Q7_11 == 'No') {
countNo += 1;
}

if ($feature.Q7_12 == 'No') {
countNo += 1;
}

if ($feature.Q7_13 == 'No') {
countNo += 1;
}

if ($feature.Q7_14 == 'No') {
countNo += 1;
}

if ($feature.Q7_15 == 'No') {
countNo += 1;
}

if ($feature.Q7_16 == 'No') {
countNo += 1;
}

if ($feature.Q7_17 == 'No') {
countNo += 1;
}

if ($feature.Q7_18 == 'No') {
countNo += 1;
}

if ($feature.Q7_19 == 'No') {
countNo += 1;
}

if ($feature.Q7_20 == 'No') {
countNo += 1;
}

if ($feature.Q7_21 == 'No') {
countNo += 1;
}

if ($feature.Q7_22 == 'No') {
countNo += 1;
}

if ($feature.Q8_1 == 'No') {
countNo += 1;
}

if ($feature.Q8_2 == 'No') {
countNo += 1;
}

if ($feature.Q8_3 == 'No') {
countNo += 1;
}

if ($feature.Q8_4 == 'No') {
countNo += 1;
}

if ($feature.Q8_5 == 'No') {
countNo += 1;
}

if ($feature.Q8_6 == 'No') {
countNo += 1;
}

if ($feature.Q8_7 == 'No') {
countNo += 1;
}

if ($feature.Q8_8 == 'No') {
countNo += 1;
}

if ($feature.Q9_1 == 'No') {
countNo += 1;
}

if ($feature.Q9_2 == 'No') {
countNo += 1;
}

if ($feature.Q9_3 == 'No') {
countNo += 1;
}

if ($feature.Q10_1 == 'No') {
countNo += 1;
}

if ($feature.Q10_2 == 'No') {
countNo += 1;
}

if ($feature.Q10_3 == 'No') {
countNo += 1;
}

if ($feature.Q10_4 == 'No') {
countNo += 1;
}

if ($feature.Q10_5 == 'No') {
countNo += 1;
}

return countNo;"

0 Kudos