Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Fearless Date Formulas: Leap Years and More by Beth Breisnes
1. Leap Years and More
@bethbrains
Beth Breisnes
Consultant, Bigger Boat Consulting
Fearless Date Formulas
2. Forward-Looking Statements
Statement under the Private Securities Litigation Reform Act of 1995:
This presentation may contain forward-looking statements that involve risks, uncertainties, and assumptions. If any such uncertainties materialize or if any
of the assumptions proves incorrect, the results of salesforce.com, inc. could differ materially from the results expressed or implied by the forward-looking
statements we make. All statements other than statements of historical fact could be deemed forward-looking, including any projections of product or
service availability, subscriber growth, earnings, revenues, or other financial items and any statements regarding strategies or plans of management for
future operations, statements of belief, any statements concerning new, planned, or upgraded services or technology developments and customer contracts
or use of our services.
The risks and uncertainties referred to above include – but are not limited to – risks associated with developing and delivering new functionality for our
service, new products and services, our new business model, our past operating losses, possible fluctuations in our operating results and rate of growth,
interruptions or delays in our Web hosting, breach of our security measures, the outcome of any litigation, risks associated with completed and any possible
mergers and acquisitions, the immature market in which we operate, our relatively limited operating history, our ability to expand, retain, and motivate our
employees and manage our growth, new releases of our service and successful customer deployment, our limited history reselling non-salesforce.com
products, and utilization and selling to larger enterprise customers. Further information on potential factors that could affect the financial results of
salesforce.com, inc. is included in our annual report on Form 10-K for the most recent fiscal year and in our quarterly report on Form 10-Q for the most
recent fiscal quarter. These documents and others containing important disclosures are available on the SEC Filings section of the Investor Information
section of our Web site.
Any unreleased services or features referenced in this or other presentations, press releases or public statements are not currently available and may not
be delivered on time or at all. Customers who purchase our services should make the purchase decisions based upon features that are currently available.
Salesforce.com, inc. assumes no obligation and does not intend to update these forward-looking statements.
3. Who cares about dates?
Birthdays
Membership dates
Expiration dates
Program dates
Anniversaries
4. Where we’re headed…
Key Date Functions
3 Key Considerations to avoid Leap Year errors
Deep Dive on Two Bulletproof Date Formulas
BOO!
20. The Big Trap: Leap Years
When do leap years happen?
You don’t
need to
know!
21. The Big Trap: Three Key Considerations
1. INPUTS
2. OUTPUTS
3. SPANS
22. Calculate Membership/Contract Expiry Date
IF ( MONTH (Join_Date__c) = 2 && DAY (Join_Date__c) = 29,
Join_Date__c + 365,
DATE( YEAR(Join_Date__c) + 1,
MONTH(Join_Date__c),
DAY (Join_Date__c))
-1
)
(add 1year-1day to a date)
Modified from Celeste Keller, Salesforce Saint
23. Calculate Birthday This Year… maybe…
DATE(YEAR(TODAY()), MONTH(Birthdate), DAY(Birthdate))
(SPOILER ALERT: THIS IS A HORRIBLE FORMULA. DON’T USE THIS.)
Find the month/day
Put it in this year
1. What if their birthday has
already passed this year?
2. What if they were born on a
leap day?
24. Calculate Next Birthday
IF ( MONTH (Birthdate) = 2 && DAY (Birthdate) = 29,
IF ( TODAY() < DATE ( YEAR ( TODAY() ), 1, 1 ) + 59,
DATE ( YEAR ( TODAY() ), 1, 1) + 59,
DATE ( YEAR ( TODAY() )+1, 1, 1) + 59),
IF ( TODAY() < DATE (YEAR(TODAY()),MONTH(Birthdate),DAY(Birthdate)),
DATE(YEAR(TODAY()),MONTH(Birthdate),DAY(Birthdate)),
DATE(YEAR(TODAY())+1,MONTH(Birthdate),DAY(Birthdate))
)
)
Key Takeaway:Salesforce is a publicly traded company. Please make your buying decisions only on the products commercially available from Salesforce.
Talk Track:
Before I begin, just a quick note that when considering future developments, whether by us or with any other solution provider, you should always base your purchasing decisions on what is currently available.
So, who cares about dates? How many of you have at least one custom date formula field in your org?
I thought so!
Organizations might care about Birthdays, membership dates, expiry dates, program dates, anniversaries! The list goes on and on. Dates are cirtical pieces of data in all of our systems, so date formulas are really important for you to be confident and comfortable with as awesome admins.
First, a quick roadmap.
We’re gonna do a review of the key date functions that you use in date formulas.
Then we’ll look at the three key considerations you need to make to avoid date formula errors
And finally, we’ll take a deep dive on two bulletproof date formulas you can go use in your orgs right away.
And don’t bother taking notes because I’ll be posting these formulas in an unmanaged package so you can install them straight into your sandbox or dev org. I’ll put the shortlink up at the end.
So, did you all know it’s Astro’s birthday?
I got Astro a present. It’s a bit nerdy. Are you ready to see it?
It’s a date function machine!
This fancy machine takes in inputs and gives outputs, or results.
So you know when you create a custom field, the first thing it asks you is what data type you want? That’s what we’re talking about here. An input of a certain data type, and then an output of a certain data type. OK?
Let’s read the instructions and see how it works!
The DAY function takes in a Date and spits out a Number.
The MONTH function takes in a Date and spits out a Number.
The YEAR function takes in a Date and spits out a Number.
Trick question – the TODAY function takes NOTHING, but needs the parentheses, and spits out a Date.
Trick question – the NOW function takes NOTHING, but needs the parentheses, and spits out a DateTime.
The YEAR function takes in a Date and spits out a Number.
The YEAR function takes in a Date and spits out a Number.
So now that we know how it works, let’s take it for a spin. If we put this in a formula, what’s it going to return?
So now that we know how it works, let’s take it for a spin. If we put this in a formula, what’s it going to return?
And how about this one? Awesome! Now that you’re all experts on date functions, let’s get to the formulas.
You may be thinking, but Beth! Date math is easy!
Yes, adding a simple number of dates like this IS easy. But what about something more complex, like an contract expiry date?
You might expect to just add 364. Well this works SOME years…
But what if this were October 1, 2015? What would the expiry date be? September 29! Well what happened to September 30? It got eaten by the leap year.
When do leap years happen? [wait for some answers]
Actually, you know what? There’s a secret It doesn’t matter. You don’t need to know. We’ll get there in a few minutes.
First, I have another secret for you…
So let’s look at a better Membership Expiry Date formula that accounts for these things.
Let’s break this down.
Right off the bat we’re allowing for the input date to be a leap day. If you joined on a leap day,
Forward 365, which takes you to February 28 of the following year.
Otherwise, same date next year and (4) back up a calendar day. – This allows for leap years because it doesn’t actually need to know the length of the year.
Way to go! That is a rock solid formula, with some patterns you can apply to lots of date-related formulas in your orgs.
OK, now Chris Duarte is telling us that today isn’t actually Astro’s birthday, but Astro’s birth date IS on their record in Salesforce. Let’s come up with a formula to calculate their next birthday.
You MIGHT think you just need to find the month and day and throw it into this year, and voila!
But what if their birthday has already passed this year? Then you're not getting their next birthday, you're getting their last birthday. And don't forget the big trap -- what if they were born on a leap year, but this year isn't a leap year? If you try to return February 29th in a common year, you'll get a big fat error.
Calculate NEXT Birthday is a much better way of thinking about what you are actually after.
This formula might look intimidating, but let's break it down. We're using some nested IF() functions. So we start with If the month of their birthday is 2, and the day of their birthday is 29 -- oh, if they're a leap year baby! OK, if they're a leap year baby, we're going to check something else. Is the 59th day of this year still greater than today, or in the future? The 59th day of the year is a great way of thinking about this, because it means we don't care if it's a leap year this year or not. We're checking for February 29 or March 1, it doesn't matter. See? I told you you didn’t need to worry about when leap years happen! So if we know they were born on Feb 29, we are basically checking if their birthday has passed or not. If it hasn't, then their next birthday is the 59th day of this year, whether that be Feb 29 or March 1. If their birthday has passed, then their next birthday is the 59th day of next year.
Now we jump back out to our first IF statement for the "Else" condition, so if they are not a leap year baby.
This part uses similar logic -- are we before their birthday in this year? If so, then their next birthday is the day of their birthday in this year. If their birthday this year has passed, then their next birthday is the day of their birthday next year.
You made it through! You calculated your Astro’s next birthday and avoided the leap year trap!
If they were born on a leap day,
Are we still before feb 29/mar 1?
If so, Feb 29/Mar 1 of this year is their next birthday.If not, Feb 29/Mar 1 of NEXT year is their next birthday.
If they weren’t born on a leap day, are we still before their next birthday?
If so, their birthday’s month and day this year is their next birthday.If not, their birthday’s month and day NEXT year is their next birthday.
You are now ready to slay date formulas and never run into leap year errors! Just remember to always consider your inputs, outputs, and time spans, and you’ll be Leap Year Approved in no time!
Find me on Twitter.