Sub topic 7
Handling nulls: replace, fill down, and remove empty rows
This chapter assumes you can open the Power Query Editor, read the Applied Steps pane, and set column data types. It also assumes you have used the column profiling tools from earlier chapters to spot problem columns. Here you will learn what a null is, why it shows up, and how to deal with it using three tools: Replace Values, Fill Down, and Remove Empty rows.
What a null is
In Power Query, null means "no value". It is not the same as zero, and it is not the same as an empty string ("") or a string of spaces. These four values look similar in a report but behave differently:
| Value | M literal | What it means | Counted by Column quality as |
|---|---|---|---|
| Null | `null` | No value at all | Empty |
| Empty text | `""` | Text with zero characters | Valid |
| Spaces | `" "` | Text with one or more space characters | Valid |
| Zero | `0` | A real number | Valid |
This difference causes most null-handling bugs. If you run Replace Values on null and the column actually holds empty strings, nothing changes. The Column quality bar will still show 0% empty, and the problem will look like it was never there.
Where nulls come from
- Blank cells in Excel or empty fields in a CSV. CSV files often load empty fields as
null, but some sources load them as empty text. - Merged cells in Excel. Only the top-left cell holds the value. The other cells load as
null. - Grouped report layouts, where a label appears once and the rows beneath it are blank.
- Merge queries that find no match, and Expand operations on missing records.
- Conditional logic that returns
nullwhen no branch matches.
Check before you change anything
Turn on Column quality and Column distribution under the View tab. The Column quality bar shows the percentage of Valid, Error, and Empty values. By default, profiling uses only the first 1,000 rows. To check the whole table, use the status bar at the bottom of the editor and switch from "Column profiling based on top 1000 rows" to "Column profiling based on entire data set".
A column that looks clean in the top 1,000 rows can hold nulls further down. Profile the whole data set before you decide a column has no missing values.
Decide what a null means
Pick the fix based on why the value is missing. Three situations come up most often:
- The value is missing and unknowable. Replace it with a label such as
"Unknown"for text. For numbers, leave it asnullor replace it with a deliberate default. - The value is implied by the row above. This is a grouped layout. Use Fill Down.
- The whole row is empty. Remove it.
Be careful when you replace numeric nulls with 0. Totals stay the same, but averages change. A null is ignored by an average, while a 0 pulls it down. Replace with 0 only when 0 is the true meaning, for example "no sales that day".
Replace nulls
Select the column, then choose Transform > Replace Values. Type null in the "Value To Find" box and your replacement in "Replace With". The editor generates this step:
let
Source = #table(
{"Customer", "Region"},
{{"Alice Smith", "West"}, {"Bob Jones", null}}
),
FilledRegion = Table.ReplaceValue(Source, null, "Unknown", Replacer.ReplaceValue, {"Region"})
in
FilledRegionReplacer.ReplaceValue replaces a value only when the whole cell matches. The other option, Replacer.ReplaceText, replaces matching text inside larger strings. It works only on text columns, so it is the wrong choice for null.
The replacement value must fit the column type. If the column is typed as a number and you replace null with "Unknown", the step produces text in a number column and later steps may raise errors. Replace nulls in a number column with a number, or change the type first.
Replace blank text too
If the profile shows blanks that are not counted as Empty, the cells probably hold "" or spaces. Clean the text first (the earlier text-cleaning chapter covers Text.Trim), then convert empty text to null so one rule handles both:
let
Source = #table(
{"Customer", "Region"},
{{"Alice Smith", "West"}, {"Bob Jones", ""}, {"Cara Diaz", " "}}
),
Trimmed = Table.TransformColumns(Source, {{"Region", each Text.Trim(_), type text}}),
EmptyToNull = Table.ReplaceValue(Trimmed, "", null, Replacer.ReplaceValue, {"Region"}),
FilledRegion = Table.ReplaceValue(EmptyToNull, null, "Unknown", Replacer.ReplaceValue, {"Region"})
in
FilledRegionAfter the trim step, both blank values are "", the second step turns them into null, and the third step labels them "Unknown". The editor lets you type an empty Value To Find box for the second step.
Fill Down and Fill Up
Fill Down copies the last non-null value into the null cells beneath it. Use it for grouped layouts like this one:
| Region | Store | Sales |
|---|---|---|
| West | Seattle | 500 |
| null | Portland | 300 |
| East | Boston | 450 |
| null | New York | 700 |
Select the Region column and choose Transform > Fill > Down. Portland becomes West and New York becomes East.
let
Source = #table(
{"Region", "Store", "Sales"},
{
{"West", "Seattle", 500},
{null, "Portland", 300},
{"East", "Boston", 450},
{null, "New York", 700}
}
),
FilledDown = Table.FillDown(Source, {"Region"})
in
FilledDownFill Up works the other way. It copies a value from below into the null cells above it. Use it when a label sits at the bottom of each group, as in some report footers. The M function is Table.FillUp.
Fill Down gotchas
- Sort order matters. Fill Down uses the current row order. If you sort first, values may fill into the wrong rows. Fill before you sort or filter.
- Only true nulls are filled. Cells with
""or spaces are skipped, so the column looks unchanged. Convert blanks to null first, as shown above. - Leading nulls stay null. If the first row is null, there is nothing above it to copy.
- Fill before filtering. If you filter out the rows that hold the group labels, Fill Down has nothing to copy.
Remove empty rows
Spreadsheets often include blank separator rows or trailing empty rows. Use Home > Remove Rows > Remove Blank Rows. The editor writes this step:
let
Source = #table(
{"OrderID", "Product", "Amount"},
{{1, "Widget", 10}, {null, null, null}, {2, "Gadget", 20}}
),
RemovedBlankRows = Table.SelectRows(
Source,
each not List.IsEmpty(List.RemoveMatchingItems(Record.FieldValues(_), {"", null}))
)
in
RemovedBlankRowsThis removes a row only when every column is null or "". A row with a value in even one column stays.
Remove rows by one column
Often you care about one key column. A row with an amount but no OrderID, for example, may be invalid. To remove rows where a specific column is null, open the column's filter menu and clear the (null) check box, or write the filter directly:
let
Source = #table(
{"OrderID", "Amount"},
{{1, 10}, {null, 15}, {3, 20}}
),
KeptRows = Table.SelectRows(Source, each [OrderID] <> null)
in
KeptRowsThis differs from Remove Blank Rows. It drops the second row even though Amount has a value. Use it only when a missing key makes the row unusable.
Order of steps
Nulls interact with other cleaning steps, so the order affects the result:
- Promote headers and set the data types.
- Trim text and convert blank text to null.
- Fill Down for grouped layouts.
- Remove blank rows.
- Replace remaining nulls with defaults.
If you replace nulls before you fill down, the placeholder text is no longer null, and Fill Down skips it.
Common errors
- Replace Values changes nothing. The cells hold
""or spaces, notnull. Check the type of blank with a filter on the column. - Fill Down changes nothing. The same cause. Convert blanks to null first.
- Wrong values after Fill Down. The rows were sorted or filtered first.
- New errors after replacing a null. The replacement type does not match the column type.
- Averages shifted after replacing with 0. Nulls were ignored before and are counted now.
- Rows missing after a null filter.
each [Col] <> nullremoves every row where that column is null, including rows with useful data in other columns.
Exercise
A Region column shows 0% Empty in Column quality, yet the report shows blank regions. Fill Down on the column changes nothing. What is the likely cause, and what step fixes it?
Show answer
The blank cells hold empty text ("") or spaces, not null, so Column quality counts them as Valid and Fill Down skips them. Trim the column with Text.Trim, then use Replace Values to change "" to null. After that, Fill Down fills the cells from the row above.
Exercise
A sales table has a Discount column of numbers with some nulls. A teammate replaces the nulls with 0. The average discount drops. Why, and when is the replacement still correct?
Show answer
Average ignores nulls but includes zeros, so each new 0 lowers the average. The replacement is correct only when a missing discount truly means "no discount". If the value is unknown, keep the null.