Forum Discussion
Average payment for previous month
- 8 years ago
I downloaded and looked at your file. Only change I see to make is to remove the ".Date" part. Try this:
Avg_PreviousMonth_payment = CALCULATE ( AVERAGE ( DataFromDB[Payment_Amt] ), PREVIOUSMONTH ( CalendarTable[Date] ) ) - 8 years ago
One way to reduce is:
Creating a calculated column in your calendar table
DaysinMonth = DATEDIFF ( STARTOFMONTH ( CalendarTable[Date] ); ENDOFMONTH ( CalendarTable[Date] ); DAY ) + 1And in the measure:
TotalPayment_PreviousMonth = VAR DaysinPrevMonth = CALCULATE ( SELECTEDVALUE ( CalendarTable[DaysinMonth] ); PREVIOUSMONTH ( CalendarTable[Date] ) ) RETURN DIVIDE ( CALCULATE ( SUM ( DataFromDB[Payment_Amt] ); PREVIOUSMONTH ( CalendarTable[Date] ) ); DaysinPrevMonth )Regards
Victor
Lima - Peru
Or you can do this: (with no need for extra column in calendar table ):
Avg_PreviousMonth_payment =
VAR _previousMonth = PREVIOUSMONTH ( CalendarTable[Date] )
VAR _countDaysPreviousMonth = COUNTROWS ( _previousMonth )
VAR _sumPaymentAmt = CALCULATE ( SUM ( DataFromDB[Payment_Amt] ), _previousMonth )
RETURN
DIVIDE ( _sumPaymentAmt, _countDaysPreviousMonth )"...what is difference with and without .Date? "
Power BI Desktop in its infinite wisdom tries to help unwitting users by automatically adding hidden "Calendar" tables to data model for every Date column it finds. These extra tables are accessed using the dot notation ".[Date]". Your problem was that PREVIOUSMONTH uses the first date visible in the current filter context which for the hidden .[Date] calendar is probably the first date in CalendarTable since your slicer put a filter on "CalendarTable[Date]" and not "CalendarTable[Date].[Date]" (Filters are column specific). It then tries to go back a month from this first date (which doesn't exist) so it returns BLANK().
My personal preference is to turn off this feature and add my own Calendar tables when/where needed. It can be turned of in Options and Settings -> Options -> Data Load -> Auto Date / Time. But it is a personal choice.
I also need average for previous 2 monthes . I want to calculate total and days to get the result correct first .
This is error messae when I calculate total for 2 prvevious monthes
- mattbrice8 years ago
Solution Sage
Change 'EOMONTH' function to 'ENDOFMONTH' function and should work.
- JulietZhu8 years ago
Helper IV
mattbrice Thanks for quick reply. Yes. Syntax works and also learned ENDOFMONTH.
But the data is not correct. I need calculate the sum between 12/1/2017 to 1/31/2018. The total should be -180857.3. But with this formula in power bi is -177120.32. The difference is -3736.98, which is the payment from the beginning date 12/01/2017.
Does this mean datesbetween in power bi is not include the start date point? We know between in sql server includes both start and end date point.
BTW, if EMONTH() changed into EOMONTH(max(CalendarTable[Date]),-1))), it also works.
- JulietZhu8 years ago
Helper IV
Solved. Start date is changed from first day into EOMONTH(min()). Datesbwtween in power bi will inculde both start and end date.
TotalPayment_Previous2Monthes = calculate(sum(DataFromDB[Payment_Amt]), DATESBETWEEN(CalendarTable[Date],EOMONTH(MIN(CalendarTable[Date]),-3)+1,EOMONTH(max(CalendarTable[Date]),-1)))
But not sure why the start date will be excluded when I use FIRSTDATE(DATEADD(CalendarTable[Date],-2,month)). Somebody can explain why. Thanks