Forum Discussion

BMltc's avatar
BMltc
Icon for Helper II rankHelper II
4 years ago
Solved

RANKX on a specific date

Hi Community,   I know that this question has been asked many time but I can't find my answer. I have a list of sales by product, by location and by date.  Date Product Location Sales 20/...
  • MFelix's avatar
    MFelix
    4 years ago

    Hi BMltc ,

     

    The you just need to add a calculation to precede your measures to check if it's the maximum date:

     

    Average value = 
    var MaximumDate = CALCULATE(MAX('Table'[Date]),ALL('Table'[Date]))
    Return
    IF(SELECTEDVALUE('Table'[Date]) = MaximumDate,
        CALCULATE ( AVERAGE ( 'Table'[Sales] ) )
    )
    
    Average value (previous) = 
    var MaximumDate = CALCULATE(MAX('Table'[Date]),ALL('Table'[Date]))
    Return
    IF(SELECTEDVALUE('Table'[Date]) = MaximumDate,
        CALCULATE (
            AVERAGE('Table'[Sales]),
            FILTER ( ALL ( 'Table'[Date] ), 'Table'[Date] = MAX ( 'Table'[Date] ) - 1 )
        )
    )
    
    Ranking = 
    IF([Average value] <> BLANK(),
        RANKX (
            FILTER (
                SUMMARIZE ( ALLSELECTED ( 'Table' ), 'Table'[Date], 'Table'[Product] ),
                'Table'[Date] = MAX ( 'Table'[Date] )
            ),
            CALCULATE ( 'Table'[Average value] )
        )
    )
    
    Previous Ranking = 
    IF(
        'Table'[Average value] <> Blank(),
        RANKX (
            FILTER (
                SUMMARIZE ( ALLSELECTED ( 'Table' ), 'Table'[Date], 'Table'[Product] ),
                'Table'[Date] = MAX ( 'Table'[Date] )
            ),
            CALCULATE ( 'Table'[Average value (previous)] )
        )
    )