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
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
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
- mattbrice8 years ago
Solution Sage
Does your date slicer include the first date of the month? Date manipulation functions work by sliding the visible window of dates forwards or backwards depending on options. If slicer date range is 2/3/2017 ... 2/25/2017 then DATEADD(CalendarTable[Date], -1, month) returns 1/3/2017 ... 1/25/2017. Then FIRSTDATE (DATEADD(CalendarTable[Date], -1, month)) will return 1/3/2017.