Power Query Date.IsInYearToDate Function Guide
In this article
The Date.IsInYearToDate function in Power Query checks whether a given date falls within the current year up to today’s date (i.e., from January 1 of the current year through the current day). This function is useful for filtering or flagging records relevant to the year-to-date (YTD) period, such as sales, expenses, or project milestones. It requires a valid date or datetime value as input and returns true or false. No additional setup or data preparation is required beyond having Power Query available in Excel or Power BI.
What You'll Need
- Excel (2016 or later) or Power BI Desktop
- A data table or query containing a date column (e.g., OrderDate, TransactionDate)
- Basic familiarity with Power Query Editor (opening it via Data > Get Data or Power BI’s Transform Data)
Step-by-Step
- Open your Excel workbook or Power BI Desktop file and load the data containing the date column you want to evaluate. If the date column is not already in
dateordatetimeformat, convert it first: select the column, right-click, choose Change Type, and set it to Date or DateTime. - Launch the Power Query Editor. In Excel, go to the Data tab > Get Data group > Launch Power Query Editor. In Power BI, click Transform Data on the Home tab.
- In the Power Query Editor, locate the query containing your date column. Click the dropdown arrow at the top of the date column header, then choose Add Column > Custom Column from the ribbon.
- In the Custom Column dialog box, enter a name for the new column (e.g.,
IsYTD). In the Custom column formula box, type:Date.IsInYearToDate([YourDateColumn]). ReplaceYourDateColumnwith the actual name of your date column (e.g.,[OrderDate]). Ensure the column name is enclosed in square brackets and matches exactly (case-sensitive). - Click OK. A new column appears with
truefor dates from January 1 of the current year through today’s date, andfalseotherwise. For example, if today is May 20, 2024, a date of April 15, 2024 returnstrue, but December 1, 2023 returnsfalse. - To filter for only YTD records, click the filter icon on the new column header, uncheck
false, and keeptrueselected. Click OK. Alternatively, right-click the column and choose Filter > Filter Rows to apply more complex logic later. - Click Close & Load (Excel) or Close & Apply (Power BI) to load the updated table with the new YTD flag into your worksheet or report.
Common Mistakes
- Incorrect column reference: Using the wrong column name (e.g.,
[Order Date]instead of[OrderDate]) causes aNameerror. Always verify spelling and spacing in the column header. - Non-date data types: Passing text, numbers, or null values to
Date.IsInYearToDatethrows aExpression.Error. Ensure the input column is formatted as Date or DateTime before using the function. - Assuming static year boundaries: The function uses the *current system date* at query execution time—not a fixed reference like the report’s last refresh date. If you run the query weeks later, the YTD window will have shifted, potentially changing results unexpectedly.
- Time zone confusion: If your data contains
datetimevalues with time components, Power Query evaluates them against the *local system time*. A record dated2024-01-01 00:00:01may be treated differently depending on system locale, though the date portion (Jan 1) is usually sufficient forIsInYearToDateto returntrue.
Tips
- Combine with Date.Year for custom YTD: If you need YTD relative to a fiscal year (e.g., starting July 1), create a custom column like: .
- Cache the current date: To prevent YTD results from changing daily without your knowledge, add a parameter (e.g., CurrentDate = DateTime.LocalNow() ) and reference it in your logic: is *not* supported—instead, use after defining CurrentDate as a static value (e.g., via a parameter table).
- Use in conditional logic: Nest the function inside if statements to return custom labels: .
- Check for nulls first: Wrap the function in try ... otherwise or use to avoid errors on missing values.
Troubleshooting
If Date.IsInYearToDate returns errors or unexpected results, follow these steps:
- Verify the system clock: The function relies on the current date from your device. Open Windows Date & Time Settings (or macOS Date & Time) and confirm the year, month, and day are correct. An incorrect system date will shift the YTD boundary.
- Check column data type: Right-click the date column in Power Query > Change Type > Date. If the column shows as Any or Text, conversion is required before the function works.
- Test with a known date: Add a custom column with
= Date.IsInYearToDate(#date(2024, 3, 15))(today is assumed to be after March 15, 2024). If this returnstrue, your input column likely has formatting issues. - Inspect the error message: If you see Expression.Error: The name '...' was not found , double-check the column name in the formula. If it’s Expression.Error: We cannot apply operator < to types Date and Null , your column contains nulls—handle them with .
- Refresh the query: After fixing data types or logic, click Close & Load, then right-click the query in Excel’s Queries & Connections pane (or Power BI’s Fields pane) and choose Refresh to re-evaluate the function with the current system date.
Frequently Asked Questions
Does Date.IsInYearToDate work with past or future years?
Yes, but only relative to the current system date. A date in 2020 will return false in 2024 because it’s not part of *this* year’s YTD window (Jan 1–today, 2024).
Can I use this function to compare YTD across multiple years?
No—this function only flags dates within the *current* year. To compare YTD across years, calculate a separate YTDFlag for each year using Date.Year and Date.DayOfYear, then compare totals in your report (e.g., via DAX in Power BI).
Is there a way to hardcode a specific “current date” for YTD calculations?
Not directly with Date.IsInYearToDate , since it always uses the system date. To simulate a fixed reference date, create a parameter (e.g., named ReportDate ) set to 2024-06-30 , then build logic like: .
Why does my function return true for today but false for yesterday?
This can happen if your system date changed unexpectedly (e.g., due to daylight saving time or manual adjustment). Also, ensure your date column doesn’t include time components that push it just before midnight (e.g., 2024-01-01 23:59:59 is still part of Jan 1).
Does Date.IsInYearToDate handle leap years?
Yes—internally, Power Query uses standard .NET date handling, which correctly accounts for February 29 in leap years. A date of 2024-02-29 will be treated as valid and included in YTD if today is on or after that date.
Can I use this function in Power BI DirectQuery mode?
Yes, but the function is evaluated on the *server* using the server’s system date, not your local machine. Results may differ across users if servers are in different time zones or if the server date is managed separately.