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

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.

Reply
PwrBI01
Post Patron
Post Patron

Show last years in a chart

Hi guys,

 

I have this chart and I would like to just show the last 2 years, is there a way to do it automatically instead of using filters for the chart?

 

Imagen1.png

 

The file is in this url: https://www.dropbox.com/s/l5a8cmv5oarrktk/Sales.pbix?dl=0

 

Thanks in advance.

 

Regards.

1 ACCEPTED SOLUTION
v-eqin-msft
Community Support
Community Support

Hi @PwrBI01 ,

 

It seems that you want to show the values of  last 2 year based on the max date in Calendar table, right?

You could use the following formula to create a measure :

Measure=
VAR _maxYear =
    MAXX ( ALL ( 'Calendar table' ), YEAR ( [Date] ) )
RETURN
    IF ( _maxYear - YEAR ( MAX ( 'Sales'[Date] ) ) IN { 0, 1 }, 1, 0 )

After applying it to the filter pane ,the chart could be dynamically changed when new data like 2021 added:

dynamic changed.gif

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

15 REPLIES 15
Ashish_Mathur
Super User
Super User

Hi,

Does my solution here help? - Flex a Pivot Table to show data for x months ended a certain user defined month.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi @Ashir, thank you very much. I needed to show always the same month (not the previous ones). Anyway, that link is very useful and I am sure I will use it later for another visuals.

v-eqin-msft
Community Support
Community Support

Hi @PwrBI01 ,

 

It seems that you want to show the values of  last 2 year based on the max date in Calendar table, right?

You could use the following formula to create a measure :

Measure=
VAR _maxYear =
    MAXX ( ALL ( 'Calendar table' ), YEAR ( [Date] ) )
RETURN
    IF ( _maxYear - YEAR ( MAX ( 'Sales'[Date] ) ) IN { 0, 1 }, 1, 0 )

After applying it to the filter pane ,the chart could be dynamically changed when new data like 2021 added:

dynamic changed.gif

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @v-eqin-msft, I have add values until May 2021 and I have a new question.

 

If I select the month December it just shows the year 2020 (because there is a filter for the last two years and there are no data for December 2021).

 

Is it possible to filter the last two years with data? So if I select December it shows December 2019 and December 2020 and if I select May it shows May 2020 and May 2021

 

Thanks in advance.

 

Regards.

Hi @PwrBI01 ,

 

It's suggested post a new thread since this has been resolved. 

More engineers will answer for you as soon as possible.😀

 

And please provide necessary information such as tables and expect outputs.

 

Best Regards,
Eyelyn Qin

Hi @v-eqin-msft,

 

Thank you for your answer. I have post it with the new problem on https://community.powerbi.com/t5/Desktop/Show-last-2-years-with-data-in-a-chart/m-p/1915142#M732288

 

Thanks in advance.

 

Regards.

Hi @v-eqin-msft, that is exactly what I need, thank you very much.

VijayP
Super User
Super User

@PwrBI01 

I have got this solution with small manual intervention. However will work out the final out come as well
check this pbix

 




Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!
MY Blog || My YouTube Channel || Connect with me on Linkedin || My Latest Data Story - Ageing Analysis

Proud to be a Super User!


Thank you very much @VijayP,

 

I will try continue looking for a way with no manual intervention, but if there is not possible I will use this one.

 

Regards.

@PwrBI01 If you find this as solution please mark this as solution . Thanks




Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!
MY Blog || My YouTube Channel || Connect with me on Linkedin || My Latest Data Story - Ageing Analysis

Proud to be a Super User!


VijayP
Super User
Super User

@PwrBI01 remove the slicers to see the result in the 2nd visual




Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!
MY Blog || My YouTube Channel || Connect with me on Linkedin || My Latest Data Story - Ageing Analysis

Proud to be a Super User!


VijayP
Super User
Super User

@PwrBI01 

"I have this chart and I would like to I have this chart and I would like to just show the last 2 years, is there a way to do it automatically instead of using filters for the chart? , is there a way to do it automatically instead of using filters for the chart?"

That was your question hence created the measure. It is giving last two years . you can remove the slicer and see the result. you can change the number in the formula and see the number of years increasing! or decreasing




Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!
MY Blog || My YouTube Channel || Connect with me on Linkedin || My Latest Data Story - Ageing Analysis

Proud to be a Super User!


Hi @VijayP,

 

I'm sorry if I explained in a wrong way. What I want is to get the chart on the right part automatically with a measure, so it should show the years 2019 and 2020 with the data that are charged and if for example I add 2021 and I don't delete any year it should show the years 2020 and 2021.

 

If I do this with a filter on the chart, I have to change the filter every time I add a new year.

 

I send you the photo of what I want:

 

Imagen2.png

 

Regards.

VijayP
Super User
Super User

@PwrBI01 

test = CALCULATE([YTD-Sales],FILTER(ALL('Calendar table'),'Calendar table'[Year]<=MAX('Calendar table'[Year])-4))
use this formula
and pbix file is attached here
 



Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!
MY Blog || My YouTube Channel || Connect with me on Linkedin || My Latest Data Story - Ageing Analysis

Proud to be a Super User!


Hi @VijayP,

 

Thank you for your answer.

 

I tried to download the pbix you sent but I think you confused and you sent the same as me.

 

When I created that measure it shows the last 2 years, but the results aren't the same.

 

Imagen1.png

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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