Date.IsInPreviousNWeeks

Updated on

Date.IsInPreviousNWeeks is a Power Query M function that indicates whether the given date(time) value occurs during the previous number of weeks, as determined by the system. The function returns a boolean value (true or false).

Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365

Syntax

Date.IsInPreviousNWeeks(
   dateTime as any,
   weeks as number,
) as nullable logical

Description

Date.IsInPreviousNWeeks checks if a given value (of type date, datetime, or datetimezone) falls within a specified number of weeks before the current system date and time, excluding the current week. It also includes a weeks argument to specify the number of weeks.

Examples

Let’s explore two straightforward examples to understand how the Date.IsInPreviousNWeeks function works.

Identifying Dates in the Previous N Weeks

Let’s say you have a calendar table and you want to add a column that shows true for dates within the last n weeks. You can do this by using the Date.IsInPreviousNWeeks function on your [Date] column, where n is the number of weeks you want to check.

Date.IsInPreviousNWeeks( [Date], n ) // Output depends on [Date] column and n

This expression returns true for any date in the [Date] column that falls within the previous n weeks. To test if your values fall within the last 4 weeks, you set n to 4:

Date.IsInPreviousNWeeks( [Date], 2 )

If today is July 6, 2024, this function will return true for all rows with dates between June 17, 2024, and June 30, 2024. Here’s a screenshot showing this:

Example of how to use the Date.IsInPreviousNWeeks function in Power Query M

You can see that dates within the previous 2 weeks are marked with true.

Using the Current Date and Time

If we retrieve the current date (generated by DateTime.FixedLocalNow) and shift it back 2 weeks, we can check if it falls within the last 4 weeks by using:

// Output: true
Date.IsInPreviousNWeeks( Date.AddWeeks( DateTime.FixedLocalNow(), -2 ), 4 )

Other functions related to Date.IsInPreviousNWeeks are:

Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/date-isinpreviousnweeks

2023-2024 © BI Gorilla. All rights are reserved. Information from Microsoft docs is property of Microsoft Corp. | Privacy Policy