Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
BMltc
Helper II
Helper II

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. 

DateProductLocationSales
20/12/2021AppleParis5
20/12/2021AppleToronto6
20/12/2021AppleNew York56
20/12/2021BananaParis62
20/12/2021BananaToronto100
20/12/2021BananaNew York24
20/12/2021OrangeParis22
20/12/2021OrangeToronto66
20/12/2021OrangeNew York73
21/12/2021AppleParis99
21/12/2021AppleToronto51
21/12/2021AppleNew York28
21/12/2021BananaParis6
21/12/2021BananaToronto30
21/12/2021BananaNew York86
21/12/2021OrangeParis77
21/12/2021OrangeToronto32
21/12/2021OrangeNew York65
22/12/2021AppleParis31
22/12/2021AppleToronto7
22/12/2021AppleNew York94
22/12/2021BananaParis50
22/12/2021BananaToronto22
22/12/2021BananaNew York16
22/12/2021OrangeParis65
22/12/2021OrangeToronto29
22/12/2021OrangeNew York55

 

I want to display a table with my 3 products, being able to get the average of sales for the last date, the date before, ranking them on these specific date and finally compare the rankings. 

 Sales on 21/12/21RankSales on 22/12/21RankCompare
Apple59,3144,021
Banana40,7329,330
Orange58,0249,71-1

 

I can't succeed in my rankx function.

 

Can you please help me?

 

Many thanks all

1 ACCEPTED SOLUTION

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)] )
    )
)

 

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

6 REPLIES 6
MFelix
Super User
Super User

Hi @BMltc ,

 

Don't know if you want to show the two  values but you can do the following:

  • Add a calendar table to your model
  • Create the following measures:
Average value =
IF (
    SELECTEDVALUE ( 'Table'[Date] ) = MAX ( 'Calendar'[Date] ),
    CALCULATE ( AVERAGE ( 'Table'[Sales] ) )
)


Average value (previous) =
IF (
    SELECTEDVALUE ( 'Table'[Date] ) = MAX ( 'Calendar'[Date] ),
    CALCULATE (
        AVERAGE ( 'Table'[Sales] ),
        FILTER ( ALL ( 'Table'[Date] ), 'Table'[Date] = MAX ( 'Calendar'[Date] ) - 1 )
    )
)


Ranking =
IF (
    'Table'[Average value] <> BLANK (),
    RANKX (
        FILTER (
            SUMMARIZE ( ALLSELECTED ( 'Table' ), 'Table'[Date], 'Table'[Product] ),
            'Table'[Date] = MAX ( 'Calendar'[Date] )
        ),
        CALCULATE ( 'Table'[Average value] )
    )
)


Previous Ranking =
IF (
    'Table'[Average value] <> BLANK (),
    RANKX (
        FILTER (
            SUMMARIZE ( ALLSELECTED ( 'Table' ), 'Table'[Date], 'Table'[Product] ),
            'Table'[Date] = MAX ( 'Calendar'[Date] )
        ),
        CALCULATE ( 'Table'[Average value (previous)] )
    )
)


Ranking Difference = [Ranking] - [Previous Ranking]

 

Final result below:

MFelix_0-1640608326627.png

PBIX file attach.

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



thank you @MFelix , that is almost perfect! I have noticed that you have filtered the table

BMltc_0-1640616482718.png

is it possible to have the same table without this filter?

the concept is that date will change and i want the latest date and compare the data a week ago.

 

Hi @BMltc ,

 

If you don't want  to filter the table, then you don't need the calendar table, redo the measures to:

 

Average value = 
    CALCULATE ( AVERAGE ( 'Table'[Sales] ) )



Average value (previous) = 
    CALCULATE (
        [Average value],
        FILTER ( ALL ( 'Table'[Date] ), 'Table'[Date] = MAX ( 'Table'[Date] ) - 1 )
    )


Ranking = 
    RANKX (
        FILTER (
            SUMMARIZE ( ALLSELECTED ( 'Table' ), 'Table'[Date], 'Table'[Product] ),
            'Table'[Date] = MAX ( 'Table'[Date] )
        ),
        CALCULATE ( 'Table'[Average value] )
    )



Previous Ranking = 
    RANKX (
        FILTER (
            SUMMARIZE ( ALLSELECTED ( 'Table' ), 'Table'[Date], 'Table'[Product] ),
            'Table'[Date] = MAX ( 'Table'[Date] )
        ),
        CALCULATE ( 'Table'[Average value (previous)] )
    )


Ranking Difference = [Ranking] - [Previous Ranking]

 

MFelix_0-1640622036503.png

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Thanks again @MFelix, sorry but my last request was not clear. In this example, my dataset is from 20/12 to 22/12 but the day after, it will be updated from 23/12 to 20/12 (and the day after 24/12 to 20/12). And I want the table you have done 

BMltc_0-1640677326898.png

with the lastest date compared to 1 day before. is it clear?

 

Thanks again

 

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)] )
    )
)

 

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Really helpful, many thanks

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.