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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Bobbys
Helper III
Helper III

Help with YTD for different years

Hello all,

I'm trying to create a measure which show the YTD for the same period in previous years.So far I've created a YTD measure for this year which is:YTD = TOTALYTD(SUM(Sheet1[Sales TY]),Dates[Date])*1000

 

I've also created a YTD for last year, which is: YTD -1 = CALCULATE([Total sales LY],DATESYTD(Dates[Date]))

I created a separate table for year and a "Is current year" measure in my date table which contains the value of 1 if the date is in 2021. Then I used this DAX to find the YTD for the years previous to the selected ones:

 

YTD All Years =VAR YearsBack = YEAR(TODAY()) - SELECTEDVALUE(Years[Year])RETURNCALCULATE([Totalt salg YTD],DATEADD(Dates[Date], -YearsBack, YEAR))

This was the result:

 
Bobbys_5-1633523121585.png

 

 

However, as you can see from the 2021 line, my data is updated once a month and only contains values from late 2018 until august this year. However the grap "thinks" that we're in December 31st, and so therefore doesn't show YTD from january until august, but from january until december. Any idea how to fix this?

 
Bobbys_6-1633523157687.png

 

 

This is the final date from the data set I imported. I have however created a separate data table called Dates which consists of dates until december 31st:

Bobbys_7-1633523192378.png

 

Also, even my YTD - 1 measure seems to be off, as it shows the entire sales value of 2020, and not january-august like my YTD for this year measure does. This is how it's looking in my dashboard:

Bobbys_8-1633523224287.png

 

And this is how the values are supposed to look:

Bobbys_9-1633523254263.png

 

 

Would appreciate any help!! Also, sorry if this was posted in the wrong section. 

5 REPLIES 5
Greg_Deckler
Community Champion
Community Champion

@Bobbys Try something like:

YTD -1 = 
  VAR __Max = MAX('Sheet1'[Date])
  VAR __LY = DATE(YEAR(__Max)-1,MONTH(__Max),DAY(__Max))
RETURN
  CALCULATE([Total value LY],DATESYTD(Dates[Date]),FILTER(Dates[Date]<__LY)*1000


if you don't want to monkey around with TI functions then you could have done this without TI functions or even a date table:

YTD = 
  VAR __Max = MAX('Sheet1'[Date])
  VAR __Min = MINX(FILTER('Sheet1',YEAR([Date])=YEAR(__Max)),[Date])
RETURN
  SUMX(FILTER('Sheet1',[Date]>=__Min && [Date]<=__Max),[Value])

YTD -1 = 
  VAR __Max = MAX('Sheet1'[Date])
  VAR __Min = MINX(FILTER('Sheet1',YEAR([Date])=YEAR(__Max)),[Date])
  VAR __LYMax = DATE(YEAR(__Max)-1,MONTH(__Max),DAY(__Max))
  VAR __LYMin = DATE(YEAR(__Min)-1,MONTH(__Min),DAY(__Min))
RETURN
  SUMX(FILTER('Sheet1',[Date]>=__LYMin && [Date]<=__LYMax),[Value])


Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Thank you @Greg_Deckler, and I'll make sure to keep that in mind. If there is a way to get the measures I want without creating a whole new table, I'm totally down for that. Now that I've already made my date table though, I'll try the formula you posted. I got a error when trying to implement it: https://gyazo.com/454e4d4674b588a07922fa2ed96a4a8d

 

 

The error says "Too few arguments were passed to the filter funtion".

Any tips @Greg_Deckler? I appreciate your help so far!

Greg_Deckler
Community Champion
Community Champion

@Bobbys 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...

 

If that's not helpful, Sorry, having trouble following, can you post sample data as text and expected output?
Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Hello @Greg_Deckler, thanks for the info.
Did however not find much help in those posts. 

 

I'll try and explain my problem more clearly:

This is how my raw data looks. 

Date (Year Week)ProductValueValue Last Year
4 W 2018 33Product 10,440,33
4 W 2018 33Product 20,280,08
4 W 2018 33Product 3 0,941,33
4 W 2018 33Product 40,540,64


The most left column is the only date information I have from my data source. And as you can see, it's not a proper date colum. For this reason, I have created an additional date column in EXCEL, and I've then gone on and imported the data source into PowerBI. 


I've read multiple places that I need to make an additional date table. So I've done that. I've then gone on and created a relation between the dates in my date table, and the date in my data source table. Note: the dates in my data source table ONLY goes until august 2021, meanwhile the dates in my date table has dates until Dec 31st 2021. 

 

Bobbys_0-1633542938278.png

Now there are several measure I want to calculate. Two of the main ones are YTD and YTD -1, -2, -3 etc. 

This is the YTD formula I've used:  YTD = TOTALYTD(SUM(Sheet1[Value]),Dates[Date])*1000

Bobbys_1-1633543495573.png
In this case, the graph looks correct, as it only shows values from january until august. 
Then I've created a measure for YTD - 1 (january to august of last year) using this formula:

YTD -1 = CALCULATE([Total value LY],DATESYTD(Dates[Date]))*1000
Bobbys_2-1633543749585.png

The YTD -1 however shows the entire sales from 2020, and not the same period as for the YTD of this year. Basically, I want the chart to show sales from january - august 2021, which I have, and january - august 2020. 



Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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