Date.IsInPreviousNDays is a Power Query M function that indicates whether the given date(time) value occurs during the previous number of days, 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.IsInPreviousNDays(
dateTime as any,
days as number,
) as nullable logical
Description
Date.IsInPreviousNDays determines if a given value (of type date, datetime, or datetimezone) is within a specified number of days before the current system date and time. It excludes current day values.
Examples
Let’s explore two straightforward examples to understand how the Date.IsInPreviousNDays function works.
Identifying Dates in the Previous N Days
If you have a calendar table and you want to add a column that indicates true
for dates that fall within the previous n days. You can do this using the Date.IsInPreviousNDays function on your [Date]
column. Here, n is the number of days you want to check.
Date.IsInPreviousNDays( [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 days. Let’s say you set n to 4. You can do that using:
Date.IsInPreviousNDays( [Date], 4 )
If today is July 6, 2024, this function will return true
for all rows with dates between July 2, 2024, and July 5, 2024. Here’s a screenshot showing this:
You can see that dates within the previous 4 days are marked with true
.
Using the Current Date and Time
To check if a date and time from the today minus 2 falls within the last 4 days falls , you can use the following expression:
// Output: true
Date.IsInPreviousNDays( Date.AddDays( DateTime.FixedLocalNow(), -2 ), 4 )
Related functions
Other functions related to Date.IsInPreviousNDays are:
2023-2024 © BI Gorilla. All rights are reserved. Information from Microsoft docs is property of Microsoft Corp. | Privacy Policy