cancel
Showing results for
Did you mean:
Post Patron

## Calculate previous year sales based on mini and max date

Hi Experts

For the year 2019 my dates run from 29.12.2018 to 30.12.2019  I want to dynamically calculate the sales based on year slicer. The year 2018 runs from 28.12.2018 to 28.12.2019..

Don't as why?

My sales table is Fact Edw Sales

My DimDate is Edw Time with a date column and year and periods column

1 ACCEPTED SOLUTION
Community Support

Hi, @Cloud11

According to your description, I create some data, and you can try this measure:

``````Year sales =
var _selectedyear=YEAR(MIN('DimDate'[Date]))
return
SWITCH(
_selectedyear,
2018,CALCULATE(SUM('Fact Edw Sales'[Sales]),FILTER('Fact Edw Sales',[Date]>=DATE(2018,12,28)&&[Date]<=DATE(2019,12,28))),
2019,CALCULATE(SUM('Fact Edw Sales'[Sales]),FILTER('Fact Edw Sales',[Date]>=DATE(2018,12,29)&&[Date]<=DATE(2019,12,30))),
BLANK())``````

Then you can create a Slicer (place ‘DimDate’ [year] ) and a card chart(place this measure [Year sales]), like this:

And you can get what you want.

If this result is not what you want, you can post some sample data(without sensitive data) and your expected result.

Best Regards,

Community Support Team _Robert Qin

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

4 REPLIES 4
Community Support

Hi, @Cloud11

According to your description, I create some data, and you can try this measure:

``````Year sales =
var _selectedyear=YEAR(MIN('DimDate'[Date]))
return
SWITCH(
_selectedyear,
2018,CALCULATE(SUM('Fact Edw Sales'[Sales]),FILTER('Fact Edw Sales',[Date]>=DATE(2018,12,28)&&[Date]<=DATE(2019,12,28))),
2019,CALCULATE(SUM('Fact Edw Sales'[Sales]),FILTER('Fact Edw Sales',[Date]>=DATE(2018,12,29)&&[Date]<=DATE(2019,12,30))),
BLANK())``````

Then you can create a Slicer (place ‘DimDate’ [year] ) and a card chart(place this measure [Year sales]), like this:

And you can get what you want.

If this result is not what you want, you can post some sample data(without sensitive data) and your expected result.

Best Regards,

Community Support Team _Robert Qin

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

Super User IV

@Cloud11 , I am assuming you have year(FY) or year start date, now bases on year or year start date you can get these calculation

Start of year = based on calendar

Year Rank = RANKX(all('Date'),'Date'[Year Start date],,ASC,Dense)

Day of Year =datediff([Year Start date] , [Date],Day) +1

This Year = CALCULATE(sum('order'[Qty]),filter(ALL('Date'),'Date'[Year]=max('Date'[Year])))
Last Year = CALCULATE(sum('order'[Qty]),filter(ALL('Date'),'Date'[Year]=max('Date'[Year])-1))
rolling = CALCULATE(sum('order'[Qty]),filter(ALL('Date'),'Date'[Year]>=max('Date'[Year])-2 && 'Date'[Year]<=max('Date'[Year])) )

diff = [This Year]-[Last Year ]
diff % = divide([This Year]-[Last Year ],[Last Year ])

This Year = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Year Rank]=max('Date'[Year Rank])))
Last Year = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Year Rank]=max('Date'[Year Rank])-1))

YTD= CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Year Rank]=max('Date'[Year Rank]) && 'Date'[Day of Year] <= Max('Date'[Day of Year]) ))
LYTD = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Year Rank]=max('Date'[Year Rank])-1 && 'Date'[Day of Year] <= Max('Date'[Day of Year])))

Proud to be a Super User!

Post Patron

HI amit this looks like a work able solution do you have a pbix

Continued Contributor

Please consider this solution and leave kudos ....

Use a Calendar table with a [dontaskwhyyearoffset] column which runs  from 28/12 to 27/12,

eg yearoffset  = 0 is this year, -1 is last year, -2 is the previous year and +1 is next year

Then use the offset in filters and CALCULATE measures

eg sales last year = CALCULATE(SUM(Sales[value]),Calendar[dontaskwhyyearoffset] = -1)

Announcements