Arcade line to remove NaN value

482
3
Jump to solution
02-29-2024 09:50 AM
APBlough
New Contributor III

This maybe a simple snippet, but I cannot get it to work. I have a small arcade line on my dashboard list to calculate the time that gas was turned off. Simpily subtracts the On time from the Off time. 

But, when a service is never turned off, it has nothing in these fields to subtract and results in a 'NaN' I wanted to add an iif statement that takes these NaN's and either leaves them blank and displays nothing or more ideally comes back with 'Zero'

But since it is a NaN result and not something that I can add an If Blank, I am struggling to figure out how this can be done. 

 

Any help would be amazing! Thanks. 

 

Attached: List view from dashboard and the snippet of the arcade 

 

Arcade Text:

var Status = $datapoint.OutageStatus
var Off = $datapoint.DateOff
var On = $datapoint.DateRelightComplete
var DaysOff = DateDiff (On, Off, 'hours')
var HoursOff = Round(DaysOff,1)

return {
textColor: '',
backgroundColor: '',
separatorColor:'',
selectionColor: '',
selectionTextColor: '',
attributes: {
HoursOff:HoursOff,
}
}

 

Line item template:

({expression/HoursOff} Hours without Gas)

-APB
0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

You can wrap the whole expression in an Iif function, and return something else when there's no value. And you can actual build the entire text string in the expression as well.

var Off = $datapoint.DateOff
var On  = $datapoint.DateRelightComplete

var HoursOff = Iif(
  IsEmpty(Off), // check if field is empty
  'System Never Turned Off',
  `(${Round(DateDiff(On, Off, 'hours'), 1)} Hours without Gas)`
)

return {
  attributes: { HoursOff }
}

 

Then your line item template can just be {expression/HoursOff} .

- Josh Carlson
Kendall County GIS

View solution in original post

3 Replies
KenBuja
MVP Esteemed Contributor

You can check if those values are empty before doing your calculations

 

var Status = $datapoint.OutageStatus;
var Off = $datapoint.DateOff;
var On = $datapoint.DateRelightComplete;
var HoursOff;
if (IsEmpty(On) || IsEmtpy(Off)) {
  HoursOff = 0;
} else {
  HoursOff = Round(DateDiff(On, Off, 'hours'), 1);
}

return {
  textColor: '',
  backgroundColor: '',
  separatorColor:'',
  selectionColor: '',
  selectionTextColor: '',
  attributes: {
    HoursOff:HoursOff,
  }
}

or you can use the IsNan function afterward

var Status = $datapoint.OutageStatus
var Off = $datapoint.DateOff
var On = $datapoint.DateRelightComplete
var DaysOff = DateDiff (On, Off, 'hours')
var HoursOff = Round(DaysOff,1)
if (IsNan(HoursOff)) HoursOff = 0;

 

jcarlson
MVP Esteemed Contributor

You can wrap the whole expression in an Iif function, and return something else when there's no value. And you can actual build the entire text string in the expression as well.

var Off = $datapoint.DateOff
var On  = $datapoint.DateRelightComplete

var HoursOff = Iif(
  IsEmpty(Off), // check if field is empty
  'System Never Turned Off',
  `(${Round(DateDiff(On, Off, 'hours'), 1)} Hours without Gas)`
)

return {
  attributes: { HoursOff }
}

 

Then your line item template can just be {expression/HoursOff} .

- Josh Carlson
Kendall County GIS
APBlough
New Contributor III

That's perfect! I was trying to get the iif statement in the wrong spot. This also helps me understand how to simplify my arcade formatting. Thank you so much for the help!

-APB
0 Kudos