The Official Salesforce Blog

The Official Salesforce Blog

Creating a Custom Formula to Calculate Age

Eric Bezar Jun 27, 2005

Days_open

When working with date fields there’s often a need to calculate the number of days since a particular event took place. For example, if you run a support organization you might want to calculate the number of days it takes to resolve a case.

This is a little bit tricky because if the case is still open you’ll want to calculate the difference between the create date and the current date. If the date is closed, it’s the difference between the create date and the close date.

The formula field below accounts for both situations and even rounds down to the nearest integer. The end result is a simple standardized number which is easy to work with in reports and dashboards.

Formula:
IF( {!IsClosed} , ROUND({!ClosedDate} - {!CreatedDate}, 0) ,
ROUND((NOW() - {!CreatedDate}),0))

  • This formula uses the special Boolean field {!IsClosed} to determine whether the case is open or closed.  Note that when you use a Boolean field in an IF function, you do not need to compare it to TRUE or FALSE.
  • {!CreatedDate} and {!ClosedDate} are both date/time fields, so you can use them together with the NOW() function in subtraction operations.
  • The ROUND function is used to round off the decimal portion of the resulting duration in days.

Note that this is general approach and could be used for calculating age of any type of data -- Leads, Accounts, custom objects, and so on.

 

21 Comments

spazure

Although all Date and Date/Time calculations return a value in days, you can easily modify your Case Age formula to display time information in minutes instead of days. If you multiply the number of days by 24, then multiply that number by 60, you will have that information displayed in minutes instead of days.

To do this, you'll just need to take the two calculations and add in the appropriate mathematical operators.

IF( {!IsClosed} , ROUND(

(({!ClosedDate} - {!CreatedDate}) * 24) * 60, 0) ,

ROUND((

((NOW() - {!CreatedDate}) * 24) * 60),0))

For best results when creating this formula, I'd suggest that you set the formula return type as "Number", and select decimal places as "0".

Dave

Within the lead page, how do i calculate age of lead if there is no "Create date" field. Is there a way to extract the date from the "created by" field.

Eric Bezar

In the formula wizard, you should have access to the {!CreatedDate} field on Lead. I just tried it myself, and it worked.

Try using this formula:
ROUND( NOW() - {!CreatedDate}, 0)


Jason

Is there a way to calculate age in business days? We want to track projects through different steps in a process, with the duration of each step in business days only.

Shawn

Is there a way to extract the time from the CreatedDate field? I am interested in reporting on events that have been created between 09:00 and 10:00 each day.

Mark Bruso

We need to calculate ages on reports but not on a summary but rather on a record by record basis. It appears that we need to do create formula fields on the object and then we can display these fields on a report. Is this correct?

Jan

I want to either create a Task or send a Reminder email to the Owner if an Opportunity was created 7 days ago and has not changed Stage.

Doesn't look I can create a rule to do this. Any ideas?

Persio Afonso

Is there any way to calculate the opportunity aging in a specific sales stage?

Persio Afonso

How can I calculate how many stages an opportunity has been through in my pipeline?
The reason I asked this is that such metric is quickly becoming a great way of avoiding the famous "miracle effect" by ensuring deals are not entered in a pipeline just a few days before they are closed.

John Johnson

Is there a way to calculate age or number of days between a date/time field and a date field?

Kingsley

@ John: This is a thread in the developer forums about this. Let me know if you have problems accessing that link. You can also check this out: http://www.ihatethebus.com/blog/index.php?entry=entry050831-144419

May

How to calculate the number of days it takes to resolve a case without the weeekend and public holiday?

Denise Mcdonough

I need to calculate number of minutes a case is open, per business day (from 9AM to 6 PM EST Monday - Friday).

Can we figure this out??

Avi

We would like to monitor and adjust the initially assumed "Probablity (%)" on each Stage by calculating its actual real value over a period of time.
Is it feasible to calculate it?
Thanks

Scott Koenigsberg

Is there a way to calculate business days between two dates - similar to the NETWORKDAYS function on Excel?

Avi

Opportunity Age (days) formula : what am I doing wrong?
I selected Formula Return Type as Number with Decimal places = 0:
IF( {!IsClosed} , {!CloseDate} - {!CreatedDate} , NOW() - {!CreatedDate})
and I get:
"Error: Incorrect parameter for function -. Expected Number, Date, received DateTime"

Eric Bezar

Scott -- the problem with your formula is that CloseDate is a date field and CreatedDate is actually a datetime field. You can only do data arithmetic when both fields are the same type. The good news is that in Winter '07, there will be a DATEVALUE conversion function that allows you to convert a datetime to a date field.

louise

i'm looking for one or more formulas to work out the reading age of a piece of text for my maths coursework. is there any chance of finding out what they are?

alex

On personal opinion, I find this very helpful.
Guys, I have also posted some more relevant info further on this, not sure if you find it useful: http://www.bidmaxhost.com/forum/

shafiq

just amazing

Jeff Ku

I did a work around for networkdays

IF(MOD( Final_Q_to_Ops__c - DATE(1900, 1, 6), 7) + MAX( Programming_Days__c , Graphic_Development_Days__c )<7, Final_Q_to_Ops__c+MAX( Programming_Days__c , Graphic_Development_Days__c ),Final_Q_to_Ops__c+(MAX( Programming_Days__c , Graphic_Development_Days__c )+(ROUND(MAX( Programming_Days__c , Graphic_Development_Days__c )/5,0)*2)))

Post a comment