Power Query List.Median Function: How to Use It
In this article
The LIST.MEDIAN function in Power Query calculates the middle value of a list of numbers, returning the exact median for odd-count lists or the average of the two middle values for even-count lists. It requires a list input (e.g., a column reference or manually created list) and only works with numeric data; non-numeric or null values cause errors unless handled first.
What You'll Need
- A Power Query environment (available in Excel, Power BI Desktop, or Microsoft Fabric)
- A column or list containing numeric values (integers, decimals, or a mix)
- Basic familiarity with Power Query Editor (opening it, referencing columns, adding custom columns)
Step-by-Step
- Open Power Query Editor: In Excel, go to the Data tab and select Get Data > Launch Power Query Editor; in Power BI, click Transform Data in the Home tab.
- Load or select your data: Ensure your table contains a numeric column (e.g., Sales, Score, or Price). If not, create or append data first.
- Identify the list source: Decide whether you want the median of an entire column, a filtered subset, or a manually defined list. For column-level median, proceed to step 4; for grouped or custom lists, see step 5.
- Calculate median for a single column:
- Click on the column header (e.g., Revenue) to select it.
- Right-click the column header and choose Reference (this creates a new query referencing the original table) OR proceed directly in the current query if you're comfortable modifying it.
- In the formula bar (top of the editor), replace the existing formula with:
= List.Median(YourTableName[ColumnName]). For example, if your table is named SalesData and the column is Amount, type= List.Median(SalesData[Amount]). - Press Enter. Power Query evaluates the list, sorts it internally, and returns a single numeric value (e.g., 42.5).
- Calculate median for a filtered or grouped subset:
- Apply filters to narrow your data (e.g., filter rows where Region = North).
- Right-click the column header of your numeric field and select Advanced Sort > Ascending to verify sorting (optional but helpful for debugging).
- Click Close & Load To > Only Create Connection if you plan to use this filtered list in another step.
- Create a new blank query: Go to Home > New Source > Blank Query.
- In the formula bar, enter:
= List.Median(FilteredTableName[ColumnName]), replacing names as needed. - To compute medians per group (e.g., per salesperson), use Group By first: Select Home > Group By, choose your grouping column (e.g., Salesperson) and aggregate function All Rows. Then, in the new grouped table column, expand it and add a custom column with
= List.Median([Amount]).
- Handle list creation manually (for static lists or derived values):
- Open a blank query.
- In the formula bar, enter a list directly:
= {10, 20, 30, 40, 50}. - Wrap it in the median function:
= List.Median({10, 20, 30, 40, 50}). - Press Enter to see the result (30).
- Load the result: Click Close & Load to send the median value to Excel (as a single cell) or Power BI (as a card or table), depending on your destination.
Common Mistakes
- Using non-numeric data: Including text, dates, or nulls in the list causes a Expression.Error: We cannot apply operator - to types Text and Text or similar. Always filter or convert first using
Table.SelectRowsorTable.TransformColumns. - Forgetting brackets in column references: Typing
= List.Median(SalesData.Amount)instead of= List.Median(SalesData[Amount])results in a syntax error. Square brackets are mandatory for column access in record-based tables. - Assuming automatic aggregation:
LIST.MEDIANdoes not aggregate by group unless explicitly grouped. Using it on a table (not a list) returns an error. - Overlooking empty lists: If your filter returns zero rows,
List.Median({})throws Expression.Error: The argument is empty. Pre-check withif List.IsEmpty(...) then null else List.Median(...).
Tips
- Pre-filter before calculating: Use
Table.SelectRowsto remove blanks or outliers first:= List.Median(Table.SelectRows(Source, each [Value] <> null and [Value] > 0)[Value]). - Combine with List.Sort for verification: To debug, create a step like
= List.Sort(Source[Value])and compare its length and middle values to your median result. - Use in calculated columns: If you need the median value repeated for every row in a table, add a custom column with:
= List.Median(#"PreviousStep"[Value])(note: this recalculates per row, which can be slow on large datasets). - Handle decimals:
LIST.MEDIANreturns decimals for even-count lists (e.g., (10+20)/2 = 15.0). UseNumber.Roundif you need integers:= Number.Round(List.Median(...), 0). - Check data types: Right-click the column header > Data Type > ensure it's Decimal Number or Whole Number. Text-formatted numbers (e.g., "100") must be converted with
Table.TransformColumns(..., {"Column", each Number.From(_), type number}).
Troubleshooting
If you encounter an error, follow these steps in order:
- Verify the input is a list: Hover over the step name in the applied steps pane. If the result type shows Table instead of list, you passed a table instead of a list column. Drill down: add
[ColumnName]to extract the list. - Check for hidden non-numeric values: In the source table, sort the numeric column. Look for text entries (left-aligned in Excel), blank cells, or error values (e.g., #N/A). Remove or convert them before calculating.
- Test with a small list: Create a new blank query with
= List.Median({1, 2, 3}). If this works, the issue is with your data source or column reference. - Inspect the formula bar syntax: Ensure all parentheses, brackets, and quotes match. Common typos include missing commas in lists or using round brackets instead of square for column access.
- Handle empty results: Wrap the function in a conditional:
= if List.Count(Source[Value]) = 0 then null else List.Median(Source[Value]). - Restart Power Query: Close and reopen Power Query Editor. Occasionally, cached metadata causes stale errors.
Frequently Asked Questions
Does List.Median ignore null values?
No. LIST.MEDIAN includes nulls as invalid and throws an error. You must remove them first using Table.SelectRows(Source, each [Column] <> null) or replace them with zeros via Table.ReplaceValue.
Can I use List.Median with text columns?
No. It only accepts numeric lists. Attempting this returns a type mismatch error. Convert text to numbers first using Number.From if the text represents valid numeric values (e.g., "123.50").
Why does my median differ from Excel's MEDIAN function?
Power Query’s LIST.MEDIAN uses the same mathematical definition: for even counts, it averages the two middle numbers. Differences arise if Power Query includes/excludes rows due to filtering, data type mismatches, or hidden characters (e.g., spaces in text numbers). Verify row counts and data types match.
How do I calculate the median across multiple columns?
Combine columns into a list first. For example, with columns A and B, use: = List.Median(List.Combine({Source[A], Source[B]})). Ensure both columns are numeric to avoid errors.
Does List.Median work with dates?
No. LIST.MEDIAN expects numbers. To find the median date, convert dates to serial numbers (e.g., in Excel, dates are stored as integers); calculate the median; then convert back using Date.From. For example: = Date.From(List.Median(Table.TransformColumns(Source, {"DateCol", each Date.ToNumber(_), Int64.Type})[DateCol])).
Can I use List.Median in a custom column definition?
Yes, but it recalculates the median for the entire column on every row, which is inefficient for large tables. For per-row group medians, use Group By instead. If you must use a custom column, reference a pre-calculated median value stored in a variable or separate query to avoid repeated computation.