To calculate a median in Excel based on multiple criteria, use the MEDIAN function combined with IF in an array formula: =MEDIAN(IF((range1=criteria1)*(range2=criteria2), median_range)), entered with Ctrl+Shift+Enter in older Excel, or naturally in Excel 365. This returns the middle value of the subset of numbers that meet all specified conditions.
Why a Simple MEDIAN Isn’t Enough
The standard MEDIAN function in Excel ignores text and logical values but considers every numeric value in the range. When you need the median of a subset—say, sales figures for a specific region and product—you must first filter the data logically. Unlike AVERAGEIFS or SUMIFS, there is no built-in MEDIANIFS function in Excel (as of Microsoft 365). The workaround is to use an array formula that evaluates conditions row by row and passes only the matching numbers to MEDIAN.
How the Array Formula Works
Assume your data has regions in column A (A2:A100), products in column B (B2:B100), and sales in column C (C2:C100). To find the median sales for “North” region and “Widget” product, enter:
=MEDIAN(IF((A2:A100="North")*(B2:B100="Widget"), C2:C100))
In Excel 365 and Excel 2021, this works as a dynamic array formula—just press Enter. In older versions (Excel 2019 and earlier), you must press Ctrl+Shift+Enter to enter it as an array formula; Excel will show it enclosed in curly braces { } in the formula bar. The multiplication * acts as an AND operator: each condition returns TRUE/FALSE, which Excel converts to 1/0, so the product is 1 only when both are TRUE. The IF then returns the sales value for those rows, and MEDIAN calculates the middle of that array.
Using MEDIAN with OR Logic
If you need the median where at least one of several criteria is met (OR logic), replace the multiplication with addition +. For example, median sales for either “North” OR “South”:
=MEDIAN(IF((A2:A100="North")+(A2:A100="South"), C2:C100))
Be careful: if both conditions are TRUE for the same row, the sum becomes 2, which is still non-zero and thus TRUE in the IF test, so it works fine. However, this approach can double-count if you later use the result in other calculations—here it only affects the IF condition, not the median value.
Handling Blank Cells and Errors
Blank cells in the median range are ignored by MEDIAN, but text or errors in the criteria ranges can cause the formula to return a #VALUE! error. To avoid this, ensure your criteria ranges contain no errors. If your data has blank cells in the median range, they are simply skipped. If you want to exclude zeros (which are numeric and counted), add an extra condition like (C2:C100<>0) inside the IF.
For example, to exclude zeros and blanks (though blanks are already ignored): =MEDIAN(IF((A2:A100="North")*(C2:C100<>0), C2:C100)).
Alternative: Using AGGREGATE or Power Query
If you prefer a non-array formula, you can use AGGREGATE with function number 16 (MEDIAN) and option 7 (ignore errors and hidden rows). But AGGREGATE cannot apply criteria directly; you would need a helper column. A cleaner alternative is to use a PivotTable or Power Query to filter the data, then calculate the median on the filtered result. For dynamic filtering without formulas, Excel’s Filter feature (Data > Filter) lets you select criteria visually, and then you can use a simple MEDIAN on the visible cells—but note that MEDIAN ignores hidden rows only if you use SUBTOTAL or AGGREGATE, not the plain MEDIAN.
Common Mistakes and Troubleshooting
- Forgetting Ctrl+Shift+Enter in older Excel: If you press Enter only, you get a
#VALUE!error or a wrong result. Use Ctrl+Shift+Enter for legacy versions. - Mismatched range sizes: All ranges in the formula must have the same number of rows and columns. If they differ, you get a
#VALUE!error. - Using entire columns (e.g., A:A): This works in Excel 365 but can slow down older versions; it’s safer to use a defined range like A2:A1000.
- Criteria with wildcards: The
IFapproach does not support wildcards (* or ?). For partial matches, useISNUMBER(SEARCH("text", range))as a condition.
Comparing with AVERAGEIFS and Other Functions
Excel has dedicated functions for conditional averages (AVERAGEIFS) and sums (SUMIFS), but no MEDIANIFS. The array formula is the standard workaround. If you need multiple medians for many groups, consider using a PivotTable with the “Median” calculation—however, the default PivotTable value field does not offer median. You can add a calculated field or use the MEDIAN function in a separate table with UNIQUE and BYROW (Excel 365) to create dynamic group medians.
Frequently Asked Questions
Can I use MEDIANIFS directly?
No, Excel does not have a MEDIANIFS function. The array formula with IF is the accepted method. Some third-party add-ins offer it, but they are not part of standard Excel.
Why does my formula return #VALUE!?
This usually happens when the ranges are not the same size, or when you press Enter instead of Ctrl+Shift+Enter in Excel 2019 or earlier. Check that all ranges have the same number of cells and re-enter with the correct key combination.
How do I exclude zeros from the median?
Add a condition like (C2:C100<>0) inside the IF. For example: =MEDIAN(IF((A2:A100="North")*(C2:C100<>0), C2:C100)). This will only consider non-zero sales values.
Does this work on Mac Excel?
Yes, the formula is the same. The only difference is the keyboard shortcut: on Mac, press Control+Shift+Return to enter an array formula in older versions. In Excel 365 for Mac, just press Return.
Can I use wildcards in the criteria?
No, the IF-based array formula does not support wildcards. For partial text matches, use ISNUMBER(SEARCH("text", range)). For example, to match any product containing “Widget”: =MEDIAN(IF((A2:A100="North")*(ISNUMBER(SEARCH("Widget", B2:B100))), C2:C100)).
Is there a way to avoid array formulas?
Yes, you can use a helper column that flags rows meeting the criteria (e.g., =IF(AND(A2="North", B2="Widget"), C2, "")) and then use a regular MEDIAN on that helper column. This is simpler for beginners and avoids Ctrl+Shift+Enter.
Related
- How to Convert a Cell Range to a Table in Excel
- Tricky SQL Interview Queries: 15 Real Examples
- Google Sheets Personal Finance: Setup Guide
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Excel Wrap Text in Quotes? How to Add Quotation MarksYes, you can wrap text in quotation marks in Excel, and there are several ways to do it…
- Excel Text Equals: How to Compare Text Values in CellsIn Excel, use the equals sign in a formula to check whether two text values match: with the…
- Remove Formatting in Microsoft Word: A Step-by-Step GuideTo remove formatting in Microsoft Word, select the text, then choose Home > Clear All Formatting (the A…
- Convert an Excel File to a Google Doc: Step by StepTo turn an Excel file into a Google Doc, upload it to Google Drive, open it in Google…
- Snowflake SPLIT to Array: Syntax, Examples, and TipsYes. In Snowflake, use SPLIT(text, delimiter) to turn delimited text into an array; for example, SPLIT('red,green,blue', ',') returns…
- Google Sheets Conditional Formatting: If-Then RulesIn Google Sheets, an if-then conditional formatting rule checks whether a condition is true and automatically changes a…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.