Forum Discussion

MiloPowerBI's avatar
MiloPowerBI
Frequent Visitor
3 years ago
Solved

Running Weekly Total Combining Two Data Sources

Hello all,    I'm looking to create a Matrix (or table) that displays Weekly Planned Targets for each week of the Quarter, which then turn into Actual numbers as the Quarter progresses. So for any ...
  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi MiloPowerBI ,

     

    Here I create a sample to have a test.

    Measure:

    Running Total = 
    VAR _CURRENTYEARWEEK =
        YEAR ( TODAY () ) * 100
            + WEEKNUM ( TODAY (), 1 )
    VAR _GENERATE =
        GENERATE (
            CALCULATETABLE ( VALUES ( 'Calendar'[YearWeek] ), ALL ( 'Calendar' ) ),
            CALCULATETABLE ( VALUES ( Actual[Product] ), ALL ( Actual[Product] ) )
        )
    VAR _ADD =
        ADDCOLUMNS (
            _GENERATE,
            "Value",
                IF (
                    'Calendar'[YearWeek] < _CURRENTYEARWEEK,
                    CALCULATE (
                        SUM ( Actual[Value] ),
                        FILTER (
                            Actual,
                            Actual[YearWeek] = EARLIER ( [YearWeek] )
                                && Actual[Product] = EARLIER ( [Product] )
                        )
                    ) + 0,
                    CALCULATE (
                        SUM ( Plan[Value] ),
                        FILTER (
                            Plan,
                            Plan[YearWeek] = EARLIER ( [YearWeek] )
                                && Plan[Product] = EARLIER ( [Product] )
                        )
                    )
                )
        )
    VAR _MIN =
        CALCULATE ( MIN ( Actual[YearWeek] ), ALLEXCEPT ( Actual, Actual[Product] ) )
    VAR _MAX =
        CALCULATE ( MAX ( Plan[YearWeek] ), ALLEXCEPT ( Plan, Plan[Product] ) )
    RETURN
        IF (
            MAX ( 'Calendar'[YearWeek] ) >= _MIN
                && MAX ( 'Calendar'[YearWeek] ) <= _MAX,
            SUMX (
                FILTER (
                    _ADD,
                    [Product] = MAX ( Actual[Product] )
                        && [YearWeek] <= MAX ( 'Calendar'[YearWeek] )
                ),
                [Value]
            )
        )

    Result is as below.

     

    Best Regards,
    Rico Zhou

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.