fields, general salesforce

How to get the MAX or Most Recent Date of several date fields in Salesforce Formula

Posted by SFDCr

Goal: Return the MAX or Most Recent Date of several dates

Recently I needed to get the most recent date of 4 separate date fields.  I created a new formula field and was shocked to find that MAX(custom_date_1,custom_date_2,custom_date_3,custom_date_4) did not work.  MYSQL allows MAX() to function for date fields and I was surprised it didn’t work here!

Workaround: MAX(DATE) Formula

The full formula example of the workaround is below.  An important aspect of the formula is to handle if any of the dates that are being compared are null.  Also remember to add DATEVALUE() for any fields that are of type DATETIME.

IF(
    AND(
        ISBLANK(custom_date_1),
        ISBLANK(custom_date_2),
        ISBLANK(custom_date_3),
        ISBLANK(custom_date_4)
    ),
    NULL,
    DATE(1900, 01, 01) +
    MAX(
        BLANKVALUE(
            custom_date_1, 
            DATE(1900, 01, 01)
        ) - DATE(1900, 01, 01),
        BLANKVALUE(
            custom_date_2,
            DATE(1900, 01, 01)
        ) - DATE(1900, 01, 01),
        BLANKVALUE(
            custom_date_3,
            DATE(1900, 01, 01)
        ) - DATE(1900, 01, 01),
        BLANKVALUE(
            custom_date_4,
            DATE(1900, 01, 01)
        ) - DATE(1900, 01, 01)
    )
)

I hope you find this helpful!

Related Post

5 thoughts on “How to get the MAX or Most Recent Date of several date fields in Salesforce Formula

  1. Austin

    Wow! This is great! Thank you so much! Would you be able to go line by line and just explain to us how everything is being calculated?

  2. Nick Lauricella

    I need this but I need it to return the field name. How can I do this?

  3. Suzzy

    very useful. Thank you for sharing

  4. Jennifer

    I’ve been looking for this forever it seems! I’ve usually had to use at least 2 fields or triggers to get this same effect.

    1. sfdcr_admin

      Glad you found this helpful! I am truly shocked Salesforce doesn’t have a better solution for this.

Leave a Reply to Suzzy Cancel reply