Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
mtrevisiol
Helper V
Helper V

Calendar with IF statement

Hi everyone.

I would like to create a Calendar table that contains all the dates of the previous 12 months + dates until today.

For example, if today is 3rd December 2021, the table would contain dates from 01/12/2020 to 03/12/2021.

 

Of course, if today is, for example, 05/01/2021, the table would contain dates from 01/12/2019 to 05/01/2021, so I need to turn back 2 years.

 

This is the formula I wrote:

MyCalendar =
var decreased_month = MONTH(TODAY())-1
RETURN
IF(decreased_month>0,
      CALENDAR(DATE(YEAR(TODAY())-1decreased_month1), NOW()),
      CALENDAR(DATE(YEAR(TODAY())-2decreased_month+121), NOW()))
 
but I get the message "The expression specified in the query is not a valid table expression."
 
Thank you in advance for help!
1 ACCEPTED SOLUTION
Samarth_18
Community Champion
Community Champion

Hi @mtrevisiol ,

 

Your question is not much clear to me but the code which you are trying needs to be written as mentioned below:

 

MyCalendar =
VAR decreased_month =
    MONTH ( TODAY () ) - 1
VAR start_date =
    IF (
        decreased_month > 0,
        DATE ( YEAR ( TODAY () ) - 1, decreased_month, 1 ),
        DATE ( YEAR ( TODAY () ) - 2, decreased_month + 12, 1 )
    )
RETURN
    CALENDAR ( start_date, TODAY () )

 Thanks,

Samarth

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

View solution in original post

1 REPLY 1
Samarth_18
Community Champion
Community Champion

Hi @mtrevisiol ,

 

Your question is not much clear to me but the code which you are trying needs to be written as mentioned below:

 

MyCalendar =
VAR decreased_month =
    MONTH ( TODAY () ) - 1
VAR start_date =
    IF (
        decreased_month > 0,
        DATE ( YEAR ( TODAY () ) - 1, decreased_month, 1 ),
        DATE ( YEAR ( TODAY () ) - 2, decreased_month + 12, 1 )
    )
RETURN
    CALENDAR ( start_date, TODAY () )

 Thanks,

Samarth

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors