cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anonymous
Not applicable

M Query - Last Year This Month, Last Year Last Month

Hi - I'm trying to come up with a way to only bring in certain data based on a date column in the Query Editor.

 

I have a date column that has the first day of each month for last year (January 1st, 2018; February 1st, 2018, etc)

I'm trying to filter my query so that I only get two months worth of 'last year' data returned. I want "Last Year This Month" and "Last Year Last Month" data.

 

Example: Today is August 28, 2019. I want to bring back August 2018 and July 2018 data in my query editor layer.

 

Any help would be greatly appreciated!

 

1 ACCEPTED SOLUTION
dax
Community Support
Community Support

Hi  CT767,

You might miss comma at the end of above row, I test my code in my enviornment, it will work well(no error, but result might not be what you want)

let
    
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc5RCgAhCEXRvfgdpDY6tZZo/9tInCBlfg9yn3MCI/VKlaAAIaxy5HXhC+ogF7qDRuB8YQ0xeWJE4sk4w3xBa8vwPUJpRvKOZThnxSuaTmJlWKX9l9GkwVob", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [date = _t, v1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"date", type date}, {"v1", Int64.Type}}),
    
    #"FILTER"=Table.SelectRows(#"Changed Type",each ([date] >=#date(Date.Year(DateTime.LocalNow()) -1 , Date.Month(DateTime.LocalNow())-1, 1)) 
    or ([date] <#date(Date.Year(DateTime.LocalNow()) -1 , Date.Month(DateTime.LocalNow())+1, 1)))
  in  
#"FILTER"

Best Regards,
Zoe Zhi

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

4 REPLIES 4
dax
Community Support
Community Support

Hi CT767,

You could try below query to see whether it works or not

let
    
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc5RCgAhCEXRvfgdpDY6tZZo/9tInCBlfg9yn3MCI/VKlaAAIaxy5HXhC+ogF7qDRuB8YQ0xeWJE4sk4w3xBa8vwPUJpRvKOZThnxSuaTmJlWKX9l9GkwVob", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [date = _t, v1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"date", type date}, {"v1", Int64.Type}}),
    
    #"FILTER"=Table.SelectRows(#"Changed Type",each ([date] >=#date(Date.Year(DateTime.LocalNow()) -1 , Date.Month(DateTime.LocalNow())-1, 1)) 
    and  ([date] <#date(Date.Year(DateTime.LocalNow()) -1 , Date.Month(DateTime.LocalNow())+1, 1)))
  in  
#"FILTER"

Best Regards,
Zoe Zhi

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Hi @dax ,

 

I tried the below code, what you posted but changed the "AND" to "OR".

 

I'm getting an error "Expression.SyntaxError: Token Comma expected", I'm unable to find what comma i'm missing.



= Table.SelectRows(#"Removed Columns1", each ([Snapshot Date] >= #date(Date.Year(DateTime.LocalNow())-1, Date.Month(DateTime.LocalNow())-1,1)) OR ([Snapshot Date] <#date(Date.Year(DateTime.LocalNow())-1, Date.Month(DateTime.LocalNow())+1,1)))

 

dax
Community Support
Community Support

Hi  CT767,

You might miss comma at the end of above row, I test my code in my enviornment, it will work well(no error, but result might not be what you want)

let
    
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc5RCgAhCEXRvfgdpDY6tZZo/9tInCBlfg9yn3MCI/VKlaAAIaxy5HXhC+ogF7qDRuB8YQ0xeWJE4sk4w3xBa8vwPUJpRvKOZThnxSuaTmJlWKX9l9GkwVob", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [date = _t, v1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"date", type date}, {"v1", Int64.Type}}),
    
    #"FILTER"=Table.SelectRows(#"Changed Type",each ([date] >=#date(Date.Year(DateTime.LocalNow()) -1 , Date.Month(DateTime.LocalNow())-1, 1)) 
    or ([date] <#date(Date.Year(DateTime.LocalNow()) -1 , Date.Month(DateTime.LocalNow())+1, 1)))
  in  
#"FILTER"

Best Regards,
Zoe Zhi

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

MFelix
Super User III
Super User III

Hi @Anonymous ,

 

To what I can understand you want to get the data base on an dinamic filter based on current date make a filter on the query: and select any date you will get something similar to this:

= Table.SelectRows(#"Changed Type", each ([Date] = #date(2018, 8, 1) or [Date] = #date(2018, 12, 1)))

Then just change the date part of the two dates for the following formula:

 

= Table.SelectRows(#"Changed Type", each ([Date] = #date(Date.Year(DateTime.LocalNow()) -1 , Date.Month(DateTime.LocalNow()), 1) or [Date] = #date(Date.Year(DateTime.LocalNow()) -1, 12, 1)))

This should give expected result.

 

Regards,

MFelix


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



Helpful resources

Announcements
MBAS on Demand

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

July 2021 Update 768x460.png

Check it out!

Click here to read more about the July 2021 Updates

Power Query PA Forum 768x460.png

Check it out!

Did you know that you can visit the Power Query Forum in Power BI and now Power Apps

Top Solution Authors
Top Kudoed Authors