Quantcast
Channel: Desktop topics
Viewing all articles
Browse latest Browse all 213819

Power Query M filter data by Dynamic Date (PQ not DAX) Start of Month

$
0
0

More as a reference for us than a question which hopefully you will find useful :-)

 

I was just working on filtering data in Power Query based on the date. I kow you can manually filter your source data in the Power Query Editor by using the extensive date filter options in the drop down filter menu BUT I needed something better which was to remove data from the source based on a dynamic date point. Basically I needed to filter out any data prior to the start of the current month. Eg today is 4th August and i needed to remove everything rior to the 1st of August. Going forward into September the Query would remove everytning from the 1st of september etc.....

 

Answer 

Using the manual way for filtering anything before 1st august the code in the advanced editor was 

code was : Table.SelectRows(#"Changed Type", each [Date] > #date(2016, 8, 1))

 

To do it dynamically i replaced the fixed date with the following code:

Date.From(Date.StartOfMonth(DateTime.LocalNow() ))

 

Such that the complete new code is:

Table.SelectRows(#"Changed Type", each [Date] > Date.From(Date.StartOfMonth(DateTime.LocalNow() )))

 

In English that is "Date.From" returns a date datatype.

"Date.StartofMonth()" returns the StartofMOnth of the date

DateTime.LocalNow() returns the local system date. 

 

Do be careful using this on production as I am sure my local date where i am developing in Power BI desktop will be different to when i upload this to the Power BI service. So when refreshin g in the cloud it could change my cut off point for month end reporting. I believe there is a function to do with TImeZones that may resolve this if anyone want s to contribute please do....

 

Hope it saves you some time.

 


Viewing all articles
Browse latest Browse all 213819

Trending Articles