How to use DateDiff function with custom dates in Dashboard

3066
9
Jump to solution
06-13-2023 12:34 PM
Labels (1)
wAlexPage
Emerging Contributor

I'm building an indicator in dashboard. What I would like to show is the current velocity & the velocity required to finish by year end in 'mi'.

I've got the current velocity working, sort of. In order to keep that working and get the the second velocity working I need DateDiff to work with manually entered dates. All the examples I see link it to date fields in the feature using $feature, but for this purpose those dates are irrelevant.

The 138 is my remaining working days and that is currently manually entered, thus I need the var age to generate this number instead. (var remainingDays = 138) You can see father down (age) is showing the NaN in topText:

I my project started 3-27-23 and times out 12-31-23. I'm wondering if the problem is the output format of the Date vs Day(Now()).

See screenshot for context.

wAlexPage_0-1686684778462.png

var endDate = Date('12/31/2023','MM/DD/YYYY')
var startDate = Day(Now())
var age = DateDiff(endDate, startDate,'days')

var remainingDays = 138
var totalProduction = $datapoint["sum_Miles"]  // Actual total production to date
var totalDays = 192  // The actual total number of days avalible

var projectedVelocity = totalProduction / (totalDays - remainingDays)

return {
    //textColor:'',
    //backgroundColor:'',
    topText: (age),
    //topTextColor: '',
    //topTextOutlineColor: '',
    //topTextMaxSize: 'medium',
    middleText: Round (projectedVelocity,3)+"mi",
0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Alum

Your startDate isn't a date, because you're extracting the current day...

You can also use Today() instead of Now() to get full days.

 

 

var endDate = Date('12/31/2023','MM/DD/YYYY')
var startDate = Today()
var age = DateDiff(endDate, startDate,'days')

 

 


Have a great day!
Johannes

View solution in original post

9 Replies
JohannesLindner
MVP Alum

Your startDate isn't a date, because you're extracting the current day...

You can also use Today() instead of Now() to get full days.

 

 

var endDate = Date('12/31/2023','MM/DD/YYYY')
var startDate = Today()
var age = DateDiff(endDate, startDate,'days')

 

 


Have a great day!
Johannes
wAlexPage
Emerging Contributor

Thank you very much for your assistance! I wanted to take a few and clean up the expression for everyone to benefit from. it worked beautifully.

wAlexPage_0-1686751560161.png

I got rid a place where it was duplicating one of the variables and swapped out one of the others to subtract 'nonWorkDays' giving us more control over how many days were actually available.

Thank you again @JohannesLindner !

Alex 

  

0 Kudos
wAlexPage
Emerging Contributor
Here's what I was doing with it.
wAlexPage_0-1686777408074.png

 

//var Days from start to date
var endDate = Today()
var startDate = Date('03/27/2023','MM/DD/YYYY')
var ytdDays = DateDiff(endDate, startDate,'days')

// Actual total production to date
var totalProduction = $Datapoint["sum_Miles"]
// The actual total number of days avalible
var nonWorkDays = 22

var actualVelocity =
totalProduction / (ytdDays - nonWorkDays)

return actualVelocity
 
//Good Luck
0 Kudos
AndyMinor2
Occasional Contributor

Hey Alex,

 

I am trying to do something similar to this. I am trying to determine the Age of an item based on the Date difference from Today to a Date in an attribute field. I would like this to work as either an Indicator or a Gauge. I appreciate any help you can provide.

 

Thanks,

Andy

 

//var Days from start to date
var endDate = Today()
var startDate = $datapoint.PurDate
var age = DateDiff(endDate, startDate,'days')

return age
 
 
0 Kudos
AlexPage
Occasional Contributor

Hey Andy,

I think we don't have enough information to help here. If this is for a Dashboard, the first thing I'd mention is the gauge won't have the access to Arcade that indicator does.

The next thing is if you look back at my expression the primary data I'm pulling is sum_Miles, this is a virtual item being compiled by the indicator on the Data tab. I don't have a feature called this, it's from telling the indicator to sum the miles field.

I don't believe you can pull something else (CreationDate) from $datapoint if it's already being used. You said you want to tell the age of something with an indicator, but yours is likely like mine in that this supports some other expresion. 

What is that? Is something else already using $datapoint? I've been playing with mine for a while now and I'm still stuck.

This isn't saying much though because I'm very new to all this, about a year in. I'll be back at the desk in the morning. G'night, Alex

0 Kudos
AndyMinor2
Occasional Contributor

Alex,

 

Sorry. Yes, a Dashboard. I was trying to use your Arcade expression since it does something similar to what I am trying to do. I am trying to make an Indicator that tracks how old my chickens are for butchering. I have an attribute field for the date the chicks hatched and want to keep track of their age and butcher around 8-10 weeks old. Then I will log the data using my QuickCapture app and see the weight, cost and dates for tracking production. I was hoping to use a Gauge to act like a fuel gauge and reach full when they are done.

 

I tried creating the date variables and pulling the date field from the $datapoint but didn't have any luck. I'm still trying to learn Arcade. Thanks for the help.

Andy

 

chicken-ops-dash.jpg

AlexPage
Occasional Contributor

Andy, 

I love this, I want to see you achieve this! We'll pick up next week. I grew up in the country and we did broilers too. 

Alex

AndyMinor2
Occasional Contributor

Here's my QC app. 

0 Kudos
AlexPage
Occasional Contributor

Wanted to bring this one home for everyone here.

To complete the indicators I needed to exclude the company holidays and weekends. This was being achieved through a manual entry above, but I wanted it scripted.

I worked with one of great folks at Esri, Paul Barker who gave me a direction to go, then took what I made and actually made it work. I was using normal dates, not starting at month 0, and attempting a for loop, but his 'while' statement looks so great, and is easier to get my head around.

What it does: Divides production / working days, by removing non-working days to create project velocity, and shows you the remaining days to the deadline. To create the actual velocity, just flip flip the initial variables.

 

 

// array of holiday dates (note Month is a zero indexed value)
var holiDates = [
  Date(2023, 0, 2),
  Date(2023, 3, 7),
  Date(2023, 4, 29),
  Date(2023, 6, 4),
  Date(2023, 10, 23),
  Date(2023, 10, 24),
  Date(2023, 11, 25),
  Date(2023, 11, 26)
];

//set up initial variables
var todayDate = Today();
var counter = 0;
var endDate = Date(2023, 6, 15); // End date of project

//iterate over the range and count the work days 
while (todayDate <= endDate) {
  var currWeekDay = Weekday(todayDate);
  var isWorkDay = currWeekDay > 0 && currWeekDay < 6;
  var isHoliday = Includes(holiDates, todayDate);

  // check that its a work day and not a holiday and if so count it
  if (isWorkDay && !isHoliday) {
        counter += 1;
  }
  todayDate = DateAdd(todayDate, 1, "days");
}

// Starting total project miles
var startMiles = 112.3

// Actual total production to date
var ytdProduction = $Datapoint["sum_Miles"]

var requiredVelocity = (startMiles - ytdProduction) / counter;

return {
  //textColor:'',
  //backgroundColor:'',
  topText: (counter)+" "+"Work Days Remain",
  topTextColor: 'red',
  topTextOutlineColor: 'red',
  topTextMaxSize: 'medium',
  middleText: Round (requiredVelocity,3)+"mi",
  middleTextColor: 'orange',
  middleTextOutlineColor: 'red',
  middleTextMaxSize: 'medium',
  //bottomText: '',
  //bottomTextColor: '',
  //bottomTextOutlineColor: '',
  //bottomTextMaxSize: 'small',
  //iconName:'',
  iconAlign:'left',
  iconColor:'Orange',
  iconOutlineColor:'red',
  //noValue:false,
  //attributes: {
    // attribute1: '',
    // attribute2: ''
  // }
}

 

AlexPage_0-1687276227897.png

Thanks Paul!

0 Kudos