Hello,
I'm wanting to use an IF statement symbolize and add another field for Fiscal Year. What I'm trying to do is code that new 'Fiscal Year' field based on a range of dates from an existing date field. Below is what I have, though it is returning all 'N/A' values, so the logical '&&' operator doesn't appear to be selecting the dates properly. I will say too that I'm a novice at programming, so there may be a stupidly easy syntax solution for this, and I will be humbly grateful if there is one!
if ($feature.Projected_Date_to_Open >= 7/1/2019 && $feature.Projected_Date_to_Open <= 6/30/2020){
return 'FY 19/20';
} else if ($feature.Projected_Date_to_Open >= 7/1/2020 && $feature.Projected_Date_to_Open <= 6/30/2021) {
return 'FY 20/21';
} else if ($feature.Projected_Date_to_Open >= 7/1/2021 && $feature.Projected_Date_to_Open <= 6/30/2022) {
return 'FY 21/22';
} else {
return 'N/A';
}
Solved! Go to Solution.
When you want to compare dates, you have to provide a Date object. Your code would look like this
if ($feature.Projected_Date_to_Open >= Date(2019,6,1) && $feature.Projected_Date_to_Open <= Date(2020,5,30){ //months are 0-based, where 0 is January and 11 is December
Hi @DanielHoffman73 ,
In addition to what @KenBuja mentioned, it is advised that you use fields that contain underscores in a different way. See the corrected code below:
if ($feature["Projected_Date_to_Open"] >= Date(2019, 6, 1) && $feature["Projected_Date_to_Open"] <= Date(2020, 5, 30)) {
return 'FY 19/20';
} else if ($feature["Projected_Date_to_Open"] >= Date(2020, 6, 1) && $feature["Projected_Date_to_Open"] <= Date(2021, 5, 30)) {
return 'FY 20/21';
} else if ($feature["Projected_Date_to_Open"] >= Date(2021, 6, 1) && $feature["Projected_Date_to_Open"] <= Date(2022, 5, 30)) {
return 'FY 21/22';
} else {
return 'N/A';
}
When you want to compare dates, you have to provide a Date object. Your code would look like this
if ($feature.Projected_Date_to_Open >= Date(2019,6,1) && $feature.Projected_Date_to_Open <= Date(2020,5,30){ //months are 0-based, where 0 is January and 11 is December
Hi @DanielHoffman73 ,
In addition to what @KenBuja mentioned, it is advised that you use fields that contain underscores in a different way. See the corrected code below:
if ($feature["Projected_Date_to_Open"] >= Date(2019, 6, 1) && $feature["Projected_Date_to_Open"] <= Date(2020, 5, 30)) {
return 'FY 19/20';
} else if ($feature["Projected_Date_to_Open"] >= Date(2020, 6, 1) && $feature["Projected_Date_to_Open"] <= Date(2021, 5, 30)) {
return 'FY 20/21';
} else if ($feature["Projected_Date_to_Open"] >= Date(2021, 6, 1) && $feature["Projected_Date_to_Open"] <= Date(2022, 5, 30)) {
return 'FY 21/22';
} else {
return 'N/A';
}
Excellent. Thanks Ken and Xander for your quick responses! Totally works now. Good to know about the date formatting and syntax suggestions as well.
Glad to help. Don't forget to click the "Accept as Solution" button.