Search code examples
powerbidaxpowerbi-desktop

Twelve Rolling Month calculation by grouping existing and past three quarters


I have following test dataset: https://dbfiddle.uk/A5QVnwXe I want to calculate sum for current and past three quarter prices rows on a rolling basis.

The code that I tried in power bi with the result I got:

Test_12RM = 
var _selecteddate = MAXX(ALLSELECTED(TestTable), [ClosingDate]) 

return if( ISFILTERED(TestTable[ClosingDate].[QuarterNo]), 
CALCULATE( sum(TestTable[Price]), DATESINPERIOD(TestTable[ClosingDate],
_selecteddate,-4, QUARTER)), 
SUMX(ALLSELECTED(TestTable), [Price]) )

Result

This just shows result for the last quarter grouping, while I want results for last quarter group, the quarter before last quarter group until the top 3 quarters are left when group of 4 is not possible. Ideally in this form of table in power bi

enter image description here

raw data:

enter image description here

excel calculation: enter image description here


Solution

  • you can create a calendar table and create a measure

    MEASURE =
    VAR _start =
        EDATE ( MAX ( 'date'[Date] ), -12 ) + 1
    VAR _min =
        CALCULATE ( MIN ( 'date'[Date] ), ALL ( 'date' ) )
    RETURN
        IF (
            _start < _min,
            BLANK (),
            SUMX (
                FILTER (
                    ALL ( 'Table' ),
                    'Table'[Closing Date] >= _start
                        && 'Table'[Closing Date] <= MAX ( 'date'[Date] )
                ),
                'Table'[Price]
            )
        )
    

    enter image description here