2022年1月1日～2022年12月31日の会計年度の中に２つの期間の月が何か月含まれているか？


+ [
2つの日付間において今年度が何か月含まれるか計算したい](https://trailhead.salesforce.com/ja/trailblazer-community/feed/0D54S00000NGXAQSA5)


分かりやすいのはこの式ですが、大きすぎて保存できない
```
IF(MONTH(TODAY())<= 3,

IF(AND(StartDate__c >= DATE(YEAR(TODAY())-1,4,1),StartDate__c < DATE(YEAR(TODAY()),1,1) ,EndDate__c <  DATE(YEAR(TODAY()),1,1)), MONTH(EndDate__c) - MONTH(StartDate__c)+1,
IF(AND(StartDate__c >= DATE(YEAR(TODAY())-1,4,1),StartDate__c < DATE(YEAR(TODAY()),1,1) ,EndDate__c >= DATE(YEAR(TODAY()),1,1),EndDate__c <  DATE(YEAR(TODAY()),4,1)), MONTH(EndDate__c)+12 - MONTH(StartDate__c)+1,
IF(AND(StartDate__c >= DATE(YEAR(TODAY())-1,4,1),StartDate__c < DATE(YEAR(TODAY()),1,1) ,EndDate__c >= DATE(YEAR(TODAY()),1,1),EndDate__c >= DATE(YEAR(TODAY()),4,1)), 15 - MONTH(StartDate__c)+1,
IF(AND(StartDate__c >= DATE(YEAR(TODAY())-1,4,1),StartDate__c >= DATE(YEAR(TODAY()),1,1) ,EndDate__c <  DATE(YEAR(TODAY()),1,1)), MONTH(EndDate__c) - MONTH(StartDate__c)+1,
IF(AND(StartDate__c >= DATE(YEAR(TODAY())-1,4,1),StartDate__c >= DATE(YEAR(TODAY()),1,1) ,EndDate__c >= DATE(YEAR(TODAY()),1,1),EndDate__c <  DATE(YEAR(TODAY()),4,1)), MONTH(EndDate__c) - MONTH(StartDate__c)+1,
IF(AND(StartDate__c >= DATE(YEAR(TODAY())-1,4,1),StartDate__c >= DATE(YEAR(TODAY()),1,1) ,EndDate__c >= DATE(YEAR(TODAY()),1,1),EndDate__c >= DATE(YEAR(TODAY()),4,1)), 3 - MONTH(StartDate__c)+1,

IF(AND(StartDate__c <  DATE(YEAR(TODAY())-1,4,1) ,EndDate__c <  DATE(YEAR(TODAY()),1,1)), MONTH(EndDate__c)- 4 +1,
IF(AND(StartDate__c <  DATE(YEAR(TODAY())-1,4,1) ,EndDate__c >= DATE(YEAR(TODAY()),1,1),EndDate__c <  DATE(YEAR(TODAY()),4,1)), MONTH(EndDate__c)+12 -4 +1,
IF(AND(StartDate__c <  DATE(YEAR(TODAY())-1,4,1) ,EndDate__c >= DATE(YEAR(TODAY()),1,1),EndDate__c <  DATE(YEAR(TODAY()),4,1)), MONTH(EndDate__c)+12 -4 +1,
IF(AND(StartDate__c <  DATE(YEAR(TODAY())-1,4,1) ,EndDate__c >= DATE(YEAR(TODAY()),1,1),EndDate__c >= DATE(YEAR(TODAY()),4,1)), 12,
0))))))))))
,


IF(AND(StartDate__c >= DATE(YEAR(TODAY()),4,1) ,StartDate__c < DATE(YEAR(TODAY()+1),1,1),EndDate__c <  DATE(YEAR(TODAY())+1,1,1)), MONTH(EndDate__c) - MONTH(StartDate__c) +1,
IF(AND(StartDate__c >= DATE(YEAR(TODAY()),4,1) ,StartDate__c < DATE(YEAR(TODAY()+1),1,1),EndDate__c >= DATE(YEAR(TODAY())+1,1,1),EndDate__c <  DATE(YEAR(TODAY())+1,4,1)), MONTH(EndDate__c)+12 - MONTH(StartDate__c) +1,
IF(AND(StartDate__c >= DATE(YEAR(TODAY()),4,1) ,StartDate__c < DATE(YEAR(TODAY()+1),1,1),EndDate__c >= DATE(YEAR(TODAY())+1,1,1),EndDate__c >= DATE(YEAR(TODAY())+1,4,1)), 15 - MONTH(StartDate__c)+1,
IF(AND(StartDate__c >= DATE(YEAR(TODAY()),4,1) ,StartDate__c >= DATE(YEAR(TODAY()+1),1,1),EndDate__c <  DATE(YEAR(TODAY())+1,1,1)), MONTH(EndDate__c) - MONTH(StartDate__c) +1,
IF(AND(StartDate__c >= DATE(YEAR(TODAY()),4,1) ,StartDate__c >= DATE(YEAR(TODAY()+1),1,1),EndDate__c >= DATE(YEAR(TODAY())+1,1,1),EndDate__c <  DATE(YEAR(TODAY())+1,4,1)), MONTH(EndDate__c) - MONTH(StartDate__c) +1,
IF(AND(StartDate__c >= DATE(YEAR(TODAY()),4,1) ,StartDate__c >= DATE(YEAR(TODAY()+1),1,1),EndDate__c >= DATE(YEAR(TODAY())+1,1,1),EndDate__c >= DATE(YEAR(TODAY())+1,4,1)), 3 - MONTH(StartDate__c)+1,

IF(AND(StartDate__c <  DATE(YEAR(TODAY()),4,1) ,EndDate__c <  DATE(YEAR(TODAY())+1,1,1)),MONTH(EndDate__c)- 4 +1,
IF(AND(StartDate__c <  DATE(YEAR(TODAY()),4,1) ,EndDate__c >= DATE(YEAR(TODAY())+1,1,1),EndDate__c <  DATE(YEAR(TODAY())+1,4,1)), MONTH(EndDate__c)+12 -4+1,
IF(AND(StartDate__c <  DATE(YEAR(TODAY()),4,1) ,EndDate__c >= DATE(YEAR(TODAY())+1,1,1),EndDate__c <  DATE(YEAR(TODAY())+1,4,1)), MONTH(EndDate__c)+12 -4+1,
IF(AND(StartDate__c <  DATE(YEAR(TODAY()),4,1) ,EndDate__c >= DATE(YEAR(TODAY())+1,1,1),EndDate__c >= DATE(YEAR(TODAY())+1,4,1)), 12,
0))))))))))
)
```

重複部分を整理します。

```
IF(MONTH(TODAY())<= 3,
IF(StartDate__c >= DATE(YEAR(TODAY())-1,4,1) ,

IF(AND(StartDate__c < DATE(YEAR(TODAY()),1,1) ,EndDate__c <  DATE(YEAR(TODAY()),1,1)), MONTH(EndDate__c) - MONTH(StartDate__c)+1,
IF(AND(StartDate__c < DATE(YEAR(TODAY()),1,1) ,EndDate__c >= DATE(YEAR(TODAY()),1,1),EndDate__c <  DATE(YEAR(TODAY()),4,1)), MONTH(EndDate__c)+13 - MONTH(StartDate__c),
IF(AND(StartDate__c < DATE(YEAR(TODAY()),1,1) ,EndDate__c >= DATE(YEAR(TODAY()),1,1),EndDate__c >= DATE(YEAR(TODAY()),4,1)), 15 - MONTH(StartDate__c)+1,
IF(AND(StartDate__c >= DATE(YEAR(TODAY()),1,1) ,EndDate__c <  DATE(YEAR(TODAY()),1,1)), MONTH(EndDate__c) - MONTH(StartDate__c)+1,
IF(AND(StartDate__c >= DATE(YEAR(TODAY()),1,1) ,EndDate__c >= DATE(YEAR(TODAY()),1,1),EndDate__c <  DATE(YEAR(TODAY()),4,1)), MONTH(EndDate__c) - MONTH(StartDate__c)+1,
IF(AND(StartDate__c >= DATE(YEAR(TODAY()),1,1) ,EndDate__c >= DATE(YEAR(TODAY()),1,1),EndDate__c >= DATE(YEAR(TODAY()),4,1)), 3 - MONTH(StartDate__c)+1,
0))))))
,
IF(AND(EndDate__c <  DATE(YEAR(TODAY()),1,1)), MONTH(EndDate__c)- 3,
IF(AND(EndDate__c >= DATE(YEAR(TODAY()),1,1),EndDate__c <  DATE(YEAR(TODAY()),4,1)), MONTH(EndDate__c)+9,
IF(AND(EndDate__c >= DATE(YEAR(TODAY()),1,1),EndDate__c <  DATE(YEAR(TODAY()),4,1)), MONTH(EndDate__c)+9,
IF(AND(EndDate__c >= DATE(YEAR(TODAY()),1,1),EndDate__c >= DATE(YEAR(TODAY()),4,1)), 12,
0))))
),

IF(StartDate__c >= DATE(YEAR(TODAY())-1,4,1) ,

IF(AND(StartDate__c < DATE(YEAR(TODAY()+1),1,1),EndDate__c <  DATE(YEAR(TODAY())+1,1,1)), MONTH(EndDate__c) - MONTH(StartDate__c) +1,
IF(AND(StartDate__c < DATE(YEAR(TODAY()+1),1,1),EndDate__c >= DATE(YEAR(TODAY())+1,1,1),EndDate__c <  DATE(YEAR(TODAY())+1,4,1)), MONTH(EndDate__c)+13 - MONTH(StartDate__c) ,
IF(AND(StartDate__c < DATE(YEAR(TODAY()+1),1,1),EndDate__c >= DATE(YEAR(TODAY())+1,1,1),EndDate__c >= DATE(YEAR(TODAY())+1,4,1)), 15 - MONTH(StartDate__c)+1,
IF(AND(StartDate__c >= DATE(YEAR(TODAY()+1),1,1),EndDate__c <  DATE(YEAR(TODAY())+1,1,1)), MONTH(EndDate__c) - MONTH(StartDate__c) +1,
IF(AND(StartDate__c >= DATE(YEAR(TODAY()+1),1,1),EndDate__c >= DATE(YEAR(TODAY())+1,1,1),EndDate__c <  DATE(YEAR(TODAY())+1,4,1)), MONTH(EndDate__c) - MONTH(StartDate__c) +1,
IF(AND(StartDate__c >= DATE(YEAR(TODAY()+1),1,1),EndDate__c >= DATE(YEAR(TODAY())+1,1,1),EndDate__c >= DATE(YEAR(TODAY())+1,4,1)), 3 - MONTH(StartDate__c)+1,
0))))))
,
IF(AND(EndDate__c <  DATE(YEAR(TODAY())+1,1,1)),MONTH(EndDate__c)- 3,
IF(AND(EndDate__c >= DATE(YEAR(TODAY())+1,1,1),EndDate__c <  DATE(YEAR(TODAY())+1,4,1)), MONTH(EndDate__c)+9,
IF(AND(EndDate__c >= DATE(YEAR(TODAY())+1,1,1),EndDate__c <  DATE(YEAR(TODAY())+1,4,1)), MONTH(EndDate__c)+9,
IF(AND(EndDate__c >= DATE(YEAR(TODAY())+1,1,1),EndDate__c >= DATE(YEAR(TODAY())+1,4,1)), 12,
0))))
)
)
```