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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
Ciria
Advocate III
Advocate III

Time Intelligence // Problem with Last Year function on Linear & Area Charts

Dears:

 

I have a problem I cannot get the solution by my own, except using a slicer with "Date[Year]" and narrowing down the context.

 

Here the picture from my chart:

 

Sales -- Area Chart.PNG

 

My goal is to change my Total Sales LY function somehow to avoid Year = 2018 appears, and this needs to be done dynamically, not using either a slicer or something like Calculate( expression; Year <= "2018")....

The aim is not being on the need to amend the measure every year.

 

Total Sales LY = CALCULATE(SUM(Sales_fact[Total_Euros]);DATEADD('Date'[Date];-1;YEAR))

Here the file for your perusal:

 

https://www.dropbox.com/s/88dtadb0s7m1hpd/Dummy%20Model.pbix?dl=0

 

Kind regards!Smiley Very Happy

1 ACCEPTED SOLUTION

Hi @Anonymous

 

Thanks for the answer, it definately works on my report.

 

But I have a second question I would like to ask if you don't mind before closing this thread.

 

  • This visual was created using years in Axis.
  • I have another visual created in Year-Month and the latest sale was done on August 14th 2017.
    • Doing Total Sales - Total Sales LY, Powerbi is showing me the graph but everything shown beyond that date is consequently wrong.

Here the picture to show such problem:

 

Problem Difference Sales This Year Last Year.JPG

 

Here my script to solve the problem (two alternatives given), both work...

 

& Difference LY for Graphs = 
VAR Lastdatewithsales = LASTNONBLANK('Date'[Date],[Total Sales])
VAR Periodforcalculation= DATESBETWEEN('Date'[Date],MIN('Date'[Date]),Lastdatewithsales) // Option 1: Using Tutorial from https://www.sqlbi.com/articles/compare-equivalent-periods-in-dax/
VAR UsingLastDate = LASTDATE(Sales_fact[Invoice_Date]) 
// Using DAX Measures List https://msdn.microsoft.com/en-us/library/ee634396(v=sql.110).aspx
RETURN

IF(UsingLastDate>=MIN('Date'[Date]),
[Total Sales]-[Total Sales LY])

 

Do you think this is a good approach? any other suggestion?Smiley Sad

 

What I am learning these days with DAX is that if you want to use calculations for tables or for visuals, you have to take care about this topics, because it could hazy for some users...

 

What happens if I use this measure on a table with "Family" field leading instead of "Dates", the formula doesn't work as expected:

 

 Total Sales LY YTD.JPG

 

So here, my solution too:

 

& Difference LY YTD = 
VAR Lastdatewithsales = LASTNONBLANK('Date'[Date],[Total Sales])
VAR Periodforcalculation = DATESBETWEEN('Date'[Date],MIN('Date'[Date]),Lastdatewithsales) 
RETURN

[Total Sales]- CALCULATE([Total Sales LY],Periodforcalculation)

// IF functions doesn't work here, you don't need an interaction function here, just one single scalar value // IF(Lastdatewithsales>=MIN('Date'[Date]),[Total Sales]-[Total Sales LY])//

On Table instead of Visual (Area Chart): You can see here that IF functions (iterative) is not needed, and it doesn't get what you are looking for on this context.

Here functions like MTD, QTD may work too, but not sure if they will when you don't have populate data beyond August 14th...

 

 

 

 

Kind regards,Smiley Very Happy

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

@Ciria,

Change your measure to the following:

Total Sales LY = IF(MAX('Date'[Year])<YEAR(TODAY()); CALCULATE(SUM(Sales_fact[Total_Euros]);DATEADD('Date'[Date];-1;YEAR)))

1.JPG

Regards,
Lydia

Hi @Anonymous

 

Thanks for the answer, it definately works on my report.

 

But I have a second question I would like to ask if you don't mind before closing this thread.

 

  • This visual was created using years in Axis.
  • I have another visual created in Year-Month and the latest sale was done on August 14th 2017.
    • Doing Total Sales - Total Sales LY, Powerbi is showing me the graph but everything shown beyond that date is consequently wrong.

Here the picture to show such problem:

 

Problem Difference Sales This Year Last Year.JPG

 

Here my script to solve the problem (two alternatives given), both work...

 

& Difference LY for Graphs = 
VAR Lastdatewithsales = LASTNONBLANK('Date'[Date],[Total Sales])
VAR Periodforcalculation= DATESBETWEEN('Date'[Date],MIN('Date'[Date]),Lastdatewithsales) // Option 1: Using Tutorial from https://www.sqlbi.com/articles/compare-equivalent-periods-in-dax/
VAR UsingLastDate = LASTDATE(Sales_fact[Invoice_Date]) 
// Using DAX Measures List https://msdn.microsoft.com/en-us/library/ee634396(v=sql.110).aspx
RETURN

IF(UsingLastDate>=MIN('Date'[Date]),
[Total Sales]-[Total Sales LY])

 

Do you think this is a good approach? any other suggestion?Smiley Sad

 

What I am learning these days with DAX is that if you want to use calculations for tables or for visuals, you have to take care about this topics, because it could hazy for some users...

 

What happens if I use this measure on a table with "Family" field leading instead of "Dates", the formula doesn't work as expected:

 

 Total Sales LY YTD.JPG

 

So here, my solution too:

 

& Difference LY YTD = 
VAR Lastdatewithsales = LASTNONBLANK('Date'[Date],[Total Sales])
VAR Periodforcalculation = DATESBETWEEN('Date'[Date],MIN('Date'[Date]),Lastdatewithsales) 
RETURN

[Total Sales]- CALCULATE([Total Sales LY],Periodforcalculation)

// IF functions doesn't work here, you don't need an interaction function here, just one single scalar value // IF(Lastdatewithsales>=MIN('Date'[Date]),[Total Sales]-[Total Sales LY])//

On Table instead of Visual (Area Chart): You can see here that IF functions (iterative) is not needed, and it doesn't get what you are looking for on this context.

Here functions like MTD, QTD may work too, but not sure if they will when you don't have populate data beyond August 14th...

 

 

 

 

Kind regards,Smiley Very Happy

Anonymous
Not applicable

@Ciria,

Do you use same fields to create Area chart and table visual? I would recommend you open a new thread about this issue since it is a new issue which is different from the original question.

Regards,
Lydia

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.