Power Query Date.IsInPreviousMonth Function Guide
In this article
The Date.IsInPreviousMonth function in Power Query checks whether a given date falls within the calendar month immediately before the current date. It returns true or false and is especially useful for filtering data to include only records from last month— ideal for monthly reporting without manual date adjustments. To use it, you need a column containing valid date values in your query; no external tools or setup are required beyond having Power Query available in Excel or Power BI.
What You'll Need
- A dataset loaded into Power Query (in Excel 2016+ or Power BI Desktop)
- A column with date or datetime values (e.g., OrderDate, TransactionDate)
- Basic familiarity with adding custom columns in Power Query Editor
Step-by-Step
- Open your workbook or report in Excel or Power BI Desktop and ensure your data is loaded into Power Query. In Excel, go to the Data tab > Get Data > Launch Power Query Editor; in Power BI, click Transform Data in the Home tab.
- In the Power Query Editor, locate the column containing your dates. If your data lacks a date column, add one first or load a separate date table—Date.IsInPreviousMonth cannot infer dates from text or numbers alone.
- Right-click the date column header and choose Add Column > Custom Column (or go to the Add Column tab > Custom Column button in the ribbon).
- In the Custom Column dialog box, enter a name for your new column, such as LastMonthFlag.
- In the Formula box, type the following expression, replacing "DateColumn" with your actual column name (e.g., [OrderDate]):
Date.IsInPreviousMonth([DateColumn]) - Click OK. The new column will appear with true/false values, where true indicates the date falls in the previous calendar month relative to today's date (based on your system clock).
- To filter for only last month’s data, click the filter icon on the new column header, uncheck false, and leave true selected. Then click OK. Power Query will now show only rows where the date falls in the previous month.
- Use this filtered dataset for your report or load it back into Excel/Power BI. If you want to keep the flag column for future reference, leave it in place; otherwise, right-click the column and choose Remove.
Common Mistakes
- Using a text or number column instead of a true date type: Date.IsInPreviousMonth requires a date/datetime type. If your column is set to Whole Number or Text, convert it first by right-clicking the column > Change Type > Date or DateTime.
- Assuming it’s relative to the current month in your report context: The function always uses the *current system date*, not the latest date in your dataset. If your data ends in January but today is March 15, it will still return true for February records—even if February isn’t the "previous month relative to the last data point."
- Confusing it with Date.IsInCurrentMonth or Date.IsInNextMonth: Double-check the function name. A typo like "IsInPreviouMonth" (missing an "s") will cause an error.
- Forgetting to handle nulls: If your date column contains null values, Date.IsInPreviousMonth returns null. This may cause issues in filters. Use the null-coalescing operator (e.g.,
if [DateColumn] = null then false else Date.IsInPreviousMonth([DateColumn])) if you want to treat nulls as false.
Tips
- For dynamic monthly reports that update automatically each month, combine Date.IsInPreviousMonth with a parameter or a date table that includes today’s date as a calculated column (e.g., using DateTime.LocalNow()). This ensures consistency across users with different system times.
- To compare with *last month’s same period* (e.g., Feb 1–28 vs. Jan 1–28), consider using Date.AddMonths([DateColumn], -1) in a separate column to shift dates back, then compare to the current month using Date.IsInCurrentMonth.
- When using this in a calculated column in Power BI’s Data view, note that it will recalculate every time the data is refreshed—and again on each user’s machine at refresh time, using their local system time. For report-level consistency, use a static reference date (e.g., a parameter or a dedicated date table with a Today column).
- Test the function manually: In the Power Query Editor, create a custom column with just
Date.IsInPreviousMonth(#date(2024, 2, 15))to confirm behavior in February 2024 (should return true). - Use the Advanced Editor to paste the full M code for review. A working example looks like:
Table.AddColumn(Source, "LastMonth", each Date.IsInPreviousMonth([OrderDate]), type logical)
Troubleshooting
If Date.IsInPreviousMonth isn’t working as expected, follow these checks:
- Verify the column type: Select the date column, go to the Transform tab, and confirm the data type icon shows a calendar (Date) or clock (DateTime). If not, convert it. A common fix is to select the column > Home > Transform > Data Type > Date or DateTime.
- Check your system date: Date.IsInPreviousMonth relies on the current date of the machine running Power Query. Open Excel or Power BI and type =TODAY() in a cell to verify the system date is correct. If it’s wrong, update your computer’s clock and retry.
- Handle time zones: In Power BI, DateTime.LocalNow() uses the local time zone of the report author, not the server. If your data is stored in UTC and you’re filtering by local date, convert times first (e.g., using Date.From(DateTime.FixedLocalNow()) or Date.From([DateTimeColumn], "en-US") with appropriate offset).
- Debug nulls and errors: Add a custom column with
try Date.IsInPreviousMonth([DateColumn])to catch errors. If you see errors, inspect the source for invalid dates (e.g., "N/A", "2024-13-01"). Clean the data using Remove Errors or Replace Values before applying the function. - Refresh the query: Even after fixing the column, the flag may not update until you click Close & Apply (Power BI) or Close & Load (Excel). Also, ensure Auto-Refresh isn’t disabled in Power BI Desktop (File > Options and settings > Options > Current File > Data Load > uncheck Disable automatic refresh).
- Recreate the column: If the custom column is misbehaving, delete it, re-add it, and re-type the formula (don’t copy-paste—some editors introduce invisible characters). Use square brackets around column names: [ColumnName], not quotes.
Frequently Asked Questions
Does Date.IsInPreviousMonth work in DAX or just Power Query?
No—it is a Power Query (M language) function and does not exist in DAX. For similar logic in Power BI report visuals, use DAX like: .
Can I use this to get all dates from last month, not just flag them?
Yes. After adding the flag column, filter for true as described in Step 8. Alternatively, use a more direct filter in the query: Table.SelectRows(Source, each Date.IsInPreviousMonth([DateColumn]))—replace "Source" with your previous step name.
Does this account for leap years or varying month lengths?
Yes. Date.IsInPreviousMonth uses the built-in calendar logic of the .NET framework, so February 29 in leap years and months with 28, 30, or 31 days are handled correctly. You don’t need to write extra logic for this.
Why does my flag return null for some rows?
Null inputs produce null outputs. Check for blank or missing values in your date column using the Filter icon > Blanks. Decide whether to exclude those rows (Home > Remove Rows > Remove Blank Rows) or replace nulls with a default date before applying the function.
Will this work across time zones if my data is global?
Not reliably—Date.IsInPreviousMonth uses the local system date. For global data, convert all dates to UTC first (using Date.From([DateTimeColumn], "en-US") or Time zone functions), then compare to UTC today. Better yet, use a separate date table with a calculated Today column set at refresh time.
Can I chain multiple date conditions, like last month but only Mondays?
Absolutely. Combine with other Date functions: (assuming Monday = 1 in your system’s week start). Adjust the day number based on your culture settings (e.g., 0 for Sunday in US culture).