Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

How to create rolling averages over non-consecutive dates?

Hi community, I 'm relatively new to power BI and I 'm stuck with rolling averages. I would like to create a 3-month rolling average starting from month-1 until month+1. the built-in quick measure...
  • Anonymous's avatar
    Anonymous
    5 years ago

    Hi Anonymous ,

    I just updated my sample pbix file, please check whether that is what you want. According to your reply, it seems that you don't want to create additional calculated columns to achieve it, so I create another new measure to get the rolling average for non-consecutive dates without creating any other measure or calculated column:

    New_WB rolling average = 
    VAR _mindate =
        CALCULATE ( MIN ( 'Global'[Date] ), ALL ( 'Global' ) )
    VAR _maxdate =
        CALCULATE ( MAX ( 'Global'[Date] ), ALL ( 'Global' ) )
    VAR _curmonth =
        CONCATENATE (
            YEAR ( MAX ( 'Global'[Date] ) ),
            FORMAT ( MAX ( 'Global'[Date] ), "mm" )
        )
    VAR _premdate =
        CALCULATE (
            MAX ( 'Global'[Date] ),
            FILTER (
                ALL ( 'Global' ),
                CONCATENATE ( YEAR ( 'Global'[Date] ), FORMAT ( 'Global'[Date], "mm" ) ) < _curmonth
            )
        )
    VAR _premonth =
        CONCATENATE ( YEAR ( _premdate ), FORMAT ( _premdate, "mm" ) )
    VAR _nextmdate =
        CALCULATE (
            MIN ( 'Global'[Date] ),
            FILTER (
                ALL ( 'Global' ),
                CONCATENATE ( YEAR ( 'Global'[Date] ), FORMAT ( 'Global'[Date], "mm" ) ) > _curmonth
            )
        )
    VAR _nextmonth =
        CONCATENATE ( YEAR ( _nextmdate ), FORMAT ( _nextmdate, "mm" ) )
    VAR _sumofWB =
        CALCULATE (
            SUM ( 'Global'[WB] ),
            FILTER (
                ALL ( 'Global' ),
                CONCATENATE ( YEAR ( 'Global'[Date] ), FORMAT ( 'Global'[Date], "mm" ) ) >= _premonth
                    && CONCATENATE ( YEAR ( 'Global'[Date] ), FORMAT ( 'Global'[Date], "mm" ) )
                        <= IF (
                            _nextmonth = "",
                            CONCATENATE ( YEAR ( _maxdate ), FORMAT ( _maxdate, "mm" ) ),
                            _nextmonth
                        )
            )
        )
    RETURN
        DIVIDE (
            _sumofWB,
            IF (
                _curmonth = CONCATENATE ( YEAR ( _mindate ), FORMAT ( _mindate, "mm" ) )
                    || _curmonth = CONCATENATE ( YEAR ( _maxdate ), FORMAT ( _maxdate, "mm" ) ),
                2,
                3
            ),
            0
        )

    Best Regards

    Rena