Forum Discussion

anandav's avatar
anandav
Icon for Skilled Sharer rankSkilled Sharer
8 years ago
Solved

DATESINPERIOD calculation question

Hi All,

 

Below is my data:

CompnayDateAmount
A01/01/201810
B01/01/201820
B02/01/201820
A01/02/201830
A01/02/201810
B02/02/201840
B03/02/201850
A01/03/201850
B01/03/201860
A01/04/201870
A01/04/201850
A02/04/201840
A03/04/201870
B04/04/201880
A01/05/201890
B01/05/2018100
A01/06/2018110
B01/06/2018120
B02/06/2018130
B03/06/2018140
B04/06/2018140
B05/06/2018150
B06/06/2018160

 

I am using the following DAX formula to calculate moving / rolling average for the past 2 months.

Past X Months Average =
CALCULATE(
    AVERAGE(PrevMonth[Amount]),
    DATESINPERIOD(PrevMonth[Date],
        LASTDATE(PrevMonth[Date]),
        -2, 
        MONTH
    )
)

 

When I use the date with no heirarchy I get the correct result for the last day of the month.

 

 

But when I try to use the date hierarchy and display the year and month with the average, it is not giving the correct results.

 

Any suggestion why this is happening?

 

I do not have a calendar table.

 

Any help will be much appreciated.

  • I figured out that I need to have a calendar table to solve the above problem.

     

    If anyone is interested in the solution please see this blog.

2 Replies

  • anandav's avatar
    anandav
    Icon for Skilled Sharer rankSkilled Sharer

    I figured out that I need to have a calendar table to solve the above problem.

     

    If anyone is interested in the solution please see this blog.