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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
SteveCarter1
Advocate II
Advocate II

Need help with SAMEPERIODLASTYEAR function and a filtered master calendar

I have a measure like so:

 

Count of Appointments Same Period Last Year = CALCULATE(COUNTA(Appointments[Status]),SAMEPERIODLASTYEAR('Master Calendar'[Date]))

 

 

My data relationships are like this: Master Calendar Period filters Master Calendar filters Appointments


Capture.PNG

 

In reporting visualisation if I have the Master Calendar 'Date' column as a slicer, I can get a nice matrix table showing current and previous year appointment numbers like so:


Capture2.PNG

 

My problem is if I use the Calendar Period table as a date slicer, I no longer get the previous year period numbers, unless I include the previous year in the slicer too:

Capture3.PNGCapture4.PNG

 

I understand that this is happening because I am filtering my Master Calendar due to my selection in the Calendar Period table so the measure can't calculate numbers for the last year period.

 

Right now as a workaround I tell people they need to select a Calendar Period that spans over 1 year in order to display previous years numbers along side but I would like to present the data as in the top table screenshot while still using an easy to choose time period slicer as I already have in the Calendar Period table.

 

I need to keep the date hierarchy available so users can choose a date, week, month, quarter, year etc and the DAX calculation needs to work with all those potentional groupings, which is why I love the sameperiodlastyear function as it takes care of that for me.

I like the Calendar Period slicer as does management, to quickly pick a time period so I don't want to push onto them the more cumbersome Master Calendar date slicer.

I'm thinking there should be a DAX solution but can't get it.

2 ACCEPTED SOLUTIONS
Greg_Deckler
Super User
Super User

@SteveCarter1 - There is. 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...

 

The basic technique, you figure out the date of last year min and max from the current user selections. You use ALL or ALLSELECTED or REMOVEFILTERS to override your filter context for date and then filter back down to the range you want. @ me if you have trouble implementing and post sample data as text in a table so I can mock up your situation and get you a more specific answer. 


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

OwenAuger
Super User
Super User

@SteveCarter1  I would suggest adding REMOVEFILTERS ( 'Master Calendar Periods' ) within CALCULATE (can use ALL in place of REMOVEFILTERS if you like).

 

Your selections on 'Master Calendar' / 'Master Calendar Periods' still determine the initial dates that are visible in the visual, but the "Last Year" measure is no longer constrained by any filters from 'Master Calendar Periods'.

 

Oh, and your 'Master Calendar' table should be marked as a Date Table if it isn't already.

 

 

Count of Appointments Same Period Last Year =
CALCULATE (
    COUNTA ( Appointments[Status] ),
    SAMEPERIODLASTYEAR ( 'Master Calendar'[Date] ),
    REMOVEFILTERS ( 'Master Calendar Periods' )
)

 

 It worked in a test model at my end. Does it work for you?

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

9 REPLIES 9
Anonymous
Not applicable

Apart from what @OwenAuger says, you could also INCORPORATE the Master Calendar Periods into the main calendar, Master Calendar. Of course, you would then not be able to mark the table as a date table but you would have one table instead of two, which would be more intuitive for the end user. I know there is a 1:* relationship between the tables. THIS DOES NOT PREVENT YOU FROM MERGING THEM.
amitchandak
Super User
Super User

@SteveCarter1 , I am not sure of the role of a master calendar period. But make sure the master calendar is marked as date calendar Right-click on the table. There is an option.

you can also try

Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Year))

2 Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-2,Year))

 

Power BI — YTD
https://medium.com/@amitchandak.1978/power-bi-ytd-questions-time-intelligence-1-5-e3174b39f38a
Power BI — QTD
https://medium.com/@amitchandak.1978/power-bi-qtd-questions-time-intelligence-2-5-d842063da839
Power BI — MTD
https://medium.com/@amitchandak.1978/power-bi-mtd-questions-time-intelligence-3-5-64b0b4a4090e
Power BI — WTD
https://medium.com/@amitchandak.1978/power-bi-wtd-questions-time-intelligence-4-5-98c30fab69d3
https://community.powerbi.com/t5/Community-Blog/Week-Is-Not-So-Weak-WTD-Last-WTD-and-This-Week-vs-La...

Thanks for taking the time to reply.

I use the calendar period table to create arbitary relative time frames that can be quickly chosen from the slicer instead of requiring the end user to manually pick out a date range. For example I can create a 'Last 13 Months' period selection that would filter my master calendar to the last 13 months. I know this can be acheived without any modelling using the filter pane and relative date selection but to be frank that is simply too hard for the vast majority of my end users to deal with. Seeing a fixed set of options in a drop down seems to me, based on my client base and feedback, as the most effective solution.

Anonymous
Not applicable

"I use the calendar period table to create arbitary relative time frames that can be quickly chosen from the slicer instead of requiring the end user to manually pick out a date range."

Sorry but I don't get it.... How relevant the above is to what I wrote? Because I can't see any link. You can create your relative frames also in one big date table. You don't need 2 tables to do that.

I need to re-think my date table logic. I was under the impression for a table marked as a date table I needed a sequence of consecutive dates with no duplicates.

For it to be in one big table where I can choose 1 column for my slicer I would have thought I'd need 1 column with the period name in it then that period name I want chosen against each date, but because 1 date can be associated with more than 1 period name - e.g. the dates for 'Last Week' are the same for 'All Time' and 'This Year' and 'Last Year to Date' etc - I would have thought I would need the same date duplicated for each period name.

As an example for 1st Jan 2020 I have this in my Calendar Period table:

Capture.PNG

I generate that table in M using the master calendar table as a reference then create a new column for each period name and mark each date as a null if it does not apply or 1 if it applies to that period name then I unpivot the table so I end up with 2 columns like you see above.

Setting a relationship between those 2 tables on the date then lets me filter my master date table based on the period name.

 

I can't see how I can achieve this in a single master date table with a set of unique dates.

I'll be sure to look further into it though.

 

Thanks for the feedback.

OwenAuger
Super User
Super User

@SteveCarter1  I would suggest adding REMOVEFILTERS ( 'Master Calendar Periods' ) within CALCULATE (can use ALL in place of REMOVEFILTERS if you like).

 

Your selections on 'Master Calendar' / 'Master Calendar Periods' still determine the initial dates that are visible in the visual, but the "Last Year" measure is no longer constrained by any filters from 'Master Calendar Periods'.

 

Oh, and your 'Master Calendar' table should be marked as a Date Table if it isn't already.

 

 

Count of Appointments Same Period Last Year =
CALCULATE (
    COUNTA ( Appointments[Status] ),
    SAMEPERIODLASTYEAR ( 'Master Calendar'[Date] ),
    REMOVEFILTERS ( 'Master Calendar Periods' )
)

 

 It worked in a test model at my end. Does it work for you?

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Yours is by far the most succint and correct answer. Thank you.

I think I was having a 'can't see the forest amonst the trees' moment.

Greg_Deckler
Super User
Super User

@SteveCarter1 - There is. 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...

 

The basic technique, you figure out the date of last year min and max from the current user selections. You use ALL or ALLSELECTED or REMOVEFILTERS to override your filter context for date and then filter back down to the range you want. @ me if you have trouble implementing and post sample data as text in a table so I can mock up your situation and get you a more specific answer. 


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

As usual Greg, an extremely comprehensive reply and anyone coming across this thread can only be wiser for clicking through to read your blog article.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors