Forum Discussion
Relative Date Parameter in Power Query
The trick is to create a text parameter then convert it to a date query and then reference the query in your other queries.
Here are the detailed steps:
You have this same filter in multiple queries and you'd like to parameterize it.
#"Filtered Rows" = Table.SelectRows(#"Renamed Columns", each [Date] > Date.AddDays ( DateTime.LocalNow(), -90 ) )
But if we create a parameter for the relative date like this:
and use that parameter in the query like this:
#"Filtered Rows" = Table.SelectRows(#"Renamed Columns", each [Date] > StartDate )
it generates the following error:
Expression.Error: We cannot apply operator < to types Text and DateTime. Details: Operator=< Left=Date.AddDays ( DateTime.LocalNow(), -90 ) Right=1/1/1984 12:00:00 AM
This can be solved by converting the parameter to a query. Right-click the parameter and select "Convert to Query"
There is one more thing we need to do. Just after the conversion the query is a text type and looks like this
So we need to make sure it evaluates as a date (or datetime in this case). All we need to do is add an "=" to the step editor, or remove the quotes in the advanced editor.
Now the query results in a datetime value and evaluates correctly when referenced in other queries.
#"Filtered Jan 1 2018 - 6 weeks future" = Table.SelectRows(#"Renamed Columns", each [Date] > StartDate )