Select to view content in your preferred language

Use Arcade Expression to Populate Calculated Field

840
2
Jump to solution
11-13-2023 04:07 AM
GregReinecke
Frequent Contributor

Hello All,

I created an Arcade Expression that when used in a popup displays an attribute for a feature created in Survey123 Field App.  If a feature was collected prior to the current week it is assigned hidden. If it's collected during the current week up until Saturday at noon it is visible and if between noon and Saturday at midnight it is also hidden. This works great in a popup but as many threads prior have stated it would be perfect if this worked with a filter. 

That said since it doesn't is there a workaround? I can assign it as a calculated field but I think I have to re-run the expression on the calculated field to update all the features collected. I guess I could run a script Saturday at 1 minute after noon to reset all to be hidden since at that point they would be.

Any other ideas would be helpful like perhaps have the expression or something like it kick off directly in Survey123. That would be ideal.

The script is below if would be helpful to anyone. Thanks.

GR

// This Arcade Expression assigns an attribute "Show" or "Hide" to a feature 
// based on the day and time it was created. Also assigns "Hide" if not created
//during the current week.

// Get the current week for comparison to previous weeks
var currentDate = Now();
var currentWeek = Week(currentDate);

// Get the day of the week the feature was created
var dayOfWeek = Weekday($feature._date_time);
var dayOfWeekString = "";
if (dayOfWeek == 0) {dayOfWeekString = "Sunday"} 
    else if (dayOfWeek == 1) {dayOfWeekString = "Monday"}
    else if (dayOfWeek == 2) {dayOfWeekString = "Tuesday"} 
    else if (dayOfWeek == 3) {dayOfWeekString = "Wednesday"} 
    else if (dayOfWeek == 4) {dayOfWeekString = "Thursday"} 
    else if (dayOfWeek == 5) {dayOfWeekString = "Friday"} 
    else  (dayOfWeek == 6) {dayOfWeekString = "Saturday"}

// Get the time of day (_hour) the feature was created
var dateTimeField = $feature._date_time;
var _hour = Hour(dateTimeField);

// Get the time of day (_minute) the feature was created
var dateTimeField = $feature._date_time;
var _minute = Text(Minute(dateTimeField), '00');

// Create _hour_minute
var _hour_minute = _hour + _minute

// Get the week the feature was created
var _week = Week($feature._date_time)

if (dayOfWeek == 0 && _week == currentWeek) {return 'Show'}
    else if (dayOfWeek == 1 && _week == currentWeek) {return 'Show'}
    else if (dayOfWeek == 2 && _week == currentWeek) {return 'Show'}
    else if (dayOfWeek == 3 && _week == currentWeek) {return 'Show'}
    else if (dayOfWeek == 4 && _week == currentWeek) {return 'Show'}
    else if (dayOfWeek == 5 && _week == currentWeek) {return 'Show'}
    else if (dayOfWeek == 6 && _hour_minute <= 1200 && _week == currentWeek) {return 'Show'}  
    else return 'Hide'

 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
gis_KIWI4
Frequent Contributor

Hi @GregReinecke ,

Would it work if you set the symbology of the layer using a similar arcade expression.
Applying the expression should give you 2 categories.
The feature captured in the current week would have a proper symbol the other that do not fit the criteria would have the symbol size set to 0. 

gis_KIWI4_0-1700016971750.png

 



View solution in original post

0 Kudos
2 Replies
gis_KIWI4
Frequent Contributor

Hi @GregReinecke ,

Would it work if you set the symbology of the layer using a similar arcade expression.
Applying the expression should give you 2 categories.
The feature captured in the current week would have a proper symbol the other that do not fit the criteria would have the symbol size set to 0. 

gis_KIWI4_0-1700016971750.png

 



0 Kudos
GregReinecke
Frequent Contributor

That would do it. Thanks so much. 

0 Kudos