Forum Discussion

mws5872's avatar
mws5872
Helper II
6 years ago
Solved

Calculate with Datesbetween filter and another filter

Hi All,   I am trying to solve a difficult problem and I am not really an expert in DAX.   I am taking a week to date number and comparing to an 4 week average based on the day of the week the us...
  • V-lianl-msft's avatar
    V-lianl-msft
    6 years ago

    Hi mws5872 ,

     

    It's not sure if the results in the sample data you gave are correct. You can try to create the following measures

    2 day sum = 
    CALCULATE (
        SUM ( Data[Total] ),
        DATESINPERIOD (
            Data[Date],
            LASTDATE ( Data[Date] ),
            -2,
            DAY
        )
    )
    
    2 days avg = [2 day sum]/2
    
    comparison =
    VAR this_weekday =
        SELECTEDVALUE ( Data[Day of Wek Number] )
    VAR last_weekday =
        CALCULATE (
            MAX ( Data[Day of Wek Number] ),
            FILTER ( ALL ( Data ), Data[Date] = SELECTEDVALUE ( Data[Date] ) - 1 )
        )
    VAR avg_static =
        CALCULATE (
            AVERAGE ( Data[Total] ),
            FILTER (
                ALL ( Data ),
                Data[Date] >= DATE ( 2020, 2, 9 )
                    && Data[Date] <= DATE ( 2020, 3, 8 )
                    && Data[Day of Wek Number] IN { this_weekday, last_weekday }
            )
        )
    RETURN
        DIVIDE ( [2 days avg], avg_static )

    Sample .pbix

     

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