Pivot or Power Pivot: how to calculate the other as a remainder between subtotal and top N items

南楼画角 提交于 2021-01-05 11:32:07

问题


I am trying to create a calculated item in Excel pivot that would calculate the "other" as the difference between subtotal of the group and top N selected items.

Here is the example of original data table and the current pivot table. The Product field is filtered by the top 2 items from Sales:

Is there any way to add a calculated item for each Group within the Country as the difference between the total Sales of the Product (before filtering) and the sum of the visible top N items of the Product?

The desired output would be like this:

Either Power Pivot or simple Pivot would work.

I would highly appreciate any ideas.


回答1:


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.



来源:https://stackoverflow.com/questions/65159092/pivot-or-power-pivot-how-to-calculate-the-other-as-a-remainder-between-subtotal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!