I'll answer this along the same lines as the linked post, but with some modifications.
First, we need to add another product to serve as the remainder outside of the top 2. I'm going to name this "Remainder"
since you already have "Other"
. Unlike the linked post, I'm going to create this as a separate dimension table, so my source data for the pivot table are two tables Table1
and Products
that look like this:
You'll need to create a relationship between these tables on the Product
column. Excel might be able to auto-detect but do it manually if you need to.
Like in the linked post, we'll define a couple of base measures to make the one we're after easier to write:
SumSales = SUM ( Table1[Sales] )
and
Product Rank = RANKX ( ALLSELECTED ( Products[Product] ), [SumSales] )
With these defined, we can write the measure that we actually want to use:
Top 2 and Remainder =
IF (
HASONEVALUE ( Products[Product] ),
IF (
[Product Rank] <= 2,
[SumSales],
IF (
VALUES ( Products[Product] ) = "Remainder",
SUMX (
FILTER ( ALLSELECTED ( Products[Product] ), [Product Rank] > 2 ),
[SumSales]
)
)
),
[SumSales]
)
Using the Table1[Country]
, Table1[Group]
, and Products[Product]
for the rows in the pivot table and this measure we get:
Note that since I'm using ALLSELECTED instead of ALL for the ranking, this measure should still work when you apply filters to your data.