Excel Pivot Table and Measures – How Can I have Distinct Count of IDs and Distinct Sum of related values

huangapple go评论62阅读模式
英文:

Excel Pivot Table and Measures - How Can I have Distinct Count of IDs and Distinct Sum of related values

问题

ID Amount State Category
1 10 我的状态 我的类别1
1 10 我的状态 我的类别2
1 10 我的状态 我的类别3
2 20 我的状态 我的类别1
2 20 我的状态 我的类别2
3 30 我的状态 我的类别1

这是我的Excel表格。

我使用数据透视表来显示每个州的总计,并使用ID的不同计数来显示每个类别中有多少个。

问题是Amount的总和与ID列的不同计数不匹配。

如何创建一个数据透视表的度量,以便可以获得Amount的有效不同总和(与ID相关)?

谢谢。

英文:
ID	Amount State Category
1	10 MyState MyCategory1
1   10 MyState MyCategory2
1	10 MyState MyCategory3
2	20 MyState MyCategory1
2	20 MyState MyCategory2
3	30 MyState MyCategory1

Here is my Excel table.

I use a Pivot Table to show totals per state, and I use DISTINCT COUNT of ID to show how many are in each category.

The problem is that the SUM of the Amount doesn't match up with the number in the DISTINCT COUNT of ID column.

How can I create a measure for the Pivot Table so that I can have an effective DISTINCT SUM of Amount (relating to ID)?

Thank you.

答案1

得分: 1

你可以使用 Power Pivot 中的 DAX 语言SUMXDISTINCT 函数。

Power Pivot 选项卡 > 管理数据模型 > 主页 > 新建度量

使用以下公式,替换 MyTableName

DistinctSumAmount := SUMX(DISTINCT('MyTableName'[ID]), 
CALCULATE(SUM('MyTableName'[Amount])))

保存并返回到你的 Excel 工作簿,使用数据模型创建一个数据透视表,并将新的 DistinctSumAmount 度量添加到你的数据透视表。

英文:

You can use the DAX language in Power Pivot with the SUMX and DISTINCT functions.

Power Pivot tab > Manage Data Model > Home > New Measure

Use the following formula, replacing MyTableName:

DistinctSumAmount := SUMX(DISTINCT('MyTableName'[ID]), 
CALCULATE(SUM('MyTableName'[Amount])))

Save and go back to your Excel workbook and create a Pivot Table using the data model and add the new DistinctSumAmount measure to your Pivot Table.

答案2

得分: 0

这是你要求的翻译内容:

"It's difficult to understand what you want because your example data set has distinct combinations of ID and Category, so even when you take a distinct count and a distinct sum, it would give the same values as an absolute count and absolute sum.

Also, a distinct sum based on ID only makes sense if each ID has a fixed Amount. If there are different rows where the ID is the same but the Amount is different, then it's not clear what a distinct sum should be.

So, using different example data where there are duplicate IDs for the same Category, but always with the same Amount for each ID, here's a potential solution:

=COUNTIFS($A$2:A2,A2,$D$2:D2,D2)=1"

请注意,代码部分没有翻译。

英文:

It's difficult to understand what you want because your example data set has distinct combinations of ID and Category, so even when you take a distinct count and a distinct sum, it would give the same values as an absolute count and absolute sum.

Also, a distinct sum based on ID only makes sense if each ID has a fixed Amount. If there are different rows where the ID is the same but the Amount is different, then it's not clear what a distinct sum should be.

So, using different example data where there are duplicate IDs for the same Category, but always with the same Amount for each ID, here's a potential solution:

=COUNTIFS($A$2:A2,A2,$D$2:D2,D2)=1

Excel Pivot Table and Measures – How Can I have Distinct Count of IDs and Distinct Sum of related values

Step 1 is to add a helper column that identifies whether each row contains the first unique combination of ID and Category. It does this by counting from the first row down the current row (using a range that is fixed to start from $A$2 but extends to A2, so that it expands as the formula fills down).

Excel Pivot Table and Measures – How Can I have Distinct Count of IDs and Distinct Sum of related values

Then add a pivot table as normal but set the First column to be used as a page filter, and filter it to TRUE. This then allows you to take a count of the ID column and a sum of the Amount column, and it only includes each unique combination once.

Excel Pivot Table and Measures – How Can I have Distinct Count of IDs and Distinct Sum of related values

huangapple
  • 本文由 发表于 2023年3月31日 22:16:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定