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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Anonymous
Not applicable

Custom Predefined Date Ranges - Prior Year Calculations

I have used this post to create my custom predefined date ranges, which was really helpful btw.

https://community.powerbi.com/t5/Desktop/Custom-Predefined-Date-ranges/m-p/228667#M101979

 

Now I'm facing an issue when I try prior year calculations:

PY Sales =
    CALCULATE(
        SalesPerItem[NetSales]
        ,SAMEPERIODLASTYEAR(Calendar[Date])
    )

I understand that the issue is as @steph_io said:

"[...]it works to filter most measures, except those that is looking at prior period outside of the visual context.

Because the new DatePeriod table has already filtered the CreateDateTable, this formula will not go fetch the range of the prior year to give me a Year over Year value."

Does anybody know a solution or a workaround?

1 ACCEPTED SOLUTION
v-xuding-msft
Community Support
Community Support

Hi @Anonymous ,

Prior year data is always filtered that is caused that there is a relationship between the Date Range table and Calendar table. For your requirement, we don't have to create any relationships among the Date Range table and others. We could use functions to implement filter. 

I created a simple sample that you can reference.

  • Relationships

1.PNG

  • Formulas
Custom data change = 
var sel = SELECTEDVALUE('Date Range'[Period])
var mon_today = MONTH(TODAY())
var year_today = YEAR(TODAY())
var mon = CALCULATE(SUM(SalesPerItem[ Sales]), FILTER('Calendar', 'Calendar'[MONTH] = mon_today))
var year = CALCULATE(SUM(SalesPerItem[ Sales]), FILTER('Calendar', 'Calendar'[YEAR] = year_today))
return
SWITCH(TRUE(), sel="This month",mon,sel="This year",year,SUM(SalesPerItem[ Sales]))
Last year = CALCULATE(SUM(SalesPerItem[ Sales]),SAMEPERIODLASTYEAR('Calendar'[Date]))

2.PNG3.PNG

I attached my sample that you can try and check if is what you want. Hope it may help you.

 

Best Regards,

Xue Ding

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. Kudos are nice too.

 

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-xuding-msft
Community Support
Community Support

Hi @Anonymous ,

Prior year data is always filtered that is caused that there is a relationship between the Date Range table and Calendar table. For your requirement, we don't have to create any relationships among the Date Range table and others. We could use functions to implement filter. 

I created a simple sample that you can reference.

  • Relationships

1.PNG

  • Formulas
Custom data change = 
var sel = SELECTEDVALUE('Date Range'[Period])
var mon_today = MONTH(TODAY())
var year_today = YEAR(TODAY())
var mon = CALCULATE(SUM(SalesPerItem[ Sales]), FILTER('Calendar', 'Calendar'[MONTH] = mon_today))
var year = CALCULATE(SUM(SalesPerItem[ Sales]), FILTER('Calendar', 'Calendar'[YEAR] = year_today))
return
SWITCH(TRUE(), sel="This month",mon,sel="This year",year,SUM(SalesPerItem[ Sales]))
Last year = CALCULATE(SUM(SalesPerItem[ Sales]),SAMEPERIODLASTYEAR('Calendar'[Date]))

2.PNG3.PNG

I attached my sample that you can try and check if is what you want. Hope it may help you.

 

Best Regards,

Xue Ding

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. Kudos are nice too.

 

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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.