Extracting the two smallest values corresponding to the unique-to-the-column ID

馋奶兔 提交于 2019-12-11 11:46:06

问题


I have two rows, that is: ID and Time. I want to extract the two smallest Time value corresponding to the unique column ID.

ID  Time
27604   13:00:12
27604   13:00:32
27604   13:00:34
27604   13:00:38
27604   13:00:41
27604   13:00:47
27604   13:00:50
27605   13:00:20
27605   13:00:23
27605   13:00:39
27605   13:00:42
27605   13:00:45
27605   13:00:48
27605   13:00:54
27605   13:00:57
27605   13:01:00
27606   13:00:49
27606   13:00:52
27606   13:00:55
27606   13:01:01
27606   13:01:04
27606   13:01:07

For ID 27604 I want to extract 13:00:12 and 13:00:32 only.

For ID 27605 I want to extract 13:00:20 and 13:00:23 only.

For ID 27606 I want to extract 13:00:27 and 13:00:30 only.

I want to extract all this values as below:

27604   13:00:12
27604   13:00:32
27605   13:00:20
27605   13:00:23
27606   13:00:49
27606   13:00:52

回答1:


You can achieve the SMALL values (with criteria) easily with the AGGREGATE¹ function's SMALL sub-function (e.g. 15).

    

The formula in E4 is,

=AGGREGATE(15, 6, (B:B)/(A:A=D4), COUNTIF(D$4:D4, D4))

Fill down as necessary. Since we are using the SMALL sub-function, we can easily retrieve the second, third, etc. ratios by increasing the k parameter as I've done with a COUNTIF function implementing a progressive range and floating criteria.

The 6 is the AGGREGATE parameter for ignoring error values. By dividing the time by whether or not column A is the correct ID we are producing #DIV/0! errors for anything we do not want considered leaving them ignored.

¹The AGGREGATE¹ function's was introduced with Excel 2010. It is not available in previous versions.




回答2:


I used this array formula starting in C2 to get the pairs of ID's

=IFERROR(INDEX(A$2:A$10,MATCH(TRUE,(COUNTIF(C$1:C1,A$2:A$10)<2),0)),"")

and this one starting in D2 to get the first two times

=SMALL(IF(A$2:A$10=C2,B$2:B$10),COUNTIF(C$2:C2,C2))

must be entered using CtrlShiftEnter

tested on a subset of data. Must be at least two times for each ID.

Should work on most versions of Excel.



来源:https://stackoverflow.com/questions/32134569/extracting-the-two-smallest-values-corresponding-to-the-unique-to-the-column-id

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