Date functions in IF Statement
Hi, I'm trying to get the previous month and previous year via a formula. Here is the formula I am trying to use:
=IF(MONTH(TODAY()) = "2", MONTH(TODAY()) - 1 + "/" + YEAR(TODAY()) - 1, "false")
It works when I remove the -1 from the YEAR(TODAY()) - 1 part, but when I add the -1 back in I get #INVALID OPERATION. Any ideas? Thanks!
Answers
-
Same thing happens if I try it outside of an IF Statement:
=MONTH(TODAY()) - 1 + "/" + YEAR(TODAY()) -1
-
Can you add some parentheses around the year part?
=MONTH(TODAY()) - 1 + "/" + (YEAR(TODAY()) - 1)
I think the formula might be getting confused after the + to concatenate text and then going back to doing number math (that's my totally non-technical explanation).
-
HI, Thanks! That works outside of the IF statement, but when I put it back in the IF statement I get the #INVALID OPERATION error again with or without the parentheses.
=IF(MONTH(TODAY()) = "2", MONTH(TODAY()) - 1 + "/" + YEAR(TODAY()) - 1), "false")
=IF(MONTH(TODAY()) = "2", MONTH(TODAY()) - 1 + "/" + YEAR(TODAY()) - 1, "false")
-
Did you try:
=IF(MONTH(TODAY()) = "2", MONTH(TODAY()) - 1 + "/" + (YEAR(TODAY()) - 1), "false")
😉
-
Boom! That did it. Missing parentheses... Thanks much!
-
No problem!
Keep an eye on those parentheses - small but mighty (and best kept in pairs).
Help Article Resources
Categories
- All Categories
- 14 Welcome to the Community
- Customer Resources
- 64.6K Get Help
- 433 Global Discussions
- 136 Industry Talk
- 465 Announcements
- 4.9K Ideas & Feature Requests
- 143 Brandfolder
- 147 Just for fun
- 63 Community Job Board
- 466 Show & Tell
- 32 Member Spotlight
- 2 SmartStories
- 298 Events
- 36 Webinars
- 7.3K Forum Archives
Check out the Formula Handbook template!