Select to view content in your preferred language

FeatureSet GroupBy CASE expression

705
3
Jump to solution
04-02-2024 09:47 PM
Labels (1)
BlakeMorrison
Regular Contributor

I'm trying to write an SQL CASE expression for use within a FeatureSet/GroupBy function. I have a Date field that I'm grabbing using FeatureSetByPortalItem and I want to create a field that just puts the ages in classes.

Ideally, the dates would be dynamic as well, based on today's date and a defined age.

I'm pretty sure the indentation below is improper, but you get the idea of what I'm trying to achieve.

    {
        name: 'feature_age',
        expression: 'CASE 
                       WHEN date_stat => 2020 THEN 1
                       ELSE date_stat => 2012 THEN 2
                       ELSE date_stat => 1984 THEN 3
                       ELSE date_stat => 1850 THEN 4
                       ELSE NULL
                      END',
        statistic: 'SUM'
    },

 

Related to a previous post that I found the solution to: https://community.esri.com/t5/arcgis-dashboards-questions/data-too-big-for-a-featureset-calculation-...

0 Kudos
1 Solution

Accepted Solutions
BlakeMorrison
Regular Contributor

For those playing at home, the answer is:

 

    {
        name: 'feature_age',
        expression: 'CASE WHEN @today-date_stat < 1460 THEN 1 WHEN @today-date_stat < 4380 THEN 2 WHEN @today-date_stat < 14600 THEN 3 WHEN @today-date_stat > 14600 THEN 4 ELSE NULL END',
        statistic: 'SUM',
    },

 

Note: Number of days is used to calculate the age. Ie. 1460 = 4x365

Note2: @today is a var, outside the GroupBy function, defined as Now()

View solution in original post

0 Kudos
3 Replies
BlakeMorrison
Regular Contributor

For those playing at home, the answer is:

 

    {
        name: 'feature_age',
        expression: 'CASE WHEN @today-date_stat < 1460 THEN 1 WHEN @today-date_stat < 4380 THEN 2 WHEN @today-date_stat < 14600 THEN 3 WHEN @today-date_stat > 14600 THEN 4 ELSE NULL END',
        statistic: 'SUM',
    },

 

Note: Number of days is used to calculate the age. Ie. 1460 = 4x365

Note2: @today is a var, outside the GroupBy function, defined as Now()

0 Kudos
KenBuja
MVP Esteemed Contributor

You're not accounting for leap days. Four years has 1461 days, 12 years has 4383 days, and 40 years has 14610 days.

BlakeMorrison
Regular Contributor

haha

Thanks!

0 Kudos