Search code examples
arraysexcelif-statementmodeifs

MODE IFS in excel - applying criteria to MODE function and excluding a number from the series


I am using MODE IF in Excel, which seems to work fine, but I need it to exclude from the MODE all number 2's.

The Formula I am using is as follows:

=IFERROR(MODE(IF($I:$I=I4,$K:$K)),K4)

entered with Ctrl+Shift for an array formula.

The IFERROR bit is there for when there is only one occurrence of a number.

Could anyone please tell me how to develop this formula so that, where the result would normally be a "2", that it finds the next highest occurring number instead?

Many thanks Rich


Solution

  • Try the following array formula**:

    =MODE(IF($I1:$I20=I4,IF($K1:$K20<>{2,2},$K1:$K20)))

    for which, incidentally, it is not necessary to include an IFERROR clause.

    I strongly recommend that you don't use entire column references within an array formula. Unlike COUNTIF, SUMIF, COUNTIFS or SUMIFS, for example, array formulas calculate over all cells passed to them, whether technically beyond the last-used cells in those ranges or not.

    So if, for example, you only have data extending as far as row 1000, then, by referencing an entire column's worth of rows, you are effectively forcing Excel to calculate more than one million rows than are actually necessary, resulting in an astonishingly resource-heavy formula. And that's just for one instance of that formula.

    With some functions you can get away with referencing entire columns with no detriment to performance, though not with functions which operate over arrays, such as AGGREGATE, SUMPRODUCT, and any construction requiring CSE.

    As such, you should either choose a suitably low, though sufficient, upper bound for the end row being referenced or, even better, makes your ranges dynamic, such that they automatically adjust as your data expands/contracts.

    I chose an end row reference of 20 here, which obviously you can amend to meet your requirements, though you should be careful not to make it too arbitrarily large (and certainly don't reference entire columns!), since, as mentioned, for each additional cell referenced, extra calculation will be required.

    Regards

    **Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).