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
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.
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.
- JulietZhu8 years ago
Helper IV
mattbrice You are right, my slice from 2nd day. Not first day. I thought firstdate is the 1st calendar day of each month. Actually it is the first date of slicer range. For my case, obviously I need 1st calendar day of each month and that is why EOMONTH(MIN()) works. Thanks so much for explantion. Total get it. Thanks soooooo much.
- mattbrice8 years ago
Solution Sage
Great.
And FYI you could also do: STARTOFMONTH ( DATEADD ( Calendartable[Date] , -1, month ) )
and you will get the first day of the month based on the first date visible in the current filter context and then not worry about having to be as careful about slicer dates.
- JulietZhu8 years ago
Helper IV
4 average measures have been calculated so far including Last 30 days, Last 60 days, Previous month, Previous 2 months. Now I need drop down list and let user to choose which average you want to see. My idea is adding a slicer table with Average Option column which includes the above 4 average name. In the Measure table add four measures and DAX samples are below.
Avg_Payment_Last30DaysSlicer = IF(
CONTAINS(AverageSlicerTable,AverageSlicerTable[AverageOption],"Last 30 days"),
MeasureFromSqlServer[Avg_PaymentAmt_Last30Days],
BLANK()
))
But after I add all the slicer measures to the value and choose the option from slicer, all measures’ header are shown below include blank measures (left table). I am looking for the way only shows the measure header and number that you chose? For example, If I choose last 60 days, only this measure shows in the table (like right table). Other blank measures including header won't show. Thanks
- mattbrice8 years ago
Solution Sage
Set your card to use only the following measure:
Slicer_measure = VAR _slicerSelection = SELECTEDVALUE ( slicer_Table[measure], "Avg_payment_Last_30days" ) RETURN SWITCH ( _slicerSelection, "Avg_payment_Last_30days", [Avg_payment_Last_30days], "Avg_payment_Last_60days", [Avg_payment_Last_60days], "Avg_payment_PreviousMonth", [Avg_payment_PreviousMonth], "Avg_payment_Previous2Month", [Avg_payment_Previous2Month] )But unfortunately the card will only show the category label "Slicer_Measure" and there are no formatting options to programatically change category label afaik. But what you can do is add another card with this measure :
slicer_Measure text = SELECTEDVALUE ( slicer_Table[measure], "Avg_payment_Last_30days" )
- JulietZhu8 years ago
Helper IV
mattbrice, Switch is enough since it only shows the measure chosen. That is exactly what I am looking for.
For text box, it is no problem since I will put the slicer aside the measure and user will know which average measure they choose.
Thanks so much again for your quick reply.
- JulietZhu8 years ago
Helper IV
Further requirement for this project. Need layout like below
But in power bi those 9 numbers are from 9 different measures. The layout I got so far frow power BI is below.
All the texts are from text boxes and numbers are from multirow card without catagory lable.
But in this way, I can't adjust the distance bewteen row and columns and can't add gridline on columns or rows. What is best way to do it? Thanks for help.