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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Anders_G
Helper I
Helper I

Help with Date_request in Date.IsInPreviousNMonths

Hi,

 

I'm trying to manage my data imports with date filters. I want to import X months in the past from the previous year. I've read you can change the date_request in the Date.IsInPreviousNMonths([date_request], 2) function.

 

I've tried creating a custom date query from a blank query and giving it today's date one year in the past. I've named the query Datefilter.

Anders_G_0-1668414941774.png

 

My custom function then looks like this.

= Table.SelectRows(Dataset_(LY)_View, each Date.IsInPreviousNMonths(Datefilter, 2))

 

However, it doesn't return any data. What am I doing wrong?

1 ACCEPTED SOLUTION

Hi , @Anders_G 

Accoding to your description, you want to filter the previous N months od the last year. Right?

I mean this , you can put this in "Advanced Editor" in Power Query Editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tc3BDQAhCETRXjib4ODqQi3G/ttYJJrl+vIzMydJFWEwqBBolQsu3UnkGsLGtpbQGLoNTwrrCZEsHsYPnc3h1SOw2HfS5lPrAw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"value", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", (x)=>if Duration.Days(Date.AddDays(Date.From(Date.From(DateTime.LocalNow())),-365)-x[Date]) >=0 and  Duration.Days(Date.AddDays(Date.From(Date.From(DateTime.LocalNow())),-365)-x[Date]) <=60  then 1 else -1          )
in
    #"Added Custom"

The table is like this:

vyueyunzhmsft_0-1668562484558.png

Then we can filter the [Custom]=1 then we will meet your need .

 

Best Regards,

Aniya Zhang

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
Rickmaurinus
Helper V
Helper V

Hi there,

 

The Date.IsInPreviousNMonths function tests whether a date value is within the specified number of months, compared from your current system time. 

 

For your filter to work,  you should change around the 'DateFilter' variable, to a date column that's actually within your query. For instance, if you have a [Sales Date] column you could use: 

Table.SelectRows(Dataset_(LY)_View, each Date.IsInPreviousNMonths([Sales Date], 2))

 

More details here: https://powerquery.how/date-isinpreviousnmonths

 

However, if you want to filter on dates starting from last year, you would need something like: 

 

Table.SelectRows( 
  Dataset_(LY)_View, 
  each [Sales Data] < Date.AddYears( Date.From( DateTime.LocalNow() ), -1 )
    and [Sales Data] >= Date.AddMonths( Date.From( DateTime.LocalNow() ), -14 )
)

  

In that way you filter your data down to the relevant dates. 

 

--------------------------------------------------

@ me in replies or I'll lose your thread

 

Master Power Query M? -> https://powerquery.how

Read in-depth articles? -> BI Gorilla

Youtube Channel: BI Gorilla

 

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

v-yueyunzh-msft
Community Support
Community Support

Hi , @Anders_G 

The "Date.IsInPreviousNMonths" function is automatically compared with the current time of the system, and the comparison date cannot be customized.

For more information, you can refer to:
Date.IsInPreviousNMonths - PowerQuery M | Microsoft Learn

 

If you want to  import X months in the past from the previous year .  I think you need to calculate the days between two days and then compare the days to filter your table.

 

Best Regards,

Aniya Zhang

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

 

Thank you for the clarification.

 

If you want to  import X months in the past from the previous year .  I think you need to calculate the days between two days and then compare the days to filter your table.

I would be very grateful if you could explain the process in more detail. I have no real knowledge of M or SQL. I mostly copy and paste what I can find on this forum. 

Hi , @Anders_G 

Accoding to your description, you want to filter the previous N months od the last year. Right?

I mean this , you can put this in "Advanced Editor" in Power Query Editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tc3BDQAhCETRXjib4ODqQi3G/ttYJJrl+vIzMydJFWEwqBBolQsu3UnkGsLGtpbQGLoNTwrrCZEsHsYPnc3h1SOw2HfS5lPrAw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"value", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", (x)=>if Duration.Days(Date.AddDays(Date.From(Date.From(DateTime.LocalNow())),-365)-x[Date]) >=0 and  Duration.Days(Date.AddDays(Date.From(Date.From(DateTime.LocalNow())),-365)-x[Date]) <=60  then 1 else -1          )
in
    #"Added Custom"

The table is like this:

vyueyunzhmsft_0-1668562484558.png

Then we can filter the [Custom]=1 then we will meet your need .

 

Best Regards,

Aniya Zhang

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

 

 

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

AugPowerBI_Carousel

Power BI Monthly Update - August 2024

Check out the August 2024 Power BI update to learn about new features.

August Carousel

Fabric Community Update - August 2024

Find out what's new and trending in the Fabric Community.