Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

DAX: Show last two months breakdown dynamically

Hi guys

 

I am stuck with a DAX problem. I have users selecting a month and based on that selection I want to dynamically show the breakdown between the previous and selected month in terms of decrease/increase etc. with a waterfall chart, something like this:

 

I am aware that I have to use an independent Date to be used in the charts as otherwise, the concept would not work. I have an IdDay and a date Tons_Date in the fact table that I use.

 

I wrote the following measure:

 

Previous 2 Months Actual Sales= 
VAR _MaxFactDate = CALCULATE(MAX('Sales'[IdDay]);ALL('DATE_Day'))
VAR _Tons = [Actual Sales]
VAR _maxDate = ENDOFMONTH('DATE_Day'[Date])
VAR _minDate = LOOKUPVALUE('DATE_Day'[IdDay];'DATE_Day'[Date];EDATE(_maxDate;-2))

RETURN

IF(_MaxFactDate <= MAX('DATE_Day'[IdDay]) && _MaxFactDate > _minDate;
    CALCULATE([Actual Sales];ALL('DATE_Day')))

 

where Date_Day is my main Date Table (and use in the slicer) and [Actual Tons] the explicit measure for which I want to show the breakdown.

The measure sort of works in a way that it shows the previous two months dynamically, except for September (it goes back to July although it should only show August and July), and unfortunately it does not show the right value for [Actual Tons].

 

Can anyone see a mistake in the DAX above? 

 

Thanks a lot for the help!

4 Replies