[SalesForce] comparing DateTime value in formula

I am trying to create a formula field to set a DateTime value, but it fails with the following error:

Error: Incorrect parameter type for function 'IF()'. Expected Number, received DateTime

The formula I am trying to construct is as follows:

IF(MQL_Date__c < First_Sales_Activity_Logged__c, First_Sales_Activity_Logged__c-MQL_Date__c, MQL_Date__c)


MQL_Date__c - Date/Time
First_Sales_Activity_Logged__c - Date/Time
Formula Field - Date/Time

Only if MQL_Date__c is less than First_Sales_Activity_Logged__c than set the formula value to First_Sales_Activity_Logged__c-MQL_Date__c else MQL_Date__c

Best Answer

You cannot return a DateTime by subtracting two DateTime fields, it will always return you the difference in number of days.

Also, your formula should return the value of same type in all the conditions, i.e. in your case, it is returning Integer in one case and DateTime in another case which is incorrect.

If you want to display the difference between two DateTime fields, you will have to change your formula return type to either Number or Text and show the difference.