Power Query DateTime.IsInNextNHours Function Guide
In this article
The DateTime.IsInNextNHours function in Power Query is used to determine if a specific date and time value falls within a specified number of hours starting from the current moment. It returns a logical TRUE or FALSE value, making it an essential tool for creating dynamic alerts, filtering upcoming deadlines, or managing time-sensitive schedules in Power BI or Excel. To use it, you need a column formatted as a DateTime type and a numeric value representing the hour window you wish to check.
What You'll Need
- A dataset imported into the Power Query Editor (via Excel or Power BI).
- A column containing date and time data (formatted as the
DateTimedata type). - Basic familiarity with the "Add Custom Column" interface.
Step-by-Step
- Open the Power Query Editor: In Excel, go to the Data tab and select Get Data > Launch Power Query Editor. In Power BI, click on Transform Data from the Home ribbon.
- Verify Data Types: Locate the column containing your timestamps. Look at the icon to the left of the column header. If it shows a calendar and a clock, it is already a DateTime type. If it only shows a calendar (Date) or a clock (Time), right-click the column header, select Change Type, and choose DateTime. The function will return an error if the input is not a DateTime value.
- Create a Custom Column: Navigate to the Add Column tab on the top ribbon and select Custom Column. This opens the Custom Column dialog box where you can write your M formula.
- Enter the Function Formula: In the Custom column formula box, type the following syntax:
= DateTime.IsInNextNHours([YourColumnName], 24). Replace[YourColumnName]by double-clicking your timestamp column from the list on the right. Replace24with the number of hours you want to look ahead (e.g., use 1 for the next hour, 48 for the next two days). - Validate the Formula: Check the bottom of the dialog box for the message "No syntax errors have been detected." If you see a red error message, ensure you have used a comma to separate the column reference and the number of hours, and that the function name is capitalized exactly as
DateTime.IsInNextNHours(M is case-sensitive). - Confirm and Apply: Click OK. A new column will appear containing TRUE for any row where the timestamp occurs between the current system time and the end of the specified hour window, and FALSE for everything else.
- Filter the Results: If you only want to see the upcoming events, click the filter dropdown arrow on the new custom column header and uncheck FALSE.
- Clean Up: Once you have filtered your data, you can right-click the custom TRUE/FALSE column and select Remove. The filter step remains in the Applied Steps list, keeping your data filtered without needing the helper column in your final report.
Common Mistakes
One of the most frequent errors is using a Date column instead of a DateTime column. Because the function calculates based on hours, it requires a precise time component. If you provide a Date value, Power Query may attempt to coerce it, but this often leads to unexpected results or errors. Always ensure the data type is explicitly set to DateTime before applying the function.
Case sensitivity is another major pitfall. Power Query's M language is strictly case-sensitive. Writing datetime.isinnextnhours or DateTime.IsInNextNHours (with incorrect casing) will result in a "The name '...' was not recognized" error. The function must be written exactly as DateTime.IsInNextNHours.
Users often forget that this function is relative to the current time of the system running the refresh. If you are developing a report on your local machine and then publish it to a cloud service, the "current time" will be the time of the server (often UTC), not your local timezone. This can cause your TRUE/FALSE results to shift by several hours.
Another mistake is attempting to use a negative number for the hour argument. This function is specifically designed to look forward in time. If you need to check if a time was in the previous N hours, you must use the DateTime.IsInPreviousNHours function instead.
Tips
To make your reports more flexible, avoid hard-coding the number of hours into the formula. Instead, create a Parameter in Power Query (Home > Manage Parameters). Name it HourWindow and give it a numeric value. In your custom column formula, use = DateTime.IsInNextNHours([YourColumnName], HourWindow). This allows you to change the look-ahead window for the entire report without editing the M code in every step.
If you are dealing with large datasets, apply the DateTime.IsInNextNHours filter as early as possible in your query steps. Filtering rows early reduces the amount of data that subsequent steps (like merges or complex transformations) have to process, which significantly improves the refresh speed of your dashboard.
Combine this function with conditional formatting in Power BI. By bringing the TRUE/FALSE column into your final model, you can set a rule that highlights a row in red if the value is TRUE, providing an immediate visual cue for urgent upcoming deadlines.
When working with different time zones, use DateTimeZone.UtcNow() to standardize your current time reference before comparing it to your data. This ensures consistency regardless of where the report is being refreshed.
Troubleshooting
If the function returns an error for every row, the first check is the data type. Right-click the timestamp column and ensure it is set to DateTime. If it is set to "Any" or "Text," the function will fail. If the data is currently text, use the "Replace Values" tool to fix any formatting issues before changing the type to DateTime.
If the function returns FALSE for rows that you believe should be TRUE, check your system clock and the timezone of your data. If your data is in UTC but your system is in EST, there is a multi-hour gap that might push your timestamps outside the window. To fix this, use the "Add Column" > "Custom Column" feature to adjust your timestamp column to the correct timezone using DateTimeZone.SwitchZone before applying the IsInNextNHours function.
If the Power Query Editor becomes sluggish after adding the custom column, it may be because the function is being recalculated for every row during the preview. Try clicking "Refresh Preview" to see if the performance stabilizes, or apply a filter to the data before adding the custom column to reduce the row count.
If you encounter a "Token Literal Expected" error, check your comma and brackets. Ensure the column name is enclosed in square brackets [ ] and that there is a single comma separating the column reference from the number of hours.
Frequently Asked Questions
Can I use this function for days instead of hours?
While you can multiply days by 24 (e.g., use 72 for 3 days), it is more efficient to use the DateTime.IsInNextNDays function if you only care about the date and not the specific hour of the day.
Does this function include the current hour?
Yes, the function checks if the timestamp falls between the current moment and the moment exactly N hours from now. If a timestamp is 10 minutes from now, it will return TRUE.
Is there a way to check for a range, like between 2 and 5 hours from now?
You cannot do this with a single IsInNextNHours function. You would need to create two custom columns (one for 5 hours and one for 2 hours) and filter for rows where the 5-hour check is TRUE and the 2-hour check is FALSE.
Will this function update automatically when the report refreshes?
Yes. Because the function relies on the current system time, every time the data is refreshed, the TRUE/FALSE values are recalculated based on the new current time.
Can I use this function in a DAX calculated column instead?
No, DateTime.IsInNextNHours is an M function used in the Power Query Editor. For a similar result in DAX, you would need to use a combination of NOW() and date subtraction logic in a calculated column.
What happens if the timestamp is in the past?
If the timestamp is even one second in the past, the function will return FALSE, as it only looks forward from the current time.