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.
Hi there,
I have a question about what the below Dax function is actually doing with the date range.
The below DAX Function is the function for same period last year, but if the date is the 21st of this month, is the below function measuring from the 1st to the 21st or the 1st to the 31st (Full month).
LYTM Value = CALCULATE([Total Value],SAMEPERIODLASTYEAR(DATESMTD('CALENDAR'[DATE])))
This is the Sum
Total Value = SUM(SALES[VALUE])
This is the This year this month calc
TYTM Value = CALCULATE([Total Value],DATESMTD('CALENDAR'[DATE]))
This is the main measure, this shows negitive for most of the month, until it gets later into the month thus me questioning if the LYTM calculation is to the date last year or to the full month of the date this year - Hope that makes sense.
TYLM Growth Value = DIVIDE(([TYTM Value]-[LYTM Value]),[LYTM Value],0)
What I want the LYTM function to do, is go up until the date of the TYTM function.
Solved! Go to Solution.
Hi @MarkCBB,
Thanks for attaching your model 🙂
I have uploaded a file with some suggested changes.
https://www.dropbox.com/s/zhxslmbe8xy63l0/DateTesting%20V2.pbix?dl=1
I think the V3 measures in the above file do what you want, but have a play with them yourself.
This YTD V3 (blank after max date in DATA) = IF ( MIN ( DATES[DATE] ) <= CALCULATE ( MAX ( DATA[DATE] ), ALL ( DATA ) ), CALCULATE ( [Total Sales], DATESYTD ( DATES[DATE] ) ) ) Last YTD V3 (blank after max date in DATA) = IF ( MIN ( DATES[DATE] ) <= CALCULATE ( MAX ( DATA[DATE] ), ALL ( DATA ) ), CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( INTERSECT ( DATESYTD ( DATES[DATE] ), DATESBETWEEN ( DATES[DATE], BLANK (), CALCULATE ( MAX ( DATA[DATE] ), ALL ( DATA ) ) ) ) ) ) )
Explanation:
Owen 🙂
Owen Auger, CFA
https://msdn.microsoft.com/en-us/library/ee634972.aspx
The dates returned are the same as the dates returned by this equivalent formula:
DATEADD(dates, -1, year)
Hi @Greg_Deckler I have create a small table to test the results, the table has 2 columns, Date, Sales. Dates start from 2015-01-01 and end at 2016-07-24. Each date has a Sales value of 1.
The Ytd function returns 206, so 206th date. however the Last ytd function returns 212, so the dates go past the 2016-07-24.
Should the function not return that same value?
This appears to be one of the quirks of time-shifting time intelligence functions, and a good example of why your Date table should include complete months and years.
SAMEPERIODLASTYEAR (and DATEADD) apply special logic when shifting the last day of a particular month, to ensure it shifts to the last day of the same month in the prior year (or month in the case of DATEADD used with months).
In your case, SAMEPERIODLASTYEAR recognises that 2016-07-24 is the last day in July 2016 in your Date table, so when it shifts back one year to 2015, it deems the corresponding date to be 2015-07-31 (the last day of July in 2015 in your Date table).
This is more useful for cases when months have unequal numbers of days (e.g. Feb 28/29 when shifting by a year, or 30/31 day months when using DATEADD).
This is obviously not what you intended! Whether you call it a feature or a bug,extending your Date table to cover full months/years should solve it.
Owen 🙂
There is something very basic that I am not understanding here, Below is a link to download my pbix file, in the file there are 2 tables, DATA and DATES.
In the DATA table there are 2 columns, Date and Sales, in teh DATE table there is 1 column Date.
I have added in 3 measures, but I am not geting the results that I am expecting and I have no idea why,
Would either of you mind have a look at it and let me know where I am going wrong.
What I would like is for the sales to match, i.e. This YTD should SUM from the 2016-01-01 to the 2016-07-24 and the the Last YTD sould then SUM the 2015-01-01 to the 2015-07-24.
Hi @MarkCBB,
Thanks for attaching your model 🙂
I have uploaded a file with some suggested changes.
https://www.dropbox.com/s/zhxslmbe8xy63l0/DateTesting%20V2.pbix?dl=1
I think the V3 measures in the above file do what you want, but have a play with them yourself.
This YTD V3 (blank after max date in DATA) = IF ( MIN ( DATES[DATE] ) <= CALCULATE ( MAX ( DATA[DATE] ), ALL ( DATA ) ), CALCULATE ( [Total Sales], DATESYTD ( DATES[DATE] ) ) ) Last YTD V3 (blank after max date in DATA) = IF ( MIN ( DATES[DATE] ) <= CALCULATE ( MAX ( DATA[DATE] ), ALL ( DATA ) ), CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( INTERSECT ( DATESYTD ( DATES[DATE] ), DATESBETWEEN ( DATES[DATE], BLANK (), CALCULATE ( MAX ( DATA[DATE] ), ALL ( DATA ) ) ) ) ) ) )
Explanation:
Owen 🙂
Owen Auger, CFA
I'm sorry, what functions are you referring to when you say "Ytd" and "Last ytd"?
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
112 | |
100 | |
80 | |
64 | |
57 |
User | Count |
---|---|
146 | |
110 | |
93 | |
84 | |
67 |