powerquery

How can I filter rows to exclude non-date values in a date column?


I'm dealing with a large Excel table supposed to contain dates in a particular column.

However, some of the table rows do not contain date values in that particular column. Instead, some free text can be found there.

In Power Query, I'd like to filter the result set to only contain rows having a valid date value in that column.

How would I do that?


Solution

  • You can try to convert to a date and check for errors.

    If your preceding step has the column set as type any or type text:

    Table.SelectRows(#"Preceding Step", each not (try Date.FromText([Dates]))[HasError])
    

    If your preceding step is of type date, then the non-dates will already show an error, so you would use:

    Table.RemoveRowsWithErrors(#"Preceding Step", {"Dates"})
    

    You need to enter the code in the Advanced Editor, so the full M-Code would look like:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ30jcyMDJRitUBcQwNkHgG+oYInhFQHUipoQWYm5iUDKZNUdQAZQ2NDI0h2k3N9E0s9I3MLSyVYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Dates = _t]),
        #"Removed Non-Dates"= Table.SelectRows(Source, each not (try Date.FromText([Dates]))[HasError]),
        #"Changed Type" = Table.TransformColumnTypes(#"Removed Non-Dates",{{"Dates", type date}})
    in
        #"Changed Type"
    

    enter image description here

    transforms to:

    enter image description here