Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Promethes_2
Helper II
Helper II

Power Query Current Date Filter

Hello,

 

I'm learning Power Query and I'm having an issue trying to use a simple filter.  I have a current table with a column called ValidTo.  In TSQL I could filter this column to only return the records that were >= GETDATE() (greater than or equal to what the date is at run time). I am trying to accomplish the same result in Power Query but I am not able to find which function to use.  I was revewing https://msdn.microsoft.com/en-us/library/mt296608.aspx but was unable to determine which function returns the current datetime value when it's refreshed.  I've tried TODAY(), GETDATE(), NOW() all with no luck (ignore the before or equal to).  This seems like a simple thing to do so I must be missing where the function is. 

 

Any help?  Thank you.

 

Filter.png

1 ACCEPTED SOLUTION
KHorseman
Community Champion
Community Champion

Power Query does not use DAX formulas. It has its own language. TODAY() is not a valid formula in that language.

 

Also that dialog box will only allow literal date values, not formulas that evaluate to a date. But you can get past that. Type some date into that box and hit OK. It doesn't matter what date you enter because you're just using it to get out of the dialog box with a valid formula, after which you're going to replace it. After you hit OK look up at the formula bar for the step you just inserted. You should see something like:

 

= Table.SelectRows(#"Name of Previous Step", each [VALIDTO] <= #datetime(2016, 9, 12, 0, 0, 0))

 

Replace the end of that formula with DateTime.LocalNow() which is the Power Query nearest equivalent to TODAY()*.

 

= Table.SelectRows(#"Name of Previous Step", each [pafo_EstimatedEndDate] <= DateTime.LocalNow())

 

...or you could just go to the advanced editor and type in that step by hand if you know how to hand-code a new step.

 

 

*Technically it returns a Date/Time value, where TODAY() returns a Date value. The literal closest to TODAY() is Date.From(DateTime.LocalNow()) which you should use rather than DateTime.LocalNow() alone if the column you're filtering is already a plain date type rather than date/time.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

15 REPLIES 15
drmarinko
New Member

I was trying to do something similar and the logic or syntax may apply.  I was trying to limit my TCdate (timecard date) to any date prior to today.  I used this in Power Query and it worked:  = Table.SelectRows(#"Filtered Rows", each [TCdate] < List.Max(#"Filtered Rows"[TCdate]))

drmarinko
New Member

I was trying to do something similar and the logic or syntax may apply.  I was trying to limit my TCdate (timecard date) to any date prior to today.  I used this in Power Query and it worked:  = Table.SelectRows(#"Filtered Rows", each [TCdate] < List.Max(#"Filtered Rows"[TCdate]))

Anonymous
Not applicable

This one really worked for me, give a try 

Table.AddColumn(#"Changed column type", "IsBeforeToday", each if [TheDate] <= #date(Date.Year(DateTime.LocalNow()), Date.Month(DateTime.LocalNow()), Date.Day(DateTime.LocalNow())) then 1 else 0)
bisah
New Member

What if I need the dau before my current date? (yesterday)

 

I tried using Date.From(DateTime.LocalNow()) -1 but I get the following error: 

 

Expression.Error: Não conseguimos aplicar o operador - aos tipos Date e Number.
Detalhes:
Operator=-
Left=29/01/2020
Right=1

 

Thanks!

KHorseman
Community Champion
Community Champion

@bisah  Date.From(Date.AddDays(DateTime.LocalNow(), -1)) ought to do it.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




@KHorseman, what if I want to have the last Sunday as the cut-off date?

KHorseman
Community Champion
Community Champion

Power Query does not use DAX formulas. It has its own language. TODAY() is not a valid formula in that language.

 

Also that dialog box will only allow literal date values, not formulas that evaluate to a date. But you can get past that. Type some date into that box and hit OK. It doesn't matter what date you enter because you're just using it to get out of the dialog box with a valid formula, after which you're going to replace it. After you hit OK look up at the formula bar for the step you just inserted. You should see something like:

 

= Table.SelectRows(#"Name of Previous Step", each [VALIDTO] <= #datetime(2016, 9, 12, 0, 0, 0))

 

Replace the end of that formula with DateTime.LocalNow() which is the Power Query nearest equivalent to TODAY()*.

 

= Table.SelectRows(#"Name of Previous Step", each [pafo_EstimatedEndDate] <= DateTime.LocalNow())

 

...or you could just go to the advanced editor and type in that step by hand if you know how to hand-code a new step.

 

 

*Technically it returns a Date/Time value, where TODAY() returns a Date value. The literal closest to TODAY() is Date.From(DateTime.LocalNow()) which you should use rather than DateTime.LocalNow() alone if the column you're filtering is already a plain date type rather than date/time.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




I have been trying to figure this out for a while now. Thank you for making it so easy to figure out and understand!

That's great, thank you this worked for me. It does seem strange to me that we have IsInCurrentDay but not IsBeforeCurrentDay, I suppose they can't think of everything 🙂

Hi,  This solution does not seem to work for me as I get the following error:

KervBruce_0-1662058115788.png

 

No, ignore me I'm being dim.  If course you cant compare Date with DateTime.  I changed the Date field to DateTime and presto!

Anonymous
Not applicable

hi i am looking get data between 91-180, 181-270, 271-365 days from the current date. could you please help in doing so. looking for something similar to "in the previous...days" function would really appreciate the help

This is exactly what I was looking for!  I understood it uses M language but didn't realize you couldn't type the formula into the dialog box (TODAY() was just an example, I had tried DateTime.LocalNow() but that didn't work either). I am going to request this functionality to be added as it would seem more intuitive and user friendly to be able to build it on the fly rather than the solution you've provided.

 

Thank you very much for this helpful information!

@Promethes_2

 

You can add this idea on ideas site. And if KHorseman’s solution is exactly what you are looking for, you can mark his answer as solution to close this thread.

 

Best Regards,

Herbert

Done and Done!  Thank you again!

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.