Forum Discussion

LostInTheFlood's avatar
LostInTheFlood
Frequent Visitor
4 years ago
Solved

Calculate forecast based on increase and previous calculated month result

Hi smart people,   I have issues with a calculations and need your help. I’m trying to do a “total + forecast” measure (in the picture called Total). Where I have actuals I will show the actual a...
  • DataInsights's avatar
    4 years ago

    LostInTheFlood,

     

    Try these measures. I made the Period column a date by adding the first day of the month (2022-01 becomes 2022-01-01). This will enable you to create a relationship with the date table. Alternatively, you could create a Period Number column with values like 202201, and use Period Number instead of Period in the Running Total measure.

     

    Actual = SUM ( FactTable[Actual] )
    Forecast = SUM ( FactTable[Forecast] )
    Running Total = 
    VAR vLastActual =
        CALCULATE (
            LASTNONBLANKVALUE ( FactTable[Actual], [Actual] ),
            ALLSELECTED ( FactTable )
        )
    VAR vResult =
        IF (
            MAX ( FactTable[Actual] ) <> BLANK (),
            [Actual],
            vLastActual
                + CALCULATE (
                    [Forecast],
                    FILTER (
                        ALLSELECTED ( FactTable ),
                        FactTable[Period] <= MAX ( FactTable[Period] )
                    )
                )
        )
    RETURN
        vResult

     

     

  • DataInsights's avatar
    DataInsights
    4 years ago

    LostInTheFlood,

     

    Try this measure. I adjusted the DAX to handle months where both Actual and Forecast exist.

     

    Running Total = 
    VAR vLastActual =
        CALCULATE (
            LASTNONBLANKVALUE ( Actual[Actual Value], [ActualValue] ),
            ALLSELECTED ( Actual )
        )
    VAR vLastActualDate =
        CALCULATE (
            LASTNONBLANK ( 'Date'[Date], [ActualValue] ),
            ALLSELECTED ( Actual )
        )
    VAR vResult =
        IF (
            MAX ( Actual[Actual Value] ) <> BLANK (),
            [ActualValue],
            vLastActual
                + CALCULATE (
                    [ForecastValue],
                    FILTER (
                        ALLSELECTED ( 'Date' ),
                        'Date'[Date] <= MAX ( 'Date'[Date] )
                            && 'Date'[Date] > vLastActualDate
                    )
                )
        )
    RETURN
        vResult

     

     

    Now you can take that vacation. 🙂