Forum Discussion
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.
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.
- TomMartens9 years agoSuper User
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
- Hindska5 years agoNew Member
Worked like a charm. Dank u Tom! 🙂
- v-jiascu-msft9 years agoMicrosoft Employee
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