List.RemoveNulls Function in Power Query M

Sumit Bansal
Written by

Sumit Bansal is the founder of TrumpExcel.com and a 13-time Microsoft Excel MVP. He started this site in 2013 to share his passion for Excel through easy tutorials, tips, and training videos, helping you master Excel, boost productivity, and maybe even enjoy spreadsheets!

The List.RemoveNulls function in Power Query removes every null value from a list and keeps everything else in the same order. A blank cell in your data comes in as null, so this is how you get a list of only the values that are actually there.

This matters because some list functions treat null as a real value. So a most-common-value check can come back as null, and a “last value” lookup can land on an empty row.

Follow along with the example file

List.RemoveNulls Function Examples.xlsx

Download

List.RemoveNulls Function Syntax

List.RemoveNulls(list as list) as list
  • list – the list you want to remove the null values from. It can be a list you type in curly brackets, such as {1,null,3}, or a column of a table.

What it returns: A new list with all the null values removed. The other values stay in their original order, and if the list has no nulls, you get the same list back.

The List.RemoveNulls function is part of the Power Query M language, so it works wherever Power Query does in Excel: Excel 365, 2024, 2021, 2019, and 2016 for Windows, and Excel for Microsoft 365 on Mac.

When to Use the List.RemoveNulls Function

Use this function when you need to:

  • Get a clean list of values from a column that has blank cells
  • Get the unique values in a column without a blank (null) item in the list
  • Find the most common value in a column while ignoring the blanks
  • Get the last filled value in a column, like the latest reading in a log
  • Get the latest filled value across several columns in each row
  • Stack two or more uneven columns into one list with no gaps

Example 1: Remove Nulls From a List

Let’s start with the simplest use. Suppose you have the number of orders a shop received each day for six days, and two of the days have no number (null) because no data was recorded.

In Excel, go to Data > Get Data > From Other Sources > Blank Query. In the Power Query Editor that opens, type this formula in the formula bar:

= List.RemoveNulls({42,null,37,51,null,46})

Result: A list with 42, 37, 51, and 46

List.RemoveNulls removes the two nulls and returns 42, 37, 51 and 46

Here, the curly brackets create a list of six items, two of which are null. List.RemoveNulls drops the two nulls and returns the four numbers in the same order they were in.

In real work, the list usually comes from your data and not from numbers you type. That’s what Examples 2 to 7 do.

In the example file, Examples 1 and 8 type their values right into the query, so they don’t have a sheet. You can open them from Data > Queries & Connections.

Example 2: Get a Clean List From a Column With Blank Cells

Here’s the most common use of List.RemoveNulls. Below is a sign-up log in an Excel Table named ReferralSignups, and I want a list of only the referral codes that people actually used.

ReferralSignups table with the referral code of each sign-up and some blank codes

Some people signed up without a code, so those cells are blank. In Power Query, those blank cells come in as null.

Create a new blank query (as in Example 1), open the Advanced Editor (Home > Advanced Editor), and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="ReferralSignups"]}[Content],
Codes = List.RemoveNulls(Source[Referral Code])
in
Codes

Result: A list with FRIEND10, SPRING25, FRIEND10, and VIP50

List.RemoveNulls on the Referral Code column returns only the four codes that were used

Here, the Source line uses the Excel.CurrentWorkbook function to pull the ReferralSignups table into Power Query by its name. Source[Referral Code] then takes the Referral Code column as a list of eight values, four of them null.

List.RemoveNulls removes the four nulls, so you’re left with the four codes people used.

FRIEND10 shows up twice because two people used it. List.RemoveNulls removes only nulls, not duplicates.

If all you need is how many people used a code, you don’t need List.RemoveNulls at all. The List.NonNullCount function counts only the non-null values, so List.NonNullCount(Source[Referral Code]) gives 4, while the List.Count function counts the nulls too and gives 8.

Example 3: Get Unique Values Without the Blank

This next one comes up a lot when you want a list of categories, for example to use as a filter or a parameter. Below is a table named ServiceRequests, and I want the list of unique request categories, without any blank.

ServiceRequests table with the category of each request and a few blank categories

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="ServiceRequests"]}[Content],
Categories = List.Distinct(List.RemoveNulls(Source[Category]))
in
Categories

Result: A list with Plumbing, Electrical, and Carpentry

List.Distinct with List.RemoveNulls returns Plumbing, Electrical and Carpentry

Here, List.RemoveNulls first removes the three nulls from the Category column. Then the List.Distinct function keeps one copy of each category.

If you use the List.Distinct function on its own, you get four items, because it treats null as one more distinct value. So your category list would include a blank (null) item, which is rarely what you want.

Example 4: Find the Most Common Value While Ignoring Blanks

Now let’s look at a case where a null changes the answer. Below is a table named WorkshopSignups with the time slot each attendee prefers, and I want to find the most popular slot.

WorkshopSignups table with each attendee's preferred slot and four blanks

Four attendees didn’t pick a slot, so their cells are blank. Evening was picked three times, which is the most of any slot.

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="WorkshopSignups"]}[Content],
MostPopular = List.Mode(List.RemoveNulls(Source[Preferred Slot]))
in
MostPopular

Result: Evening

List.Mode with List.RemoveNulls returns Evening as the most popular slot

Here, List.RemoveNulls removes the four nulls from the Preferred Slot column, and the List.Mode function returns the value that appears most often in what’s left, which is Evening.

Important: Without List.RemoveNulls, List.Mode(Source[Preferred Slot]) returns null. The List.Mode function counts null like any other value, and there are four nulls but only three Evenings, so null wins.

Follow along with the example file

List.RemoveNulls Function Examples.xlsx

Download

Example 5: Get the Last Filled Value in a Column

Here’s another common one. Below is a weekly log in a table named VanMileage, with the odometer reading of a delivery van, and I want the latest reading.

VanMileage table with weekly odometer readings, a missed week and blank future weeks

The weeks are filled in ahead of time, so Weeks 6 to 8 are still blank, and the reading for Week 3 was missed.

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="VanMileage"]}[Content],
LatestReading = List.Last(List.RemoveNulls(Source[Odometer Reading]))
in
LatestReading

Result: 50530

List.Last with List.RemoveNulls returns the latest reading, 50530

Here, List.RemoveNulls removes the blank readings, so the list becomes 48210, 48795, 49960, and 50530. The List.Last function then returns the last item in that list, which is the Week 5 reading.

If you use the List.Last function on the column directly, you get null, because the last row (Week 8) is blank.

With the List.RemoveNulls version, a column where every reading is blank becomes an empty list once the nulls are removed. The List.Last function returns null for an empty list instead of an error, so you get null and not a broken query.

Example 6: Get the Latest Filled Value Across Columns in Each Row

Now let’s do the same thing across a row instead of down a column. Below is a table named PriceRevisions with each product’s price after each quarterly review, and I want the current price of each product in a new Latest Price column.

PriceRevisions table with each product's price after each quarterly review and blanks where the price did not change

A blank cell means the price wasn’t changed in that review. So the current price is the last filled value in each row.

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="PriceRevisions"]}[Content],
#"Added Latest Price" = Table.AddColumn(Source,"Latest Price",each List.Last(List.RemoveNulls({[Jan Price],[Apr Price],[Jul Price],[Oct Price]})),type number)
in
#"Added Latest Price"

Result: A new Latest Price column (26, 155, 89, 36, and 62)

A new Latest Price column with the last filled price in each row

Here, the Table.AddColumn function adds a Latest Price column and runs the formula after each once for every row. For each row, {[Jan Price],[Apr Price],[Jul Price],[Oct Price]} builds a list of that row’s four prices.

List.RemoveNulls removes the blank prices, and the List.Last function returns the last one that’s left. For the Desk Lamp, the list {24, null, 26, null} becomes {24, 26}, so its latest price is 26.

You could also write this with the ?? operator, as [Oct Price] ?? [Jul Price] ?? [Apr Price] ?? [Jan Price], which returns the first value that isn’t null. It gives the same result for this data.

With ??, though, you have to list the columns in reverse order. The List.RemoveNulls version keeps them in calendar order, and you add one more item to the list when a new review column comes in.

Example 7: Combine Uneven Columns Into One List

This next one is handy when data is spread across columns of different lengths. Below is a table named ShiftVolunteers with the volunteers for the morning and evening shifts, and I want one list of all the volunteers.

ShiftVolunteers table with morning and evening volunteers in columns of different lengths

The two shifts don’t have the same number of people, and a couple of volunteers dropped out, so both columns have blank cells.

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="ShiftVolunteers"]}[Content],
AllVolunteers = List.RemoveNulls(List.Combine({Source[Morning Shift],Source[Evening Shift]}))
in
AllVolunteers

Result: A list of seven names (Angela Turner, Hannah Lawrence, Marilyn Carter, Zoe Russell, Sophia Warren, Jesse Moore, and Frank Kennedy)

List.Combine with List.RemoveNulls returns one list of all seven volunteers

Here, the List.Combine function joins the Morning Shift column and the Evening Shift column into one list of ten items. Three of those items are null, from the blank cells.

List.RemoveNulls then removes those three nulls, so you get just the seven names, with the morning volunteers first and then the evening ones.

Example 8: Empty Text Is Not Removed

This last one is a catch you’re likely to run into. List.RemoveNulls removes only null, so an empty text value (“”) stays in the list.

Suppose you have a list of items where two are null and one is empty text (“”). Empty text shows up more often than you’d think, for example the Text.Split function gives “” (not null) when two delimiters sit next to each other.

Create a new blank query and use this formula:

= List.RemoveNulls({"Laptop",null,"","Monitor",null,"Keyboard"})

Result: A list with Laptop, an empty item, Monitor, and Keyboard

List.RemoveNulls removes the nulls but keeps the empty text item

Here, List.RemoveNulls removes the two nulls, but the empty text stays, so the list still has a blank-looking item. The same goes for 0 and FALSE, which are real values and not null.

To remove both the nulls and the empty text, use the List.RemoveItems function with a list of the values to remove:

= List.RemoveItems({"Laptop",null,"","Monitor",null,"Keyboard"},{null,""})

This returns a list with only Laptop, Monitor, and Keyboard.

Other Excel Articles You May Also Like:

All Power Query Functions

Sumit Bansal

Sumit Bansal

13x Microsoft Excel MVP

Hey! I'm Sumit Bansal, founder of trumpexcel.com and a Microsoft Excel MVP. I started this site in 2013 because I genuinely love Microsoft Excel (yes, really!) and wanted to share that passion through easy Excel tutorials, tips, and Excel training videos. My goal is straightforward: help you master Excel skills so you can work smarter, boost productivity, and maybe even enjoy spreadsheets along the way!

Get the FREE 51 Excel Tips Ebook

Enter your details and the free PDF is on its way to your inbox.

Hmm, that didn't go through. Please check your email and try again.

No spam. You'll also get my weekly Excel newsletter. Unsubscribe anytime.

Check your inbox!

The ebook is on its way to your email. It usually lands within a couple of minutes.