I am trying to determine the variance (in months) between an actual date and a planned date.
The actual date can be either before or after the planned date, and this is what is making this difficult.
I have the following formula to determine the difference in months
TEXT(
IF(NOT(ISBLANK(Reference_Data_Item__c)),
(((YEAR(Stage_6_Complete__c) - YEAR(Stage_6_Complete_DB_Forecast_Date__c) - 1) * 12)
+
(12 - MONTH(Stage_6_Complete_DB_Forecast_Date__c) + 1)
+
MONTH(Stage_6_Complete__c)
- 1), 0)) & " Months"
This formula works fine for actual dates that end up being passed the planned date, but if the actual date ends before the planned date the variance has an extra 12 months added to it.
I would normally simply add a check to see which date comes first, but this is pushing me over the 5000 character limit salesforce imposes.
Does anyone have any ideas how I can get the difference while staying under the 5000 limit?
ABS(Date1-Date2)/30
might do the trick close enoughABS(Date1-Date2) / ( 365 / 12 )
date1Var.monthsBetween(date2Var);
. I know, I know, clicks before code. However, you should weigh the costs and benefits of each approach (is not needing to write a unit test worth the extra effort that you'd need to put forth to develop an accurate formula?)