Bucketing by Hour (Not Day)

For reports that need to bucket by hours—not days, weeks, or months—create a formula like this.

  • It returns a date+time string in 'YY: MM/DD HHh format (like '22: 03/23 09h), which is the longest string I could fit in a graph label without Salesforce truncating it.
  • It uses the American schedule for Daylight Saving. I haven’t yet tweaked it for European Daylight Saving, which diverges from American DS for about six weeks each year
  • It’s formatted for the the United States’ Central time zone—if you’re in a different US timezone, tweak the 5/24 and 6/24 terms
IF(
    DATEVALUE(CreatedDate) >= DATE(YEAR(DATEVALUE(CreatedDate)), 3, 1) + 
    (14 - CASE(MOD(DATE(YEAR(DATEVALUE(CreatedDate)), 3, 1) - 
    DATE(1900, 1, 7), 7), 0, 7, MOD(DATE(YEAR(DATEVALUE(CreatedDate)), 3, 1) - DATE(1900, 1, 7), 7)))
    &&
    DATEVALUE(CreatedDate) < DATE(YEAR(DATEVALUE(CreatedDate)), 11, 1) + 
    (7 - CASE(MOD(DATE(YEAR(DATEVALUE(CreatedDate)), 11, 1) - 
    DATE(1900, 1, 7), 7), 0, 7, MOD(DATE(YEAR(DATEVALUE(CreatedDate)), 11, 1) - DATE(1900, 1, 7), 7))),
    
    "'" & MID(TEXT(CreatedDate - 5/24), 3, 2) & ": " & 
        MID(TEXT(CreatedDate - 5/24), 6, 2) & "/" & MID(TEXT(CreatedDate - 5/24), 9, 2) & " " & 
            MID(TEXT(CreatedDate - 5/24), 12, 2),
    
    "'" & MID(TEXT(CreatedDate - 6/24), 3, 2) & ": " & 
        MID(TEXT(CreatedDate - 6/24), 6, 2) & "/" & MID(TEXT(CreatedDate - 6/24), 9, 2) & " " & 
            MID(TEXT(CreatedDate - 6/24), 12, 2)
) & "h"

The first clause in the huge IF statement determines whether we’re on Daylight Saving Time

The second clause returns the Daylight-Saving-formatted version of the date/time

The third clause returns the non-Daylight-Saving-formatted version of the date/time

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s