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
PBSuper
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

7 REPLIES 7
v-jiascu-msft
Employee
Employee

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.

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.

TheOckieMofo
Resolver II
Resolver II

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.

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.

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

2017-08-18_7-43-55.png

 

Hope this helps

 

Regards



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Worked like a charm. Dank u Tom! 🙂

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] ) )

MTD Previous.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.

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.