[英]Excel: How to count total number of unique values based on a value from a different column
I am trying to calculate total number of orders based on a referral indicator. 我试图根据推荐指标计算订单总数。 In my table I have 3 columns.
在我的表中,我有3列。
order_id (a), sku (b), referral (c)
9073765908 19333476 EMP_BC
9073765908 18717916 EMP_BC
9073741228 23224786 EMP_BC
9073729088 15870816 EMP_BC
9073238838 15927306 EMP_BC
9073216258 23210836 EMP_BC
9073216258 18709096 EMP_BC
9073216258 18705756 EMP_BC
9073169568 19333496 EMP_BC
9073169168 18714626 EMP_BC
9072921778 22723516 EMP_ES
9072921778 18708696 EMP_ES
9072921778 15933746 EMP_ES
9072910758 23223526 EMP_BC
9072910758 23184416 EMP_BC
9072910758 21081716 EMP_BC
9072910758 18704496 EMP_BC
9072895968 21082646 EMP_ES
9072856628 28685906 EMP_BC
9072856628 23795646 EMP_BC
9072856628 21447106 EMP_BC
9072856628 15936966 EMP_BC
Here is all the things I've tried thus far: 这是迄今为止我尝试过的所有事情:
=COUNTIFS(C:C, "=EMP_BC")
However this returns total including the duplicates. 但是,这会返回总计,包括重复项。
Also tried this: 还试过这个:
=SUMPRODUCT(1/COUNTIFS(A:A, A:A, C:C, "=EMP_BC"))
This however, causes my excel to freeze... Im assuming this is due to the number of records I have... I have over 60K records. 然而,这导致我的excel冻结...我假设这是由于我有的记录数...我有超过60K的记录。
[edit] I am expected the following result: [编辑]我期望得到以下结果:
for EMP_BC: 8
because the following order_id is unique and the referral is EMP_BC:
9073765908
9073741228
9073729088
9073238838
9073216258
9073169568
9072910758
9072856628
I currently have a tab specifically setup so that it can have data copied and pasted in a tab. 我目前有一个专门设置的选项卡,以便它可以将数据复制并粘贴到选项卡中。 Another tab with all the formulas will reference the data and display the results.
包含所有公式的另一个选项卡将引用数据并显示结果。 This one metric is giving me a lot of problems.
这个指标给了我很多问题。
Any recommendations? 有什么建议?
Here is an array formula that should accomplish what you want. 这是一个应该完成你想要的数组公式。 To enter an array formula, hold down
ctrl-shift
while hitting enter
. 要输入的阵列式,按住
ctrl-shift
,同时击中enter
。 If you do this correctly, Excel will place braces {...}
around the formula. 如果您正确执行此操作,Excel将在公式周围放置大括号
{...}
。
=SUM(IFERROR(1/COUNTIFS(order_id,order_id,referral,"EMP_BC"),0))
This formula assumes the ranges comprise just the used range, and not the entire column. 此公式假设范围仅包含使用的范围,而不是整个列。
It is possible that a User Defined Function written in VBA will execute much faster, even with limited range references. 即使使用有限的范围引用,用VBA编写的用户定义函数也可能执行得更快。
To enter this User Defined Function (UDF), alt-F11
opens the Visual Basic Editor. 要输入此用户定义函数(UDF),
alt-F11
将打开Visual Basic编辑器。 Ensure your project is highlighted in the Project Explorer window. 确保在Project Explorer窗口中突出显示您的项目。 Then, from the top menu, select
Insert/Module
and paste the code below into the window that opens. 然后,从顶部菜单中选择“
Insert/Module
然后将下面的代码粘贴到打开的窗口中。
To use this User Defined Function (UDF), enter a formula like =UniqueOrdersByReferral(order_id,referral,"EMP_BC")
in some cell. 要使用此用户定义函数(UDF),请在某个单元格中输入类似
=UniqueOrdersByReferral(order_id,referral,"EMP_BC")
的公式。
Option Explicit
Option Compare Text 'Comment out this line to make case SENSITIVE
Function UniqueOrdersByReferral(rOrderID As Range, rReferral As Range, sReferralCode As String) As Long
Dim Col As Collection
Dim I As Long, vOID As Variant, vREF As Variant
vOID = rOrderID
vREF = rReferral
If UBound(vOID) <> UBound(vREF) Then
MsgBox "Order ID and Referral Ranges must be of same size"
Exit Function
End If
Set Col = New Collection
On Error Resume Next
For I = 1 To UBound(vOID)
If vREF(I, 1) = sReferralCode Then Col.Add vOID(I, 1), CStr(vOID(I, 1))
Next I
On Error GoTo 0
UniqueOrdersByReferral = Col.Count
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.