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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
dyabes
Advocate I
Advocate I

Error in calculating for Previous Period when using custom date period selection

Hope you are all having a great day!

I am trying to implement this great idea from theh BI Elite team which is using a custom date period selection (MTD, YTD, QTD...), instead of the standard slicer for calendar picking the start date and end date. All is well and good. I wanted then to implement a calculation of Previous Period (adjacent prior period) and Previos Year (same dates last year) based on the date period selection.

I would then plot these measures to compare trends. 

 

I am having trouble bringing back the row context for dates. Can someone help me?

Here is a link to the PBIX file 

 

Thank you in advance!

 

Cheers,

David

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@dyabes Here are some typical date intelligence measures written in the form of Calculation groups (below) These will work with calendar dates or fiscal dates. You may find this helpful - https://community.powerbi.com/t5/Community-Blog/To-bleep-With-Time-Intelligence/ba-p/1260000

 

Also, see if my Time Intelligence the Hard Way provides a different way of accomplishing what you are going for.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TIT...

 

YTD

CALCULATE(SELECTEDMEASURE(),

    FILTER(ALL('Dates'),

         'Dates'[CalendarYear] = MAX('Dates'[CalendarYear]) &&

         'Dates'[Date] <= MAX('Dates'[Date])))

 

MTD

CALCULATE(SELECTEDMEASURE(),

    FILTER(ALL('Dates'),

        'Dates'[CalendarYear] = MAX('Dates'[CalendarYear]) &&

        'Dates'[MonthNumberOfYear] = MAX('Dates'[MonthNumberOfYear])

 && 'Dates'[Date] <= MAX('Dates'[Date])))

 

QTD

CALCULATE(SELECTEDMEASURE(),

    FILTER(ALL('Dates'),

        'Dates'[CalendarYear] = MAX('Dates'[CalendarYear]) &&

        'Dates'[CalendarQuarter] = MAX('Dates'[CalendarQuarter]) &&

        'Dates'[Date] <= MAX('Dates'[Date])))

 

PY

VAR __MaxDate = MAX('Dates'[Date])

VAR __MinDate = MIN('Dates'[Date])

RETURN

    CALCULATE(SELECTEDMEASURE(),

        FILTER(ALL('Dates'),

            'Dates'[Date] >=

                DATE(YEAR(__MinDate)-1,MONTH(__MinDate),DAY(__MinDate)) &&

            'Dates'[Date] <=

                DATE(YEAR(__MaxDate)-1,MONTH(__MaxDate),DAY(__MaxDate))))

 

PY YTD

VAR __MaxDate = MAX('Dates'[Date])

RETURN

    CALCULATE(SELECTEDMEASURE(),

        FILTER(ALL('Dates'),

            'Dates'[CalendarYear] =

                MAX('Order Dates'[CalendarYear])-1 &&

            'Dates'[Date] <=

                DATE(

                    YEAR(__MaxDate)-1, 

                    MONTH(__MaxDate),

                    DAY(__MaxDate))))

 

YOY

SELECTEDMEASURE()

    - CALCULATE(SELECTEDMEASURE(),'Date Intelligence Group'[Name] = "PY")

 

YOY %

DIVIDE(

    CALCULATE(SELECTEDMEASURE(),'Date Intelligence Group'[Name] ="YOY"),

    CALCULATE(SELECTEDMEASURE(),'Date Intelligence Group'[Name] ="PY"))

 

Equivalent measures using DAX Time Intelligence functions are the following:

  • Current: SELECTEDMEASURE()
  • YTD: CALCULATE(SELECTEDMEASURE(), DATESYTD('Order Dates'[Date]))
  • MTD: CALCULATE(SELECTEDMEASURE(), DATESMTD('Order Dates'[Date]))
  • QTD: CALCULATE(SELECTEDMEASURE(), DATESQTD('Order Dates'[Date]))
  • PY: CALCULATE(SELECTEDMEASURE(), SAMEPERIODLASTYEAR('Order Dates'[Date]))
  • PY YTD: CALCULATE(SELECTEDMEASURE(),SAMEPERIODLASTYEAR('Order Dates'[Date]),'Time Intelligence Group'[Name]= "YTD")
  • YOY: SELECTEDMEASURE()-CALCULATE(SELECTEDMEASURE(),'Time Intelligence Group'[Name] = "PY")
  • YOY %: DIVIDE(CALCULATE(SELECTEDMEASURE(),'Time Intelligence Group'[Name] ="YOY"),CALCULATE(SELECTEDMEASURE(),'Time Intelligence Group'[Name] ="PY"))


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

2 REPLIES 2
Greg_Deckler
Super User
Super User

@dyabes Here are some typical date intelligence measures written in the form of Calculation groups (below) These will work with calendar dates or fiscal dates. You may find this helpful - https://community.powerbi.com/t5/Community-Blog/To-bleep-With-Time-Intelligence/ba-p/1260000

 

Also, see if my Time Intelligence the Hard Way provides a different way of accomplishing what you are going for.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TIT...

 

YTD

CALCULATE(SELECTEDMEASURE(),

    FILTER(ALL('Dates'),

         'Dates'[CalendarYear] = MAX('Dates'[CalendarYear]) &&

         'Dates'[Date] <= MAX('Dates'[Date])))

 

MTD

CALCULATE(SELECTEDMEASURE(),

    FILTER(ALL('Dates'),

        'Dates'[CalendarYear] = MAX('Dates'[CalendarYear]) &&

        'Dates'[MonthNumberOfYear] = MAX('Dates'[MonthNumberOfYear])

 && 'Dates'[Date] <= MAX('Dates'[Date])))

 

QTD

CALCULATE(SELECTEDMEASURE(),

    FILTER(ALL('Dates'),

        'Dates'[CalendarYear] = MAX('Dates'[CalendarYear]) &&

        'Dates'[CalendarQuarter] = MAX('Dates'[CalendarQuarter]) &&

        'Dates'[Date] <= MAX('Dates'[Date])))

 

PY

VAR __MaxDate = MAX('Dates'[Date])

VAR __MinDate = MIN('Dates'[Date])

RETURN

    CALCULATE(SELECTEDMEASURE(),

        FILTER(ALL('Dates'),

            'Dates'[Date] >=

                DATE(YEAR(__MinDate)-1,MONTH(__MinDate),DAY(__MinDate)) &&

            'Dates'[Date] <=

                DATE(YEAR(__MaxDate)-1,MONTH(__MaxDate),DAY(__MaxDate))))

 

PY YTD

VAR __MaxDate = MAX('Dates'[Date])

RETURN

    CALCULATE(SELECTEDMEASURE(),

        FILTER(ALL('Dates'),

            'Dates'[CalendarYear] =

                MAX('Order Dates'[CalendarYear])-1 &&

            'Dates'[Date] <=

                DATE(

                    YEAR(__MaxDate)-1, 

                    MONTH(__MaxDate),

                    DAY(__MaxDate))))

 

YOY

SELECTEDMEASURE()

    - CALCULATE(SELECTEDMEASURE(),'Date Intelligence Group'[Name] = "PY")

 

YOY %

DIVIDE(

    CALCULATE(SELECTEDMEASURE(),'Date Intelligence Group'[Name] ="YOY"),

    CALCULATE(SELECTEDMEASURE(),'Date Intelligence Group'[Name] ="PY"))

 

Equivalent measures using DAX Time Intelligence functions are the following:

  • Current: SELECTEDMEASURE()
  • YTD: CALCULATE(SELECTEDMEASURE(), DATESYTD('Order Dates'[Date]))
  • MTD: CALCULATE(SELECTEDMEASURE(), DATESMTD('Order Dates'[Date]))
  • QTD: CALCULATE(SELECTEDMEASURE(), DATESQTD('Order Dates'[Date]))
  • PY: CALCULATE(SELECTEDMEASURE(), SAMEPERIODLASTYEAR('Order Dates'[Date]))
  • PY YTD: CALCULATE(SELECTEDMEASURE(),SAMEPERIODLASTYEAR('Order Dates'[Date]),'Time Intelligence Group'[Name]= "YTD")
  • YOY: SELECTEDMEASURE()-CALCULATE(SELECTEDMEASURE(),'Time Intelligence Group'[Name] = "PY")
  • YOY %: DIVIDE(CALCULATE(SELECTEDMEASURE(),'Time Intelligence Group'[Name] ="YOY"),CALCULATE(SELECTEDMEASURE(),'Time Intelligence Group'[Name] ="PY"))


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Hi @Greg_Deckler -- I went through most of the materials that you provided and they helped me figure out what I need but have expanded my understanding of Time Intelligence and DAX in general. 

Thank you again!

-David

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.