Average with topn

sadath

Active Member
Joined
Oct 10, 2004
Messages
262
Office Version
  1. 365
Platform
  1. Windows
i have following formula to find the average of top 2 sales in each city

AvgTopSales:=AVERAGEX(topn(2,summarize(ALL(Table1),Table1[Parlour],"TotSales",[Tot])),[TotSales])

currently my pivot table look like as below. in city1 i am supposed to get 1646.5 (avg of 1978 &1315)
but above formula returns 1527 for all store/city

Row Labels Tot AvgTopSales
CIty 1
Store1 1978 1527
Store10 1169 1527
Store4 821 1527
Store7 1315 1527
CIty 2
Store11 2013 1527
Store2 1076 1527
Store5 2114 1527
Store8 1028 1527
CIty 3
Store12 715 1527
Store3 1812 1527
Store6 2256 1527
Store9 1459 1527
Grand Total 17756 1527
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi sadath,

Try this instead:
Code:
AvgTopSales:=
CALCULATE (
    AVERAGEX (
        TOPN (
            2,
            ADDCOLUMNS ( SUMMARIZE ( Table1, Table1[Parlour] ), "TotSales", [Tot] ),
            [TotSales]
        ),
        [TotSales]
    ),
    ALL ( Table1[Parlour] )
)

The key change is that you need to clear the Parlour filter with ALL ( Table1[Parlour] ) in order to get the top 2 stores within the City (or whatever other filters may be applied).

In your original measure, using ALL ( Table1 ) meant that you were always summarising the entire Table.

I also used ADDCOLUMNS ( SUMMARIZE (...) ) rather than just SUMMARIZE - may not be strictly necessary.

Regards,
Owen
 
Upvote 0
Hi,
Thank you very much, that formula works brilliant !!.

one doubt, what is the difference between
ADDCOLUMNS ( SUMMARIZE ( Table1, Table1[Parlour] ), "TotSales", [Tot] ) & SUMMARIZE ( Table1, Table1[Parlour] , "TotSales", [Tot] )
 
Upvote 0

Forum statistics

Threads
1,213,489
Messages
6,113,953
Members
448,535
Latest member
alrossman

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top