Sub topic 8
Filling down and handling nulls
Report-style worksheets often show a value once and leave the cells below it empty. This chapter covers how Power Query represents those empty cells, how to fill them, and how to replace or remove the ones that remain. It assumes you can already open the Power Query Editor, read the Applied Steps pane, and set data types. Those topics are covered in earlier chapters.
What null means in Power Query
null is the M value for "no value". An empty cell in an Excel table loads as null. This matters because several operations treat null differently from text or numbers.
Three things look like blanks but are not the same:
| Source cell | Loaded as | Fill Down fills it? |
|---|---|---|
| Truly empty Excel cell | `null` | Yes |
| Empty string (`""`), common in CSV files and formulas such as `=""` | Empty text | No |
| Spaces only (`" "`) | Text with spaces | No |
The Editor shows null in italics in the cell. An empty string shows nothing at all. If Fill Down seems to do nothing, check which of these you have.
Filling down
Fill Down copies the nearest non-null value above into each null cell below it, within the selected column. This is the usual fix for grouped layouts like this one:
| Region | Product | Jan | Feb |
|---|---|---|---|
| North | Widget | 10 | 12 |
| null | Gadget | 7 | 9 |
| South | Widget | 4 | 5 |
| null | Gadget | 8 | 6 |
To fill it, select the Region column, then choose Transform > Fill > Down. You can also right-click the column header and choose Fill > Down. Power Query records a step like this:
let
Source = Excel.CurrentWorkbook(){[Name="SalesByMonth"]}[Content],
Filled = Table.FillDown(Source, {"Region"})
in
FilledEach row now carries its own region. The column list can name several columns, for example {"Region", "Manager"}.
Fill > Up does the reverse. It copies the value below into the nulls above, which suits layouts where a label sits at the bottom of a group, such as a subtotal row. The M function is Table.FillUp.
Order of steps matters
Fill Down reads the table as it stands at that step. Two ordering mistakes are common:
- Sorting before filling. Sorting moves rows, so a null can end up under a different group's value. Fill first, then sort.
- Filtering before filling. If you remove the rows that hold the group labels, the nulls have nothing to copy. Fill first, then filter.
Converting fake blanks to real nulls
Data from CSV files or formulas often has empty strings or whitespace. Convert them first so Fill Down can see them:
- Trim the column so whitespace-only cells become empty text. See the text cleanup chapter for
Text.TrimandText.Clean. - Replace the empty text with
null. In Transform > Replace Values, enter an empty "Value To Find" and typenullin "Replace With".
In M, the same two steps look like this:
let
Source = Excel.CurrentWorkbook(){[Name="SalesRaw"]}[Content],
Trimmed = Table.TransformColumns(Source, {{"Region", each Text.Trim(_), type text}}),
ToNull = Table.ReplaceValue(Trimmed, "", null, Replacer.ReplaceValue, {"Region"}),
Filled = Table.FillDown(ToNull, {"Region"})
in
FilledText.Trim leaves a null as null. It only changes text values, so the step is safe on a column that mixes both.
Replacing nulls with a value
Use Transform > Replace Values when a missing number should count as a known default. Select the column, open Replace Values, type null as the value to find, and enter the replacement:
let
Source = Excel.CurrentWorkbook(){[Name="SalesByMonth"]}[Content],
NoNulls = Table.ReplaceValue(Source, null, 0, Replacer.ReplaceValue, {"Jan", "Feb"})
in
NoNullsRemoving rows with nulls
To drop rows where a key column is empty, filter the column and clear the (null) checkbox in the filter menu. Alternatively, write the filter in M:
let
Source = Excel.CurrentWorkbook(){[Name="SalesByMonth"]}[Content],
Filled = Table.FillDown(Source, {"Region"}),
HasProduct = Table.SelectRows(Filled, each [Product] <> null)
in
HasProductThis is also the standard way to remove blank spacer rows or total rows that carry no product name. Home > Remove Rows > Remove Blank Rows is a shortcut, but it removes only rows where every column is null or empty. A row with one stray value survives it.
How nulls behave in calculations
Null spreads through arithmetic and cannot be compared like other values:
null + 5returnsnull, not5. A custom column that adds Jan and Feb will return null for any row where either month is null.null = nullistruein M, so filtering with= nullworks.[Amount] > 100on a null returnsnull, andTable.SelectRowsdrops those rows. Rows with null Amount disappear from a "greater than" filter and from a "less than or equal" filter alike.List.SumandList.Averageignore nulls. This is why Group By totals may differ from a row-by-row sum that included nulls.
To add safely, use the null-coalescing operator:
let
Source = Excel.CurrentWorkbook(){[Name="SalesByMonth"]}[Content],
Total = Table.AddColumn(Source, "H1Total", each ([Jan] ?? 0) + ([Feb] ?? 0), type number)
in
Totala ?? b returns a unless it is null, in which case it returns b.
Common mistakes
- Fill Down on a column with spaces. Nothing changes because the cells are not null. Trim and replace first.
- Filling the wrong column. Fill Down on Product copies product names into rows that belong to a different product. Select only the label columns.
- Replacing nulls before unpivoting. Cross-tab nulls often mean "no data". Decide whether those rows should exist before they become rows.
- Forgetting types. After replacing null with
0in a column of type any, set the type again so the column is numeric.
Exercise
A CSV import has a Region column where only the first row of each group has a value. You apply Fill Down, but the blank cells stay blank. What is the most likely cause, and what two steps fix it?
Show answer
The blank cells are empty strings or whitespace, not null, so Fill Down ignores them. Trim the column with Text.Trim (Transform > Format > Trim), then use Replace Values to replace the empty text with null. After that, apply Fill Down to the Region column.
Exercise
Write an M expression for a custom column named Total that adds Jan and Feb and treats a null month as 0.
Show answer
Table.AddColumn(Source, "Total", each ([Jan] ?? 0) + ([Feb] ?? 0), type number)