Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Date.Month Function: Step-by-Step Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. 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.
  2. 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.
  3. Add a custom column: Go to the Add Column tab > Custom Column. A dialog box titled Custom Column will appear.
  4. 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]) — replace YourColumnName with the exact name of your date column (including brackets and square brackets). For example, if your column is named "OrderDate", the formula should read Date.Month([OrderDate]).
  5. 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.
  6. 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 of Date.Month([OrderDate]) causes an error because Power Query interprets OrderDate as 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.Month returns an error. Convert text to a date first using Date.From or DateTime.From.
  • Case sensitivity: Date.month (lowercase m in Month) or date.month will fail—Power Query formula syntax is case-sensitive, so use Date.Month exactly.
  • Ignoring nulls: If your date column contains null values, Date.Month returns 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 of Date.Month. To get the year-month (e.g., "2024-03"), try Text.From(Date.Year([OrderDate])) & "-" & Text.PadStart(Text.From(Date.Month([OrderDate])), 2, "0").
  • Handle errors gracefully: Wrap Date.Month in try ... 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 using Table.TransformColumns to convert: = Table.TransformColumns(PreviousStep, {"OrderDate", each Date.From(_), type date}).
  • Use in grouping: After loading to Excel or Power BI, use the new MonthNumber column 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:

  1. Restart Power Query: Close and reopen Excel or Power BI Desktop. Sometimes the M engine cache causes odd syntax errors.
  2. Check for hidden characters: In your original data, look for leading/trailing spaces or non-breaking spaces (especially from web exports). Use Text.Trim before conversion: Date.Month(Date.From(Text.Trim([OrderDate]))).
  3. 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".
  4. Test with a static date: Temporarily replace the column reference with a literal: Date.Month(#date(2024, 5, 15)) should return 5. If this works, your issue is with the column data, not the function.
  5. Check regional settings: While Date.Month itself 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.
  6. 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.