cancel
Showing results for
Did you mean:
Frequent Visitor

## MTD Previous

I am using SAMEPERIODLASTYEAR for my MTD Last Year calculation, however it returns Sales for the entire month of the previous year. So if I am reporting MTD sales for 17-08-2017, the MTD Last Year I was expecting to be is for sales between 1-08-2016 to 17-08-2016. Whereas my dax is returning sales for 1-08-2016 to 31-08-2016.

My DAX is: MTD Sales (LY) = CALCULATE([MTD Sales],SAMEPERIODLASTYEAR(CALENDAR[CalendarKey]))

I also tried: PYMTD = totalmtd(sum(SALES[Sale Amount]),dateadd(FILTER(DATESMTD(CALENDAR[CalendarKey]),CALENDAR[CalendarKey]<TODAY()),-1,YEAR))

But this gives me sales up until the 16-08-2016, and skips sales for 17-08-2016.

I have seen many forums with this issue but I can't seem to get this going. Can someone please help?

Thanks

6 REPLIES 6
Resolver II

## Re: MTD Previous

Two things.

First, from a DAX perspective, I believe the formula you want is DATEADD. DateAdd will return partial periods whereas other functions return whole periods. Check this blog post for a good explanation: DateAdd

Second, a best practice I always use is to have a "trimmed" dates table, meaning I don't have dates that go beyond where I have data in my fact table. For example, I do a lot of month end reporting, so I always trim my dates table to stop at the last day of the prior month. The great thing about Power Query/M Language is that you can code this to basically be automatic.

So for your example, you would start by hardcoding that you want the date to be less than or equal to today's date. Once you do this, it will add a step to the query and you will see the M formula in the formula bar showing you what it's doing. Then, you can erase the hardcoded date in that formula bar and replace it with:

<=Date.From(DateTime.LocalNow())

This way, every time you refresh the data, your calendar table will always stop at todays date. There's a ton of date functions in M, so check out the Formula reference if there's other manipulations you need to make.

Frequent Visitor

## Re: MTD Previous

I have already trimmed my calendar dimension in my tabular to not include any future dates. My fact is not going to have future sales so I probably don't have to worry about that. Doing this has not fixed my problem.

Microsoft

## Re: MTD Previous

Hi @PBSuper,

Maybe it's something about context. Which table is the column date of the report from? Is it from table "Calendar"?

This formula works fine in my test:

```TotalMTDLastYear =
TOTALMTD ( SUM ( Sales[Quantity] ), SAMEPERIODLASTYEAR ( 'Calendar'[Date] ) )```

Best Regards!

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Super User III

## Re: MTD Previous

Hey,

to answer similar questions I always use the following approach, maybe it seems a little complex, but I appreciate the degree of freedom it provides, the following measures uses the same number of dates in the previous month, for reasons of simplicity I skipped the treatment being on March 30 or March 31

```SameNumberOfPeriodsPrevMonth =
var currentDay = DAY(MAXX(VALUES('Calendar'[Date]),'Calendar'[Date]))
var currentMonth = MONTH(MAXX(VALUES('Calendar'[Date]),'Calendar'[Date]))
var currentYear = YEAR(MAXX(VALUES('Calendar'[Date]),'Calendar'[Date]))
var startday = 1
var endday = currentDay
var startmonth = if(currentMonth = 1, 12, currentMonth-1)
var endmonth = if(currentMonth = 1, 12, currentMonth-1)
var startyear = if(currentMonth = 1, currentYear - 1, currentYear)
var endyear = if(currentMonth = 1, currentYear - 1, currentYear)
var startdate = DATE(startyear, startmonth, startday)
var endDate = DATE(endyear, endmonth, endday)
return
IF(NOT(ISBLANK(CALCULATE(SUM('FactWithDates'[Amount]))))
,CALCULATE(
SUM('FactWithDates'[Amount])
,DATESBETWEEN('Calendar'[Date], startdate, endDate)
)
,BLANK()
)```

The measure creates this output

Hope this helps

Regards

Hamburg - Germany
I accept Kudos :-), If you find my post helpful.

Proud to be a Datanaut!
Microsoft

## Re: MTD Previous

Hi @PBSuper,

Could you please mark the proper answer as solution or share the solution if it's convenient for you? That will be a big help to the others.

Best Regards!
Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Frequent Visitor

## Re: MTD Previous

After trying various ideas to no avail, I had to resort to splitting my last year sales from this year sales in my tabular model. This seems to be working fine.

Thank you all for your help again.

Announcements

#### Announcing the New Spanish Forum

Do you need help in Spanish? Check out our new Spanish community section.

#### MBAS Gallery 2020

Watch Microsoft Business Applications Summit sessions on-demand.

#### ‘Better Together’ Integration Forum Launch

We've launched a how-to forum where you can learn about how Power BI integrates with other Power Platform products.

Top Solution Authors
Top Kudoed Authors