Power Query M snippets
Empty columns and errors
Remove rows where a specific column is empty:
UI option - Filter > null or ““, that generates the following M code:
= Table.SelectRows(Source, each [Column1] <> null and [Column1] <> "")
Remove rows where all columns are empty:
UI option in Power Query Remove Rows > Remove Blank Rows, that generates the following M code:
= Table.SelectRows(Source, each not List.IsEmpty(List.RemoveMatchingItems(Record.FieldValues(_), {"", null})))
Remove rows where all columns are empty, but ignore Column1 and Column2:
= Table.SelectRows(#"Table1", each not List.IsEmpty(List.RemoveMatchingItems(
Record.FieldValues(Record.RemoveFields(_,{"Column1", "Column2"})), {"", null}
)))
Remove errors in all columns, ignoring column names:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
// Generate a list of all the column headings
AllColHeadings = Table.ColumnNames(Source),
// Use List.Transform to create a list of the the column headings with a value of 0 against each one
ColsAndReplacementVals = List.Transform( AllColHeadings, each {_, 0}),
// Then use Table.ReplaceErrorValues using the original table (Source) and and the new list of columns and replacement values
ReplaceErrors = Table.ReplaceErrorValues(Source, ColsAndReplacementVals)
in
ReplaceErrors
Or in one line:
= Table.ReplaceErrorValues(Source, List.Transform( Table.ColumnNames(Source), each {_, 0}))
Column management
Rename a column based on its position (here replace column at first position {0}):
= Table.RenameColumns(#"PreviousStep",{{Table.ColumnNames(#"En-têtes promus"){0}, "NewColumnName"}})