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.9K Get Help
- 441 Global Discussions
- 139 Industry Talk
- 471 Announcements
- 4.9K Ideas & Feature Requests
- 129 Brandfolder
- 148 Just for fun
- 68 Community Job Board
- 496 Show & Tell
- 33 Member Spotlight
- 2 SmartStories
- 300 Events
- 36 Webinars
- 7.3K Forum Archives
Check out the Formula Handbook template!