Select to view content in your preferred language

Data Expression To Return Most Recent Date for Hydrant Inspection in Dashboard Indicator

153
7
Jump to solution
Wednesday
MelanieBass
Occasional Contributor

I have a hydrant inspection layer that I have configured a dashboard for and I am trying to get the indicator widget to reflect the correct number of hydrants still needing inspections. Currently the number is inflated because there are multiple inspections per hydrant. I have been working on trying to create a data expression to create a FeatureSet to filter most recent inspections per hydrant, but I am having no luck. Every code I have tried multiple code combinations, and it just returns an empty dataset. 

// Load the hydrant inspections layer
var inspections = FeatureSetByPortalItem(
Portal("https://maps.casselberry.org/portal"),
"f3a4a8879ce34241a5fd139ddfaf727a", // Inspection Layer Item ID
0
);

var maxDate = Text(Date(Max(fs, 'dateinspected')), 'YYYY-MM-DD');
return Filter(fs, 'dateinspected = @maxDate');

 

This code is based on this github post I had found

MelanieBass_0-1745423030227.png

 

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

To get the latest inspection for all of the hydrants, this code should work. Make sure you change the name of 'HyrantID' to the correct field name

var inspections = FeatureSetByPortalItem(
Portal("https://maps.casselberry.org/portal"),
"f3a4a8879ce34241a5fd139ddfaf727a", // Inspection Layer Item ID
0
);
GroupBy(inspections , 'HyrantID', {name: 'Latest', expression: 'dateinspected', statistic: 'MAX'})

 

View solution in original post

7 Replies
KenBuja
MVP Esteemed Contributor

The dataset in the Github repository example has that date in a text field, not a date field. If your field is a date field, you can do this much easier using the OrderBy and First functions

var inspections = FeatureSetByPortalItem(
Portal("https://maps.casselberry.org/portal"),
"f3a4a8879ce34241a5fd139ddfaf727a", // Inspection Layer Item ID
0
);
return First(OrderBy(inspections, "dateinspected DESC"));

 

MelanieBass
Occasional Contributor

Thank you for your prompt response! I tested it and it appeared to work till I clicked done and then got this.

MelanieBass_0-1745425568418.png

 

0 Kudos
KenBuja
MVP Esteemed Contributor

Ah, I forgot the first rule of Data Expressions...always return a FeatureSet.

This should work

var inspections = FeatureSetByPortalItem(
Portal("https://maps.casselberry.org/portal"),
"f3a4a8879ce34241a5fd139ddfaf727a", // Inspection Layer Item ID
0
);
var feat = First(OrderBy(inspections, "dateinspected DESC"));
var dict = { fields: Schema(inspections ).fields, features: [feat] };
return FeatureSet(dict);
MelanieBass
Occasional Contributor

Unfortunately still not working..

MelanieBass_0-1745426632935.png

Will this code return most recent for EACH hydrant not just most recent inspections done for all hydrants? so most recent inspection per hydrantid. 

 

0 Kudos
KenBuja
MVP Esteemed Contributor

Are you using an older version of Enterprise? If so, you'll have to use

return FeatureSet(Text(dict));

And yes, this code only returns the most recent inspection.

0 Kudos
KenBuja
MVP Esteemed Contributor

To get the latest inspection for all of the hydrants, this code should work. Make sure you change the name of 'HyrantID' to the correct field name

var inspections = FeatureSetByPortalItem(
Portal("https://maps.casselberry.org/portal"),
"f3a4a8879ce34241a5fd139ddfaf727a", // Inspection Layer Item ID
0
);
GroupBy(inspections , 'HyrantID', {name: 'Latest', expression: 'dateinspected', statistic: 'MAX'})

 

MelanieBass
Occasional Contributor

OMG this worked! Thank you soooo much! I have been going crazy with this code for days! I appreciate it!

0 Kudos