Select to view content in your preferred language

Date Selector by month

2091
3
Jump to solution
08-04-2022 09:56 AM
ryanEvanczyk
Frequent Contributor

I have a dashboard displaying seasonal snow data. I want to be able to look at the historical averages by winter and month and calculate % of average.

An indicator can calculate the average per time period based on the date selector, but then I only see the average amount of snow for that specific date range, not over the 30-years of data.

I can define date options for months, but it seems tedious to choose every May for the past 30 years. Is there a way to custom define date ranges more easily?

ryanEvanczyk_0-1659471702520.png

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

A Data Expression could take care of that for you. If you reshaped the data to have a "month" field separate from its date/time field, you could then use that as a category. We can use one of the functions that utilizes SQL to get that calculated field into our output FeatureSet.

It might look something like this:

var fields = ['fields', 'you', 'need']

var fs = FeatureSetByPortalItem(
    Portal('your portal url'),
    'itemid of service',
    0, // or whatever layer index your specific layer is at
    fields,
    false
)

// convert list of fields into array of dicts that Distinct can use

var field_dicts = []

for (var f in fields){
    Push(
        field_dicts,
        {name: fields[f], expression: fields[f]}
    )
}

// push our calculated date field into array

Push(
    field_dicts,
    {name: 'the_month', expression: 'EXTRACT(MONTH FROM date_field)'}
)

// use field dicts to return featureset with added field
return Distinct(fs, field_dicts)

 

And just to explain that a bit, the FeatureSetByPortalItem function takes an array of field names as a parameter. In order to use SQL in Distinct, however, we need an array of dictionaries with a name and expression. If we omit any of our input fields from that array of dictionaries, it will be dropped from the output.

You could just type out the whole array of dicts by hand, but I don't know how many fields you want in your FeatureSet. This method of constructing the array of dicts from the array of field names can accommodate any number of fields listed in the fields array without any change.

- Josh Carlson
Kendall County GIS

View solution in original post

3 Replies
jcarlson
MVP Esteemed Contributor

A Data Expression could take care of that for you. If you reshaped the data to have a "month" field separate from its date/time field, you could then use that as a category. We can use one of the functions that utilizes SQL to get that calculated field into our output FeatureSet.

It might look something like this:

var fields = ['fields', 'you', 'need']

var fs = FeatureSetByPortalItem(
    Portal('your portal url'),
    'itemid of service',
    0, // or whatever layer index your specific layer is at
    fields,
    false
)

// convert list of fields into array of dicts that Distinct can use

var field_dicts = []

for (var f in fields){
    Push(
        field_dicts,
        {name: fields[f], expression: fields[f]}
    )
}

// push our calculated date field into array

Push(
    field_dicts,
    {name: 'the_month', expression: 'EXTRACT(MONTH FROM date_field)'}
)

// use field dicts to return featureset with added field
return Distinct(fs, field_dicts)

 

And just to explain that a bit, the FeatureSetByPortalItem function takes an array of field names as a parameter. In order to use SQL in Distinct, however, we need an array of dictionaries with a name and expression. If we omit any of our input fields from that array of dictionaries, it will be dropped from the output.

You could just type out the whole array of dicts by hand, but I don't know how many fields you want in your FeatureSet. This method of constructing the array of dicts from the array of field names can accommodate any number of fields listed in the fields array without any change.

- Josh Carlson
Kendall County GIS
ryanEvanczyk
Frequent Contributor

Thanks so much for the response.

Thankfully, we only collect data in the winter and I'm building a new survey for the upcoming season which I'll append the data to when it's ready.

The month column is a great solution. I added that and a snow year column and the new survey has calculation fields to pull those numbers from the datetime field. Now if I can just get the data to append properly....

0 Kudos
ChelseaRozek
MVP Regular Contributor
0 Kudos