Forum Discussion
Time intelligence issues with SAMEPERIODLASTYEAR and DATEADD functions
I create three calculated Tables from my Date Dimension Table 'Order Date' and I see different days than expected.
Table 1 = DATESYTD('Order Date'[Date])
Table 2 = DATEADD(DATESYTD('Order Date'[Date]),-1,YEAR)
Table 3 = SAMEPERIODLASTYEAR(DATESYTD('Order Date'[Date]))
Assume that today is the 9th of July 2019.
The first table shows as expected dates from the 1st of January of the current year till today (e.g., 01.01.2019-09.07.2019)
The second and third tables shows dates from the 1st of January of the previous year till end of the current month as of today (e.g., July) in last year (e.g., 01.01.2018-31.07.2018)!
I would expect Tables 2 and 3 to return the following dates 01.01.2018 - 09.07.2018 instead.
Do I miss something?
8 Replies
- AnonymousNot applicable
The very fact that Table 1 ends on 9 July 2019 means that your Date dimension table 'Order Date' is not a real Date Table marked as such in the model. Sort out your model first. If the dimension is not correctly built, then the date intelligence functions will not work correctly.
Best
Darek
- AnonymousNot applicable
Dear Darek,
thank you for your reply. The Table 1 ends of 9th of July 2019 because of the DATESYTD function. So Actualy Table shows what I am expecting to see. I do not understand yous statement that "your Date dimension table 'Order Date' is not a real Date Table marked as such in the model".
The 'Order Date' table has a column [Date] of type Date with has dates from the begining of 2005 till the end of 2020. Could you elaborate further? Thanks.
- AnonymousNot applicable
If you had a real date table in your model, then applying DATESYTD to it would yield the full last year that exists in the table, in your case it would be the whole year 2020. I've checked it. This is because if there is no filter on the table, the last day visible would be the very last day and DATESYTD would then use this very last day and returned all the dates in the year up to this very last day.
Best
Darek
- AnonymousNot applicable
If "Order Dates' is A REAL DATE TABLE IN THE MODEL, then this is what you want:
[Dates YTD Up to Today] = var __today = today() RETURN CALCULATETABLE( DATESYTD( 'Order Dates'[Date] ), 'Order Dates'[Date] <= __today ) [Dates YTD Up to Today Shifted 1 Year Back] = var __today = today() return CALCULATETABLE( SAMEPERIODLASTYEAR( DATESYTD( 'Order Dates'[Date] ) ), 'Order Dates'[Date] <= __today )
Best
Darek