Excel公式,统计第一周内的出现次数

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

Excel formula, count occurrences in the first week

问题

我需要找到一种方法,在数据透视表中显示特定产品在其首周出现的次数。以下是表格模型。这并不是那么简单,但是是一个有效的示例,我大约有20万行数据(我不能使用SQL或任何其他软件)。

收到日期 产品 工厂 制造日期 过期日期
2022/01/12 48 2022 Y A 2022/10/10 2023/10/10
2023/02/06 22 2023 X A 2022/10/10 2023/10/10
2023/03/06 22 2023 X A 2022/10/10 2023/10/10
2023/10/06 23 2023 X A 2022/10/10 2023/10/10
2023/10/06 23 2023 X A 2022/10/10 2023/10/10
2023/06/22 25 2023 Y A 2022/10/10 2023/10/10

在透视表中,应该是这样的,当我点击数字2或1时,它应该列出所有出现的情况:

制造 产品 制造日期 首周出现次数
A X 2022/10/10 2
A Y 2022/10/10 1

我知道如何找到第一次出现的情况;它会是这样的:

在特定列中,使用以下公式进行连接:

=CONCATENATE('产品' & '工厂' & '制造日期' & '过期日期')

然后在另一个单元格中,使用以下公式来计算连接的公式,并检查是否是第一次出现:

=IF( COUNT.IF( '连接列这里':'连接列这里' ), { '连接列这里' ) > 1 ), { "多于1次出现" ), { '第一次出现' )

我真的在尝试找到一种方法,只计算特定产品的最早周数,但我找不到方法。

英文:

I need to find a way to display in a pivot table how many occurrences of specific product that happened in his first week of appearance. Below is the table's model. It's not that simple, but a valid example, I have something around 200k rows (I can't use SQL or any other software).

Date Received Week Year Product Factory Manufactury Date Expiration Date
01/12/2022 48 2022 Y A 10/10/2022 10/10/2023
02/06/2023 22 2023 X A 10/10/2022 10/10/2023
03/06/2023 22 2023 X A 10/10/2022 10/10/2023
10/06/2023 23 2023 X A 10/10/2022 10/10/2023
10/06/2023 23 2023 X A 10/10/2022 10/10/2023
22/06/2023 25 2023 Y A 10/10/2022 10/10/2023

It should be something like this on pivot table, and when I click on number 2 or 1 it should list all occurrences:

Manufactury Product Manufactury Date occurrences first week
A X 10/10/2022 2
A Y 10/10/2022 1

I know how to find the first occurrences; it would be something like this:

In a specific column a formula for concatenate:

=CONCATENATE('Product' & 'Factory' & 'Manufactury Date' & 'Expiration Date') 

And in another cell a formula to count the concatenate formula and check if it is the first occurrences:

=IF(
    COUNT.IF(
'CONCATENATECOLUMNHERE': 'Z LINE OF CONCATENATECOLUMNHERE'
    ),
{ 'Z LINE OF CONCATENATECOLUMNHERE'
    ) > 1
),
{ "MORE THAN 1 OCCURRENCE"
),
{ 'FIRST OCCURRENCE'
) 

I'm really trying to find a way to count only the oldest week for a specific product, but I can't find a way.

答案1

得分: 4

以下是翻译好的部分:

在数据透视表中,没有直接计算这个的方法。

您可以在源数据中添加一个字段来检查该行是否在产品的前7天内:

我使用了一个数组公式创建了字段 test,但我相当肯定在 Excel 365 上更容易实现:

=IF(AND(A2>=MIN(IF($D$2:$D$7=D2;$A$2:$A$7));A2<=(MIN(IF($D$2:$D$7=D2;$A$2:$A$7))+6));1;0)

因为这是一个数组公式,必须按 CTRL+ENTER+SHIFT 输入,否则它将无法工作!

有了这个新字段,然后在数据透视表中将其用作筛选器:

更新: 当 OP 说“首次出现的第一周”时,意味着日期的自然周的第一周,而不是连续的7天。所以更新后的数组公式如下:

=IF(YEAR(A2)&WEEKNUM(A2;2)=YEAR(MIN(IF($D$2:$D$7=D2;$A$2:$A$7)))&WEEKNUM(MIN(IF($D$2:$D$7=D2;$A$2:$A$7));2);1;0)

这个公式将比较每个“收到日期”的年份和周数与每个产品的最小“收到日期”的年份和周数。然后按照之前的方法制作数据透视表。

03/06/2023 替换 05/06/2023 进行测试(这两个日期在不同的周):

请注意,我使用 WEEKNUM 将星期一设置为每周的第一天,但您可以根据需要进行更改。

此外,检查年份值是强制性的,因为不同的年份可以具有相同周数的相同日期,但它们实际上不是相同的周。

英文:

There is no way you can calculate that directly in the Pivot table itself.

You could add a field on your source data to check if that row would be inside the first 7 days of a product:

Excel公式,统计第一周内的出现次数

I did field test using an array formula but I'm pretty sure it's easier on Excel365:

=IF(AND(A2&gt;=MIN(IF($D$2:$D$7=D2;$A$2:$A$7));A2&lt;=(MIN(IF($D$2:$D$7=D2;$A$2:$A$7))+6));1;0)

Because it's an array formula it must be entered pressing <kbd>CTRL</kbd>+<kbd>ENTER</kbd>+<kbd>SHIFT</kbd> or it won't work!

With this new field then use it as a filter in your pivot table:

Excel公式,统计第一周内的出现次数

UPDATE: When OP said first week of appearance meaned the first natural week of the date, not a period of 7 days. so the updated array formula is:

=IF(YEAR(A2)&amp;WEEKNUM(A2;2)=YEAR(MIN(IF($D$2:$D$7=D2;$A$2:$A$7)))&amp;WEEKNUM(MIN(IF($D$2:$D$7=D2;$A$2:$A$7));2);1;0)

This formula will compare the year and week number of each Date received versus the year and week number of the minimun Date received for each product. Then make the pivot table as explained before.

Excel公式,统计第一周内的出现次数

Test replacing 03/06/2023 with 05/06/2023 (both dates are on different weeks):

Excel公式,统计第一周内的出现次数

Notice I'm using WEEKNUM setting Monday as first day of week, but you can change this based on your needs.

Also, it's mandatory to check the year value because different years can have same date in same week number, but it wouldn't be really the same week.

huangapple
  • 本文由 发表于 2023年7月11日 03:34:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656794.html
匿名

发表评论

匿名网友

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

确定