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]) )
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
raw data:
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]
)
)