Power Query DateTime.IsInPreviousHour Function Guide
In this article
The DateTime.IsInPreviousHour function in Power Query checks whether a given datetime value falls within the hour immediately before the current system time. It returns true if the datetime is within the last full hour, false otherwise. This function is useful for filtering recent data, such as log entries or sensor readings, to focus only on the prior hour’s activity. No special setup is required beyond having a column of datetime values in your data table.
What You'll Need
- Microsoft Excel 2016 or later with Power Query enabled, or Microsoft Power BI Desktop
- A data table containing at least one column with datetime values (date and time combined)
- Access to the Power Query Editor (via Data > Get & Transform > Edit Queries in Excel, or Transform Data in Power BI)
Step-by-Step
- Open your workbook or Power BI file and load the data containing datetime values into Power Query by selecting the data range and clicking Data > Get & Transform > From Table/Range (Excel) or Transform Data (Power BI).
- In the Power Query Editor, identify the column containing datetime values. Ensure the column’s data type is set to Date/Time — if it’s text, right-click the column header, select Change Type > Date/Time.
- Click the Add Column tab in the ribbon, then select Custom Column.
- In the Custom Column dialog box, enter a name for the new column, such as
IsInPreviousHour. - In the formula box, type:
DateTime.IsInPreviousHour([YourColumnName]), replacingYourColumnNamewith the actual name of your datetime column. For example, if your column is namedEventTime, the formula becomesDateTime.IsInPreviousHour([EventTime]). - Click OK. A new column will appear with boolean values:
trueif the datetime falls within the previous hour,falseotherwise. - To filter your data to show only records from the previous hour, click the dropdown arrow in the new column header, uncheck
false, and leave onlytrueselected. - Click Close & Load (Excel) or Close & Apply (Power BI) to apply the transformation and return the filtered data to your worksheet or report.
Common Mistakes
- Using the function on a text column instead of a Date/Time column — this causes an error because the function requires a datetime value, not a string.
- Typing the function name incorrectly, such as
DateTime.IsInPreviousHourwith extra spaces, wrong capitalization, or missing parentheses. - Assuming the function uses UTC or a custom timezone — it uses the local system time of the device running Power Query, so if your data is in a different timezone, results may be misleading.
- Expecting the function to compare against a static time reference — it always uses the current system time at the moment of refresh, so results change as time passes.
- Forgetting to refresh the query after the hour changes — if you opened the file at 2:30 PM and refreshed at 4:15 PM, the function will now evaluate against 3:00–4:00 PM, not 1:00–2:00 PM as you may expect.
Tips
- Combine this function with other time-based filters like
DateTime.IsInCurrentDayorDateTime.IsInPreviousDayto create layered time windows for analysis. - If you need to compare against a fixed reference hour (e.g., always the hour before 12:00 PM), create a custom column that subtracts one hour from a static time and use
DateTime.Compareinstead. - Use this function in combination with
Table.SelectRowsin advanced queries for dynamic filtering logic in M code. - When building dashboards, consider adding a timestamp column showing when the data was last refreshed so users understand the dynamic nature of the
IsInPreviousHourresult. - Test the function with sample dates manually entered in a column — for example, set a row’s datetime to
2024-05-15 14:30:00and confirm it returns true only when the system time is between 15:00:00 and 15:59:59.
Troubleshooting
If the function returns an error, start by verifying the data type of the column. Right-click the column header and check the data type — if it says Text, convert it to Date/Time. If conversion fails, inspect the data for invalid entries like "N/A", blank cells, or malformed strings (e.g., "2024/15/01" instead of "2024-01-15"). Use the Replace Values or Remove Rows tools to clean these before applying the function.
If the function returns false for times you expect to be in the previous hour, confirm your system clock is correct. Power Query relies on the operating system’s time. If your computer’s clock is off by more than an hour, the function will not behave as expected.
If you’re working in a shared environment (like Power BI Service), note that the function evaluates using the server’s local time, not your local time. This can cause discrepancies if your timezone differs from the server’s. In such cases, use DateTimeZone.From and DateTimeZone.SwitchZone to convert your datetime to UTC before applying DateTime.IsInPreviousHour for consistency.
If the query refreshes but the column doesn’t update, click Refresh All in Excel or Refresh in Power BI. If that doesn’t help, close and reopen the file — sometimes the editor caches old evaluations.
There is no built-in way to make the function use a different reference hour (e.g., the hour before 9:00 AM) without custom logic. To achieve that, create a custom column using: DateTime.Compare([EventTime], DateTime.LocalNow() - #duration(0,1,0,0)) = 0 — this compares the time against the exact one-hour window prior to now.
Frequently Asked Questions
Does DateTime.IsInPreviousHour work with time-only values?
No. The function requires a full datetime value (date and time). If you have only a time value, you must combine it with a date first. Use Date.From(DateTime.LocalNow()) to get today’s date and add your time value using DateTime.From.
Can I use this function in Power Query Online (Power BI Service)?
Yes, but the function uses the server’s local time zone, not your personal time zone. If your data is collected in UTC and you’re in EST, results may appear offset. Use DateTimeZone.SwitchZone to convert to UTC before applying the function.
Why does the result change when I reopen the file later?
Because the function always compares to the current system time when the query refreshes. If you open the file at 3:15 PM, it checks whether the datetime is between 2:00 PM and 3:00 PM. If you reopen it at 5:20 PM, it now checks against 4:00–5:00 PM. The result is dynamic, not static.
Can I use this function to compare against yesterday’s previous hour?
No. The function only evaluates the immediate prior hour relative to the current system time. To compare against yesterday’s 2:00–3:00 PM window, create a custom column that subtracts 25 hours from the current time and use DateTime.Compare to check for equality.
Does this function work with time zones other than local time?
Not directly. It uses the system's local time zone. To handle multiple time zones, convert your datetime column to UTC using DateTimeZone.ToUTC first, then apply DateTime.IsInPreviousHour — but be aware this will reflect UTC’s previous hour, not your local one.
Will this function slow down my query on large datasets?
It has minimal performance impact because it’s a lightweight scalar operation. However, if you’re applying it to millions of rows, consider filtering data earlier in the query pipeline (e.g., by date range) to reduce the number of rows processed by this function.