Search code examples
excelexcel-formulaexcel-2010excel-2007vba

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


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

Solution

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

        AGGREGATE MINIF

    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.