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
- Smartsheet Customer Resources
- 63.5K Get Help
- 402 Global Discussions
- 213 Industry Talk
- 450 Announcements
- 4.7K Ideas & Feature Requests
- 141 Brandfolder
- 135 Just for fun
- 56 Community Job Board
- 454 Show & Tell
- 31 Member Spotlight
- 1 SmartStories
- 296 Events
- 36 Webinars
- 7.3K Forum Archives
Check out the Formula Handbook template!