I created a formula with nested if statements. Since the conditions are similar, is there a way to write it elegantly?
Fiscal_Year__c is a picklist field with values such as 20-21, 21-22, 22-23 etc.
Fiscal Year is as follows: July 1, 2021 - June 30, 2022 = FY 21-22 July 1, 2022 - June 30, 2023 = FY 22-23
We are comparing it against current fiscal year to give it a dynamic name such as "Two FY Ago", "Last FY", "This FY" and "Next FY" for better reporting since those fiscal year records do not have fields that can allow relative date ranges.
IF(
AND(
VALUE(TEXT(MONTH(TODAY()))) < 7,
TEXT(Fiscal_Year__c) = TRIM(RIGHT(TEXT(YEAR(TODAY())-3), 2)) & "-" & TRIM(RIGHT(TEXT(YEAR(TODAY())-2), 2))),
"Two FY Ago",
/*Current FY = 21-22, Two FY Ago = 22-3=19, 22-2=20, yields 19-20.*/
IF(AND(
VALUE(TEXT(MONTH(TODAY()))) < 7,
TEXT(Fiscal_Year__c) = TRIM(RIGHT(TEXT(YEAR(TODAY())-2), 2)) & "-" & TRIM(RIGHT(TEXT(YEAR(TODAY())-1), 2))),
"Last FY",
/*Current FY = 21-22, One FY Ago/Last FY = 22-2 =20, 22-1=21, yields 20-21.*/
IF(AND(
VALUE(TEXT(MONTH(TODAY()))) < 7,
TEXT(Fiscal_Year__c) = TRIM(RIGHT(TEXT(YEAR(TODAY())-1), 2)) & "-" & TRIM(RIGHT(TEXT(YEAR(TODAY())), 2))),
"Current FY",
/*Current FY = 21-22, Current FY = 22-1 = 21, 22=22, yields 21-22.*/
IF(AND(
VALUE(TEXT(MONTH(TODAY()))) < 7,
TEXT(Fiscal_Year__c) = TRIM(RIGHT(TEXT(YEAR(TODAY())), 2)) & "-" & TRIM(RIGHT(TEXT(YEAR(TODAY())+1), 2))),
"Next FY",
/*Current FY = 21-22, Next FY = 22=22, 22+1=23, yields 22-23.*/
IF(
AND(
VALUE(TEXT(MONTH(TODAY()))) > 6,
TEXT(Fiscal_Year__c) = TRIM(RIGHT(TEXT(YEAR(TODAY())-2), 2)) & "-" & TRIM(RIGHT(TEXT(YEAR(TODAY())-1), 2))),
"Two FY Ago",
/*Current FY = 22-23, Two FY Ago = 22-2=20, 22-1=21, yields 20-21.*/
IF(AND(
VALUE(TEXT(MONTH(TODAY()))) > 6,
TEXT(Fiscal_Year__c) = TRIM(RIGHT(TEXT(YEAR(TODAY())-1), 2)) & "-" & TRIM(RIGHT(TEXT(YEAR(TODAY())), 2))),
"Last FY",
/*Current FY = 22-23, One FY Ago/Last FY = 22-1 =21, 22=22, yields 21-22.*/
IF(AND(
VALUE(TEXT(MONTH(TODAY()))) > 6,
TEXT(Fiscal_Year__c) = TRIM(RIGHT(TEXT(YEAR(TODAY())), 2)) & "-" & TRIM(RIGHT(TEXT(YEAR(TODAY())+1), 2))),
"Current FY",
/*Current FY = 22-23, Current FY = 22-23, 22=22, 22+1=23, yields 22-23.*/
IF(AND(
VALUE(TEXT(MONTH(TODAY()))) > 6,
TEXT(Fiscal_Year__c) = TRIM(RIGHT(TEXT(YEAR(TODAY())+1), 2)) & "-" & TRIM(RIGHT(TEXT(YEAR(TODAY())+2), 2))),
"Next FY",
/*Current FY = 22-23, Next FY = 23-24, 22+1=23, 22+2=24, yields 23-24.*/
""))))
))))