Median if not excel

Combine the MEDIAN and IF functions in an array formula

This tutorial example uses a MEDIAN IF array formula to find the middle tender for two different projects. The nature of the formula permits us to search for multiple results simply by changing the search criterion (in this tutorial example, the project name).

The information in this article applies to Excel 2019, Excel 2016, Excel 2013, Excel 2010, Excel 2007, and Excel for Mac.

About the MEDIAN and IF Functions

The job of each part of the formula is:

  • The MEDIAN function finds the middle value for a project.
  • The IF function allows us to choose which project we want a tender for by setting a condition using the project names.
  • The array formula lets the IF function test for multiple conditions in a single cell. When the condition is met, the array formula determines what data (project tenders) the MEDIAN function will examine to find the middle tender.

Excel CSE Formulas

Array formulas are created by pressing the Ctrl+Shift+Enter keys on the keyboard at the same time once the formula has been typed in. Because of the keys pressed to create the array formula, they are sometimes referred to as CSE formulas.

MEDIAN IF Nested Formula Syntax and Arguments

The syntax and arguments for the MEDIAN IF formula are as follows:

=MEDIAN(IF(logical_test,value_if_true,value_if_false))

Since the IF function is nested inside the MEDIAN function, the entire IF function becomes the sole argument for the MEDIAN function.

The arguments for the IF function are:

  • logical_test (required): A value or expression that is tested for a Boolean value of TRUE or FALSE.
  • value_if_true (required): The value that displays if logical_test is true.
  • value_if_false (optional): The value that displays if logical_test is false.

Excel’s MEDIAN IF Array Formula Example

The following example searches tenders for two different projects to find the middle or median tender. The arguments for the IF function accomplish this by setting the following conditions and results:

  • The logical test finds a match for the project name typed into cell D10 of the worksheet.
  • The value_if_true argument is, with the help of the MEDIAN function, the middle tender for the chosen project.
  • The value_if_false argument is omitted since it is not needed and its absence shortens the formula. If a project name that is not in the data table (such as Project C) is typed into cell D10, the formula returns a zero value.

Enter the Tutorial Data in Excel

  1. Enter the example data, as shown above, into a blank Excel worksheet.

  2. In cell D10, type Project A. The formula will look in this cell to find which project to match.

Enter the MEDIAN IF Nested Formula

When you create both a nested formula and an array formula, the entire formula must be typed into a single worksheet cell. When the formula is complete, do not press the Enter key or select a different cell because the formula will be turned into an array formula.

A #VALUE! error means that the formula was not entered correctly as an array.

  1. Select cell E10. This is where the formula results will display.

  2. Type the following formula in the cell:

    =MEDIAN(IF(D3:D8=D10,E3:E8))
    
  3. Press and hold the Ctrl and Shift keys.

  4. Press the Enter key to create the array formula.

  5. The answer 15875 ($15,875 with formatting) appears in cell E10 since this is the middle tender for Project A.

Test the Formula

Test the formula by finding the middle tender for Project B. Type Project B into cell D10 and press the Enter key.

The formula returns the value of 24365 ($24,365) in cell E10.

Thanks for letting us know!

Get the Latest Tech News Delivered Every Day

Subscribe

I need to return a median of only a certain category on a spread sheet. Example Below

Airline    5
Auto       20
Auto       3
Bike       12
Airline    12
Airline    39

ect.

How can I write a formula to only return a median value of the Airline Categories. Similar to Average if, only for median. I cannot re-arrange the values. Thank you!

Richard Marskell - Drackir's user avatar

asked Jul 19, 2011 at 19:09

Alan's user avatar

2

Assuming your categories are in cells A1:A6 and the corresponding values are in B1:B6, you might try typing the formula =MEDIAN(IF($A$1:$A$6="Airline",$B$1:$B$6,"")) in another cell and then pressing CTRL+SHIFT+ENTER.

Using CTRL+SHIFT+ENTER tells Excel to treat the formula as an «array formula». In this example, that means that the IF statement returns an array of 6 values (one of each of the cells in the range $A$1:$A$6) instead of a single value. The MEDIAN function then returns the median of these values. See http://www.cpearson.com/excel/arrayformulas.aspx for a similar example using AVERAGE instead of MEDIAN.

Community's user avatar

answered Jul 19, 2011 at 19:22

Brian Camire's user avatar

Brian CamireBrian Camire

4,6452 gold badges26 silver badges23 bronze badges

2

Make a third column that has values like:

=IF(A1=»Airline»,B1)

=IF(A2=»Airline»,B2)
etc

Then just perform a median on the new column.

answered Jul 19, 2011 at 19:23

heisenberg's user avatar

heisenbergheisenberg

9,5971 gold badge30 silver badges38 bronze badges

Expanding on Brian Camire’s Answer:

Using =MEDIAN(IF($A$1:$A$6="Airline",$B$1:$B$6,"")) with CTRL+SHIFT+ENTER will include blank cells in the calculation. Blank cells will be evaluated as 0 which results in a lower median value. The same is true if using the average funtion. If you don’t want to include blank cells in the calculation, use a nested if statement like so:

=MEDIAN(IF($A$1:$A$6="Airline",IF($B$1:$B$6<>"",$B$1:$B$6)))

Don’t forget to press CTRL+SHIFT+ENTER to treat the formula as an «array formula».

answered Sep 16, 2019 at 4:45

JordanRussellSmith's user avatar

one solution could be to find a way of pulling the numbers from the string and placing them in a column of just numbers the using the =MEDIAN() function giving the new number column as the range

answered Jul 19, 2011 at 19:14

jth41's user avatar

jth41jth41

3,7989 gold badges59 silver badges109 bronze badges

The MEDIANIF / MEDIAN IF formula is not natively available in Excel as opposed to familiar functions in Excel such as AVERAGEIF or SUMIF. The MEDIANIF function should provide us, for a population of numbers, with the middle number that splits the population into 2 halves. Additionally it should allow us to apply a filter so that the Median operation is conducted on only a subset of the population i.e. using an IF statement. Below I will provide 2 ways of solving this missing formula.

Excel MEDIAN IF via Array Formula

The first approach is to use Excel Array Formulas by combining a regular MEDIAN function with an IF function. The formula we will use is:

{ =MEDIAN( IF( criteria , median_range ) ) } 

where:

  • criteria – is a logical condition involving an Excel Range. If true for a value in this Range the relevant cell in the median_range will be used to calculate the median
  • median_range – the Excel Range of value containing the population we want to filter (apply criteria) to calculate the median

Notice the { } brackets. This is because this is an Excel Array Formula.

An Excel Array Formula is a formula that performs calculations on not a single cell (as standard formulas) but on multiple cells. Array Formulas usually internally can return an array as a result of their calculations instead of a single value. The can be further processed to produce a single value like in our example below. Read more here.

MEDIAN IF Array Formula example

Now let us put this to work. Imagine we have a range of positive and negative values. However, we want to calculate the median only for positive values (e.g. sales and returns, but we want only the sales median):
Excel Median IF example

Enter the formula

We will start by entering the following formula below.

=MEDIAN(IF(A4:A14>0,A4:A14))

What this defines is we only want an array of A4:A14 cells that are greater than 0. The problem is that the formulas work on arrays not single cells. The IF formula does not normally accept a range it is expecting a single cell instead. Therefore to make this work for an array we need to tell Excel that this is an Array Formula

Make it an Array Formula

Instead of hitting Enter hit these 3 keys ALT+SHIFT+ENTER to make it an Array Formula. Now Excel will know that it needs to split this formula in memory for each cell in the provided range and then to merge it back. Otherwise it would show you an error message.

Excel MEDIAN IF using VBA UDF

In case you want to use the MEDIANIF function directly like you would use an AVERAGEIF function we can resort to a custom VBA UDF Function (UDF = User Defined Function). See how this would look like for the same example below:
Excel MEDIANIF using VBA UDF

Below I will show you how to implement MEDIANIF using VBA UDF functions. I am assuming you already have your Developer ribbon visible. If not see my tutorial here.

  • Add a new Module to your VBA Project
  • Add the code snippet below to the new module
  • Save the file as XLSM

The VBA code snippet can be copied from below:

Public Function MEDIANIF(rng As Range, criteria As String, Optional median_range As Range = Nothing)
    Dim medRng As Range, tmp As Range
    Dim firstRow As Long, firstCol As Long
    firstRow = rng.Cells(1, 1).Row
    firstCol = rng.Cells(1, 1).Column
    
    For Each tmp In rng.Cells
        If Application.Evaluate(vbNullString & tmp.Value & criteria) Then
            If median_range Is Nothing Then
                If medRng Is Nothing Then
                    Set medRng = tmp
                Else
                    Set medRng = Union(medRng, tmp)
                End If
            Else
                If medRng Is Nothing Then
                    Set medRng = median_range.Cells(tmp.Row - firstRow + 1, tmp.Column - firstCol + 1)
                Else
                    Set medRng = Union(medRng, median_range.Cells(tmp.Row - firstRow + 1, tmp.Column - firstCol + 1))
                End If
            End If
        End If
    Next tmp
    
    MEDIANIF = Application.WorksheetFunction.median(medRng)
End Function

See the result in the VBA Project:
MEDIANIF UDF VBA Project
The code may seem complicated but what it does is use the VBA Evaluate function to run the criteria on each cell of the criteria range (rng). If the condition is true then the corresponding cell from the median_range will be added to the population for which the median is to be calculated.

Notice that median_range is optional. This is because by default the criteria range (rng parameter) is assumed to be the median range as well – unless the parameter is provided.

See parameters for the MEDIANIF UDF

Excel VBA UDF functions by default do not show you those fancy function tooltips. However you can click on the fx button marked in red below and a familiar window will show up for you to provide the arguments.
Excel function tip - MEDIANIF VBA UDF
All in all the MEDIANIF User Defined Function will make using it much more seamless than having to figure out the shortcut and logic for Array Formulas. The code above can also be easily adjusted for just about any type of function with conditions i.e. MODEIF etc.

Excel MEDIAN IF Array Formula vs VBA UDF

So which one is better? As much as I love the elegance of the VBA UDF….from a practical and performance point of view if you are Excel savy it will be more convenient to use the Excel Array Formula version. In case you need to embed a lot of these MEDIAN IF statements performance impact might be visible as well – because VBA User Defined Functions do not allow usage of multiple cores (no multithreading). One additional (slight) difference is that you will need to always save your file as a XLSM (Macro enabled file) otherwise the function logic will be lost.

What is your take?

Excel for Microsoft 365 Excel for Microsoft 365 for Mac Excel for the web Excel 2021 Excel 2021 for Mac Excel 2019 Excel 2019 for Mac Excel 2016 Excel 2016 for Mac Excel 2013 Excel 2010 Excel 2007 Excel for Mac 2011 Excel Starter 2010 More…Less

This article describes the formula syntax and usage of the MEDIAN function in Microsoft Excel.

Description

Returns the median of the given numbers. The median is the number in the middle of a set of numbers.

Syntax

MEDIAN(number1, [number2], …)

The MEDIAN function syntax has the following arguments:

  • Number1, number2, …    Number1 is required, subsequent numbers are optional. 1 to 255 numbers for which you want the median.

Remarks

  • If there is an even number of numbers in the set, then MEDIAN calculates the average of the two numbers in the middle. See the second formula in the example.

  • Arguments can either be numbers or names, arrays, or references that contain numbers.

  • Logical values and text representations of numbers that you type directly into the list of arguments are counted.

  • If an array or reference argument contains text, logical values, or empty cells, those values are ignored; however, cells with the value zero are included.

  • Arguments that are error values or text that cannot be translated into numbers cause errors.

Note: The MEDIAN function measures central tendency, which is the location of the center of a group of numbers in a statistical distribution. The three most common measures of central tendency are:

  • Average     which is the arithmetic mean, and is calculated by adding a group of numbers and then dividing by the count of those numbers. For example, the average of 2, 3, 3, 5, 7, and 10 is 30 divided by 6, which is 5.

  • Median     which is the middle number of a group of numbers; that is, half the numbers have values that are greater than the median, and half the numbers have values that are less than the median. For example, the median of 2, 3, 3, 5, 7, and 10 is 4.

  • Mode     which is the most frequently occurring number in a group of numbers. For example, the mode of 2, 3, 3, 5, 7, and 10 is 3.

For a symmetrical distribution of a group of numbers, these three measures of central tendency are all the same. For a skewed distribution of a group of numbers, they can be different.

Example

Copy the example data in the following table, and paste it in cell A1 of a new Excel worksheet. For formulas to show results, select them, press F2, and then press Enter. If you need to, you can adjust the column widths to see all the data.

Data

1

2

3

4

5

6

Formula

Description

Result

=MEDIAN(A2:A6)

Median of the 5 numbers in the range A2:A6. Because there are 5 values, the third is the median.

3

=MEDIAN(A2:A7)

Median of the 6 numbers in the range A2:A7. Because there are six numbers, the median is the midway point between the third and fourth numbers.

3.5

Need more help?

Want more options?

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.

The MEDIAN function has no built-in way to apply criteria. Given a range, it will return the MEDIAN (middle) number in that range.

To apply criteria, we use the IF function inside MEDIAN to «filter» values. In this example, the IF function filters by group like this:

IF(group=E5,data)

This expression compares each value in the named range «group» against the value in E5 («A»). Because the criteria is applied to an array with multiple values, the result is an array of TRUE FALSE values like this:

{TRUE;TRUE;TRUE;TRUE;TRUE;FALSE;FALSE;FALSE;FALSE;FALSE}

In this array each TRUE corresponds to a value in group A. The IF function evaluates these results and returns the corresponding value from the named range «data». The final result from IF is

{1;2;3;3;5;FALSE;FALSE;FALSE;FALSE;FALSE}

Notice only values in group A have survived, and group B values are now FALSE. This array is returned to the MEDIAN function, which automatically ignores FALSE values and returns median value, 3.

Note: when IF is used this way to filter values with an array operation, the formula must be entered with control + shift + enter.

Additional criteria

To apply more than one criteria, you can nest another IF inside the first IF:

{=MEDIAN(IF(criteria1,IF(criteria2,data)))}

To avoid extra nesting, you can also use boolean logic in the criteria.

Понравилась статья? Поделить с друзьями:
  • Media player and excel
  • Meaning of word streamed
  • Media capital of the word
  • Meaning of word stemming
  • Medals of the word