Power Query Date.Month Function: Step-by-Step Guide
In this article
The Date.Month function in Power Query extracts the month number (1–12) from a given date value. It requires a valid date or datetime value as input and works reliably in both Power BI Desktop and Excel’s Power Query Editor. No additional setup is needed beyond having Power Query available.
What You'll Need
- A dataset loaded into Power Query (in Excel or Power BI Desktop)
- A column containing date or datetime values (e.g., "OrderDate", "StartDate")
- Basic familiarity with adding custom columns in Power Query
Step-by-Step
- Open Power Query Editor: In Excel, go to the Data tab > Get Data > Launch Power Query Editor (or select Transform Data if your query is already open). In Power BI Desktop, click Home > Transform Data to open the editor.
- Select the date column: In the Queries pane, click on the table containing your date data. Find the column with the date values (e.g., "Order Date", "BirthDate") and click its header to highlight the column.
- Add a custom column: Go to the Add Column tab > Custom Column. A dialog box titled Custom Column will appear.
- Enter the Date.Month formula: In the Custom column name field, type a descriptive name like
MonthNumber. In the Custom column formula field, type:Date.Month([YourColumnName])— replaceYourColumnNamewith the exact name of your date column (including brackets and square brackets). For example, if your column is named "OrderDate", the formula should readDate.Month([OrderDate]). - Confirm and apply: Click OK. A new column will appear in your query editor, showing the month number (1 for January, 12 for December) for each row. Hover over the column header to see the data type—ensure it’s set to Whole Number.
- Load or close and load: Click Close & Load (in Excel) or Close & Apply (in Power BI) to push the updated data back to your worksheet or report. The new month column will now be available for filtering, grouping, or visualizing.
Common Mistakes
- Missing brackets around the column name: Using
Date.Month(OrderDate)instead ofDate.Month([OrderDate])causes an error because Power Query interpretsOrderDateas a variable, not a column reference. - Using text or non-date columns: If your column contains text (e.g., "2024-03-15" as text),
Date.Monthreturns an error. Convert text to a date first usingDate.FromorDateTime.From. - Case sensitivity:
Date.month(lowercase m in Month) ordate.monthwill fail—Power Query formula syntax is case-sensitive, so useDate.Monthexactly. - Ignoring nulls: If your date column contains null values,
Date.Monthreturns null. This may cause issues in visuals or aggregations if not handled.
Tips
- Combine with other date functions: For month names (e.g., "March"), use
Date.MonthName([OrderDate])instead ofDate.Month. To get the year-month (e.g., "2024-03"), tryText.From(Date.Year([OrderDate])) & "-" & Text.PadStart(Text.From(Date.Month([OrderDate])), 2, "0"). - Handle errors gracefully: Wrap
Date.Monthintry ... otherwise ...to avoid query failures. Example:try Date.Month([OrderDate]) otherwise null. - Verify data types: Before applying
Date.Month, right-click the column > Data Type > ensure it’s set to Date or DateTime. If unsure, add a step usingTable.TransformColumnsto convert:= Table.TransformColumns(PreviousStep, {"OrderDate", each Date.From(_), type date}). - Use in grouping: After loading to Excel or Power BI, use the new
MonthNumbercolumn to create sorted slicers or axes—Power BI allows sorting by this column to ensure January appears first.
Troubleshooting
If Date.Month isn’t working, follow these checks:
- Restart Power Query: Close and reopen Excel or Power BI Desktop. Sometimes the M engine cache causes odd syntax errors.
- Check for hidden characters: In your original data, look for leading/trailing spaces or non-breaking spaces (especially from web exports). Use
Text.Trimbefore conversion:Date.Month(Date.From(Text.Trim([OrderDate]))). - Confirm column existence: In the Advanced Editor (View > Advanced Editor), verify your column name matches exactly—no extra spaces or typos. For example, "OrderDate" ≠ "Order Date".
- Test with a static date: Temporarily replace the column reference with a literal:
Date.Month(#date(2024, 5, 15))should return5. If this works, your issue is with the column data, not the function. - Check regional settings: While
Date.Monthitself is locale-independent, date parsing functions (e.g.,Date.From) can behave differently if your system uses non-Gregorian calendars. In Power BI, go to File > Options and settings > Options > Current Workspace > Regional Settings and ensure United States or English (United States) is selected for consistency. - Rebuild the step: If all else fails, delete the custom column step in the Applied Steps pane and recreate it—corrupted steps sometimes silently fail.
Frequently Asked Questions
Can Date.Month handle time zones?
No—Date.Month operates on the date portion only and does not consider time zones. If your datetime values include time zone offsets (e.g., from an API), use DateTimeZone.ToLocal or DateTimeZone.RemoveZone first to normalize the value before extracting the month.
Why does my result show decimals (e.g., 3.0 instead of 3)?
This usually happens when the column’s data type is set to Decimal Number instead of Whole Number. Right-click the column > Data Type > select Whole Number. Power Query stores month numbers as decimals internally, but the display can be adjusted.
Does Date.Month work with fiscal calendars?
Not directly. Date.Month returns the calendar month (1–12). For fiscal years starting in July or October, use Date.AddMonths to shift the date first: e.g., Date.Month(Date.AddMonths([OrderDate], -6)) for a fiscal year beginning in July.
Can I use Date.Month in a conditional column?
Yes—combine it with if ... then ... else. Example: if Date.Month([OrderDate]) > 6 then "H2" else "H1" to label halves of the year. Access this via Add Column > Conditional Column or write the M code directly in a custom column.
What happens if the input is blank?
Date.Month(null) returns null. To replace this with a default (e.g., 0), use: if [OrderDate] = null then 0 else Date.Month([OrderDate]).
Is Date.Month the same as MONTH() in Excel formulas?
Yes—the logic is identical. If you have an Excel formula like =MONTH(A2), the Power Query equivalent is Date.Month([A2]), assuming [A2] is a date column. Power Query applies the function row-by-row during transformation, not live recalculations.