简体   繁体   English

Excel - 计算唯一值和过滤值

[英]Excel - Count Unique and Filtered Values

I'm trying to create a report to count the number of events based on a few conditions.我正在尝试创建一个报告来根据一些条件计算事件的数量。 This is the formula I've came up with so far:这是我到目前为止提出的公式:

=SUM(COUNTIFS('Sheet1'!B:B,"202204",'Sheet1'!M:M,{"This is text 1","This is text 2","This is text 3","This is text 4"})) =SUM(COUNTIFS('Sheet1'!B:B,"202204",'Sheet1'!M:M,{"这是文本 1","这是文本 2","这是文本 3","这是文字 4"}))

The formula above works well counting the total number of events but it doesn't remove the duplicates.上面的公式可以很好地计算事件总数,但它不会删除重复项。

The first condition is to select the period 202204 and then match the events with the text description.第一个条件是选择时间段 202204,然后将事件与文本描述进行匹配。 Each event has a unique number but it comes with various asset numbers in the next column in Sheet1 that I don't need for the report I'm creating.每个事件都有一个唯一的编号,但它在 Sheet1 的下一列中带有各种资产编号,我创建的报告不需要这些编号。 The reason why I only need to know/count the total number of events for each month based on the period and description.我只需要根据期间和描述知道/计算每个月的事件总数的原因。

This report is pulling data from another tab which is importing filtered data from a different spreadsheet (image below).此报告正在从另一个选项卡中提取数据,该选项卡正在从不同的电子表格中导入过滤后的数据(下图)。 The final goal here is to allow my team to simply click Refresh All data to automatically update the report.这里的最终目标是让我的团队只需单击 Refresh All data 即可自动更新报告。

The data in this spreadsheet has been edited for privacy reasons and the dataset is a lot bigger than what I'm showing.出于隐私原因,此电子表格中的数据已被编辑,数据集比我显示的要大得多。

dataset数据集

And this is the outcome that I need:这就是我需要的结果:

report - final outcome报告 - 最终结果

Thanks!谢谢!

Excel's unique function doesn't like to be counted does it! Excel的unique功能不喜欢被计算它! This task is probably better suited to using PowerQuery, but if you're looking for a one-liner then you can concatenate the columns, filter using unique and use concat to create one long string of your unique elements.此任务可能更适合使用 PowerQuery,但如果您正在寻找单线,那么您可以concatenate列,使用unique过滤并使用concat创建一个长字符串您的唯一元素。

From here you want to count unique elements for a certain period, denoted by a substring.从这里您想计算特定时期的唯一元素,用子字符串表示。 One of the easier ways to search the longer string for a substring's occurrences is to note the difference in the length before and after removing the substring using the substitute function, and then dividing that difference by the length of the substring.在较长的字符串中搜索子字符串出现的更简单的方法之一是注意使用substitute函数删除子字符串之前和之后的长度差异,然后将该差异除以子字符串的长度。 This is cleaner now that the let function exists in Office365.既然在 Office365 中存在let函数,这就更清楚了。

Putting all this together gives;将所有这些放在一起给出;

=LET(str,CONCAT(UNIQUE(CONCATENATE(B:B,M:M))),(LEN(str)-LEN(SUBSTITUTE(str,"202204","")))/LEN("220204"))

The CONCAT(UNIQUE(CONCATENATE creates the long string, and LET assigns it the name str , and then we use the difference in the length of str before and after removing the substring and divide to count how many times 202204 occurs. CONCAT(UNIQUE(CONCATENATE创建长字符串, LET将其命名为str ,然后我们使用删除子字符串前后str长度的差异并除以计算 202204 出现的次数。

If you don't have Office 365, then you need to create the longer string twice, so the formula is a little longer (and slower);如果你没有 Office 365,那么你需要创建两次较长的字符串,所以公式会更长一些(也更慢);

=(LEN(CONCAT(UNIQUE(CONCATENATE(B:B,M:M))))-LEN(SUBSTITUTE(CONCAT(UNIQUE(CONCATENATE(B:B,M:M))),"202204","")))/LEN("220204")

I like to "externalize" conditions - in this case the valid descriptions.我喜欢“外部化”条件——在这种情况下是有效的描述。

You can use the following formula:您可以使用以下公式:

=LET(filteredList, FILTER(tblData,(COUNTIF(tblFilter[valid Description],tblData[Description]))*(tblData[Date]=[@Date])),
resizedList,FILTER(filteredList,{1,1,1,0}),
uniqueList,UNIQUE(resizedList),
ROWS(uniqueList))

filteredList returns a list for the according date and the valid descriptions filteredList列表返回相应日期和有效描述的列表

resizedList removes the description column resizedList移除描述列

uniqueList can then return the unique values for Date, event, assest (as description is omitted)然后, uniqueList可以返回 Date、event、assest 的唯一值(因为省略了描述)

Then the number of unique rows is returned.然后返回唯一行数。

This is my setting:这是我的设置:

在此处输入图像描述

I found a solution at Excel Forum for my own problem that I would like to share here, although I tried both above which didn't work or were complex to my current skills.我在 Excel 论坛上为我自己的问题找到了一个解决方案,我想在这里分享,尽管我尝试了以上两种方法都不起作用或者对我目前的技能来说很复杂。 I'm thankful for you taking the time to help me.我很感谢你抽出时间来帮助我。

The formula is:公式为:

=SUM(SIGN(COUNTIFS('Sheet1'!$B$2:$B$2000,TEXT('Column Name A'!D2,"yyyymm"),'Column Name B'!$C$2:$C$2000,UNIQUE('Column Name B'!$C$2:$C$2000),'Column Name C'!$M$2:$M$2000,{"This is text 1","This is text 2","This is text 3","This is text 4"}))) =SUM(SIGN(COUNTIFS('Sheet1'!$B$2:$B$2000,TEXT('列名 A'!D2,"yyyymm"),'列名 B'!$C$2:$C$2000,UNIQUE( '列名 B'!$C$2:$C$2000),'列名 C'!$M$2:$M$2000,{"这是文本 1","这是文本 2","这是文本 3" "这是文本 4"})))

The column names and other details were changed for privacy reasons but this is the syntax that made more sense to me and has been working well.出于隐私原因更改了列名和其他详细信息,但这种语法对我来说更有意义并且运行良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM