Select to view content in your preferred language

Dashboard data Expression returns null featureset

101
3
Thursday
SamBelisleLID1990
Emerging Contributor

I am trying to create a dashboard gauge to show percentage of time passed in a season. So, if the season runs from 1 July to 30 September, then on 23 July, the gauge should read 25%. 

I want the FeatureSet to show the percentage in the 'DayPercentage' field for the gauge. 

My Arcade code to do so is as follows: 

var ThisYear = Number(Year(Now()));
//var ThisMonth = Month(Now());
//var ThisDay = Day(Now())

var startDate = Number(Date(ThisYear,7,1));
var endDate = Number(Date(ThisYear,9,30));
var currentDate = Number(Date(ThisYear, 7, 25));

var Season = Number(DateDiff(endDate,startDate))
var SeasonSoFar = Number(DateDiff(currentDate,startDate))
var SeasonPerc = Number((SeasonSoFar/Season)*100);

var features = [];

Push(features, SeasonPerc)

var PercDict = {
  'fields': [
    {'name':'DayPercentage','type':'esriFieldTypeInteger'}
  ],
  'features': features
};

var features2 = FeatureSet(PercDict);

return features2

This returns a null dataset. I've tried formatting the Dictionary differently, formatting the FeatureSet differently, using Push to get the data through in different places and formats, and I'm stuck. What am I doing wrong? 

Tags (2)
0 Kudos
3 Replies
KenBuja
MVP Esteemed Contributor

There were a couple of things that needed changing.

  • The dates are incorrect if you're portraying 1 July through 30 Sept. In the Date function, months are 0-based, so July is 6, not 7 (lines 5, 6, and 7)
  • The SeasonPerc was a NaN since you were converting the Dates to numbers (lines 5, 6, and 7). The DateDiff function needs Dates as input, not the converted number.
  • DateDiff also returns a number, so there's no need to convert it (lines 9 and 10)
  • SeasonPerc has to be an integer if you're putting it into a integer field (line 11). If it's not converted (and Arcade will not convert it for you), that field attribute will be null.
  • You have to push a dictionary object containing the "attributes" key into the features array (line 15)
var ThisYear = Year(Now());
//var ThisMonth = Month(Now());
//var ThisDay = Day(Now())

var startDate = Date(ThisYear, 6, 1);
var endDate = Date(ThisYear, 8, 30);
var currentDate = Date(ThisYear, 6, 25);

var Season = DateDiff(endDate, startDate);
var SeasonSoFar = DateDiff(currentDate, startDate);
var SeasonPerc = Round(Number(SeasonSoFar / Season * 100));

var features = [];

Push(features, { attributes: { DayPercentage: SeasonPerc } });

var PercDict = {
  fields: [{ name: "DayPercentage", type: "esriFieldTypeInteger" }],
  features: features
};

var features2 = FeatureSet(PercDict);

return features2;

 

0 Kudos
SamBelisleLID1990
Emerging Contributor

Thank you so much!

Many of those changes were things I had actually done correctly the first time but had changed in all my attempts to fix this. The push with attributes was the missing piece. 

0 Kudos
KenBuja
MVP Esteemed Contributor

Glad to help. Please check the "Accept as Solution" box to help others searching on this problem

0 Kudos