Average with topn

sadath

Active Member
Joined
Oct 10, 2004
Messages
267
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

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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