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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
mike9999
Advocate I
Advocate I

Query Folding incorrect for Date.IsInPreviousNWeeks

Hi-

I think that that query folding is incorrect for Date.IsInPreviousNWeeks. Any suggestions on how to fix the m-code on my end, or can anyone confirm this is a bug and how to request this gets fixed?

 

ex. m-code

= Table.SelectRows(model_MrpReport, each Date.IsInPreviousNWeeks([Source.Date], 52))

 

produces a native sql query with

where [_].[Source.Date] >= convert(date, '2023-04-09') and [_].[Source.Date] < convert(date, '2024-04-07')

 

whereas I think it should be <= rather than < on the last convert(date, '2024-04-07')

 

Mike

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @mike9999 

I think this is correct.

The WHERE clause

 

where [_].[Source.Date] >= convert(date, '2023-04-09') and [_].[Source.Date] < convert(date, '2024-04-07')

 

is equivalent to

 

where [_].[Source.Date] >= convert(date, '2023-04-09') and [_].[Source.Date] <= convert(date, '2024-04-06')

 

since [Source.Date] is of type date.

 

Since 2023-04-09 is a Sunday and 2024-04-06 is a Saturday, this is a range of complete Sun-Sat weeks by the looks of it.

 

Does that look right or were you expecting something different?

 

Regards

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

View solution in original post

3 REPLIES 3
ToddChitt
Super User
Super User

The CURRENT week started on 4/7, so PREVIOUS should be anything BEFORE that date, but not including that date. My regional settings say that the week starts on Sunday, as in 4/7. Are your Regional setting correct, and do they match your assumptions?




Did I answer your question? If so, mark my post as a solution. Also consider helping someone else in the forums!

Proud to be a Super User!





OwenAuger
Super User
Super User

Hi @mike9999 

I think this is correct.

The WHERE clause

 

where [_].[Source.Date] >= convert(date, '2023-04-09') and [_].[Source.Date] < convert(date, '2024-04-07')

 

is equivalent to

 

where [_].[Source.Date] >= convert(date, '2023-04-09') and [_].[Source.Date] <= convert(date, '2024-04-06')

 

since [Source.Date] is of type date.

 

Since 2023-04-09 is a Sunday and 2024-04-06 is a Saturday, this is a range of complete Sun-Sat weeks by the looks of it.

 

Does that look right or were you expecting something different?

 

Regards

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

Ah I understand now. I see on the Microsoft docs "Note that this function will return false when passed a value that occurs within the current week."

 

Intuitively- I would have assumed that Date.IsInPreviousNWeeks would return true for the current week- but really they mean the previous N FULL weeks excluding the week to date. I can solve my problem using another module!

 

Appreciate the quick response.

Mike

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.