Forum Discussion
YTD last year DAX
Hi,
I have question about YTD Last year DAX formula. Even though I have read many blogs and topics here in community, I still don't know how to solve this issue.
Suppose I have data like below.
| ID | Date | Amount |
| 1 | 01.01.2016 | 10 |
| 2 | 02.01.2016 | 15 |
| 3 | 03.01.2016 | 11 |
| 4 | 10.01.2016 | 8 |
| 5 | 20.02.2016 | 7 |
| 6 | 01.01.2017 | 10 |
| 7 | 02.01.2017 | 12 |
| 8 | 03.01.2017 | 9 |
I would like to figure up YTD value of amount for actual data (year 2017) and then YTD value for corresponding period previous year.
For YTD I use formula:
YTD = CALCULATE(SUM(Data[Amount]);DATESYTD(Date[Date]))
For YTD LY I use formula:
YTD LY = CALCULATE([YTD];SAMEPERIODLASTYEAR(Date[Date]))
Then when I use these formulas in PBI, I have got incorrect value for YTD LY. I expect value 36 (dates: 1.1. - 3.1. 2016), but in total I have got 44 (it also calculates Amount 8 in 10.1.2016) - so it gives me period of january 2016, but I want only the corresponding days from 2017.
But YTD LY value in pic below on row for 3.1.2017 is OK.
Does anybody know the solution?
Thanks.
Regards.
Pavel
It should work fine in Power BI.
You didn't have any dates in your Data table beyond 3 Jan 2017 did you?
Here is a sample PBIX file with your data posted above where the measure is working:
I made a small change to the YTD measure so that it is not displayed after the max date in Data table, but the YTD LY measure is the same as I posted.
Have a play with that - there must be some difference in your model if it is not working.
Owen
55 Replies
- OwenAuger
Super User
It sounds like you want your YTD LY measure to detect the last date that appears in your Data table, and only translate dates up to that date back to last year within SAMEPERIODLASTYEAR.
You can use a pattern like this (note the distinction between Data[Date] which I assume is a column of Data, and Date[Date]):
YTD LY (limited by last date in Data table) = VAR DataMaxDate = CALCULATE ( MAX ( Data[Date] ), ALL ( Data ) ) RETURN CALCULATE ( [YTD], SAMEPERIODLASTYEAR ( INTERSECT ( VALUES ( Date[Date] ), DATESBETWEEN ( Date[Date], BLANK (), DataMaxDate ) ) ) )DataMaxDate is the global maximum date in your Data table. You can define it differently if you want to apply different logic.
Cheers,
Owen :)
- OwenAuger
Super User
It should work fine in Power BI.
You didn't have any dates in your Data table beyond 3 Jan 2017 did you?
Here is a sample PBIX file with your data posted above where the measure is working:
I made a small change to the YTD measure so that it is not displayed after the max date in Data table, but the YTD LY measure is the same as I posted.
Have a play with that - there must be some difference in your model if it is not working.
Owen
- rajkmr2195Regular VisitorTotal YTD = CALCULATE(SUM(Sheet1[Value]),FILTER('DimDate','DimDate'[Year]))This will give individual year of YTD .
- carlosrojaspdxFrequent Visitor
rajkmr2195,
Thank you for this formula, very useful and beautifuly made 🙂
Carlos
- v-huizhn-msft
Microsoft Employee
Hi PavelR,
SAMEPERIODLASTYEAR Function Returns a table that contains a column of dates shifted one year back in time from the dates in the specified dates column, it will change with actual date is you use it combine DATESYTD function. And SAMEPERIODLASTYEAR requires consecutive dates in the filter context. In your given sample data, the data column is not consecutive. You'd better create calendar date table using the formula:Calendar = CALENDAR(MIN(Table6[Date]),MAX(Table6[Date]))
Create the two measure using the formulas below.YTD = CALCULATE(SUM(Table6[Amount]),DATESYTD('Calendar'[Date])) YTD LY = CALCULATE(Table6[YTD],SAMEPERIODLASTYEAR('Calendar'[Date]))
For your issue, it because the slicer just includethe year rather day level, it will return the the same period about month in last year. The 44 equals the total of Jan. If you create a filter including day level, it will get the expected result like the following screenshot.
If you have any other issue, please feel free to ask.
Best Regards,
Angelia- PavelR
Solution Specialist
v-huizhn-msft thanks, I really appreciate your answer.
I have already used calendar function to create date table in example - named "Date". Facts are in table named "Data".
So my formulas for YTD and YTD LY are the same as yours - only with different namings of tables.
I don't get it why YTD LY shows me data for whole January of previous year - I just want the dates till 3.1., so from 1.1. - 3.1.2016. I also don't want to use filter of day. I just want it to be in default the valu 36 only with year filter. I use it to show KPI on dashboard where I want to present actual YTD value in comparison with YTD LY value - but for the corresponding period, so 1.1-3.1.2016 for YTD LY and 1.1.-3.1.2017 for YTD.
Is that possible?
Thanks indeed.
Regards.
Pavel
- AnonymousNot applicable
do the table calendar have to be related with the data table?
- AnonymousNot applicable
Yes the date table needs to be related to data table - you can see that in the sample file provided in solution.
- BlackwormsFrequent Visitor
First of, thanks for all the help on the issue OwenAuger however, I have a problem which I couldn't resolve at all since I am kind of beginner with DAX.
My dataset has unique Sales Year-Month-Day values from 01.01.2016 to 05.20.2017 in date format and was related with a Calendar Date column which ends at 05.20.2017 as well since it's the last data collection date.
First I was struggling to compare MoM for 2016 and 2017 sales. For example, May of 2017 contains only 20 days of data and I wanted to compare the same date range for May of 2016; however, it was fetching all the 2016 data till the end of the year. I solved that issue with your explanation.
For this year sales I used:
This Year Sales = CALCULATE(SUM([SalesRevenue]);DATESYTD('Calendar'[Date].[Date]))And for last year sales:
Last Year Sales = VAR DataMaxDate = CALCULATE ( MAX ( 'Calendar'[Date] ); ALL ( 'Calendar'[Date] ) ) RETURN CALCULATE ( [This Year Sales]; SAMEPERIODLASTYEAR ( INTERSECT ( VALUES ( 'Calendar'[Date].[Date] ); DATESBETWEEN ( 'Calendar'[Date].[Date]; BLANK (); DataMaxDate ) ) ) )Since my intention was comparing apples to apples, this was the result I was looking for. I finally was able compare the first 20 days of May 2016 and first 20 days of May 2017.
However, now I am not able to visually filter any of the months. I mean, when I click Last Year Sales, it's automatically accumulating This Year Sales as well. Please see below:
My intention is:
1. To be able to individually filter every year's sales like I was able to before.
2. If I cannot filter individually because of the formulas I used, I am kind of looking for a new way to compare last year's and this year's sales. Like I mentioned, last data date must be taken into account for last year's datas.
3. I created a different Calendar.[Date] and columns related with my sales date data. (Format is in date like 01.01.2016)
Thanks for all the help, rgrds,
- OwenAuger
Super User
When you have a clustered column chart with two or more measures as Value fields, visual filters resulting from clicking on the bars are applied to the axis field.
In your example, the visual filter is applied to the month dimension on the axis to include only "May", but both measures are still visible.
You can de-emphasise bars for a particular measure by clicking on the legend, but this can't be done simultaneously with an axis dimension filter. Also, I don't think this 'filters' any other visual in any way.
Also I couldn't work out what the $3.68m represents. Are you saying that's the total of Last Year Sales and This Year Sales?
- LuisGM7New Member
Hello Friends,
I am trying to calculate YTD from previous years, 2022, 2021, 2020.... I dont want to show the full year amount but compare against current YTD, for example Today 4/APR/2023 against 4/APR/2022/2021/2020... Etc, i am connected to a DataSet hence I cannot modify the model but create DAX measures locallly. I will appreciate any input, help from your side.
Thank you.
Luis
- Ashish_Mathur
Super User
Hi,
The measure should be something like this
YTD sales LY = calculate([sales],datesytd(calendar[date],"31/12"))
To the slicer drag Year from the Calendar Table and select a year. To your visual, drag Month name from the Calendar Table.
- Jith_RaphaelFrequent Visitor
Step 1. find YTD = TOTALYTD([Total Sales],CALENDAR[Period],"3/31") - for FY from April-March.
Step 2.
VAR MAXDATE =MAX(CALENDAR'[Period])
RETURN
CALCULATE([TOTAL YTD],SAMEPERIODLASTYEAR(DATESBETWEEN(CALENDAR[Period],BLANK(),MAXDATE)))