Smart Systems for Home

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

Power Query Date.DaysInMonth Function 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.DaysInMonth function in Power Query is used to return the total number of days in a specific month of a given year. To use it, you wrap a date value inside the function, like this: Date.DaysInMonth([YourDateColumn]). This is essential for calculating daily averages, prorating costs, or creating custom calendars where the number of days varies between 28 and 31.

What You'll Need

  • Microsoft Excel or Power BI Desktop.
  • A dataset containing at least one column formatted as a Date or Date/Time data type.
  • Access to the Power Query Editor (Get & Transform Data).

Step-by-Step

  1. Open the Power Query Editor: In Excel, go to the Data tab and select Get Data > Launch Power Query Editor. In Power BI, click on Transform Data from the Home ribbon.
  2. Verify Data Types: Locate the column containing your dates. Look at the icon to the left of the column header. It must be a calendar icon (Date) or a calendar-clock icon (Date/Time). If it is an "abc" (Text) or "123" (Whole Number) icon, right-click the column header, select Change Type, and choose Date. The Date.DaysInMonth function will return an error if it is applied to a non-date data type.
  3. Create a Custom Column: Navigate to the Add Column tab on the top ribbon. Select Custom Column. This opens the Custom Column dialog box where you can write your M code formula.
  4. Enter the Formula: In the New column name field, type something descriptive, such as "Days in Month". In the Custom column formula box, start by typing Date.DaysInMonth(.
  5. Reference the Date Column : While your cursor is inside the parentheses, look at the Available columns list on the right side of the dialog box. Double-click the name of your date column. The formula should now look like .
  6. Close and Validate: Click OK. Power Query will generate a new column containing integers (e.g., 28, 30, 31) corresponding to the month of each date in that row.
  7. Set the New Column Type: By default, the new column may be set to the "Any" data type. Right-click the new column header, select Change Type, and choose Whole Number to ensure it is treated as a numeric value for future calculations.

Common Mistakes

One of the most frequent errors is attempting to use the function on a column that is formatted as Text. In Power Query, "2023-01-01" as text is not the same as a Date object. If you see the word "Error" in every cell of your new column, check your source data type immediately. Another common mistake is case sensitivity. Power Query M language is case-sensitive; typing date.daysinmonth (all lowercase) will result in a "Token Literal expected" or "Expression.Error: The name 'date.daysinmonth' wasn't recognized" because the function must start with a capital 'D' for both Date and DaysInMonth.

Users also sometimes confuse this function with Date.Day. While Date.Day returns the specific day of the month for a given date (e.g., for January 15th, it returns 15), Date.DaysInMonth returns the total capacity of that month (for January, it always returns 31). Using the wrong function will lead to incorrect calculations in financial reports or KPI dashboards.

Tips

To make your reports more dynamic, combine Date.DaysInMonth with division to calculate a "Daily Rate." For example, if you have a monthly budget column, you can create a custom column with the formula . This gives you a precise daily spend that accounts for leap years and shorter months like February.

If you are building a Date Table (Calendar Table) from scratch using a list of dates, use this function to create a "Month Length" attribute. This is incredibly useful for DAX measures in Power BI, as it allows you to calculate the percentage of the month that has elapsed by dividing the current day of the month by the total days in that month.

For those working with very large datasets, avoid creating too many intermediate custom columns. Instead of creating a column for the number of days and then another for the daily average, you can nest the function directly into your final calculation to reduce the memory footprint of your query.

Troubleshooting

If the function is not working as expected, follow these steps in order:

  1. Check for Nulls : If your date column contains null values, the Date.DaysInMonth function will return a null. If you need a default value (like 30), use an if-statement: .
  2. Verify Case Sensitivity: Double-check that you have used Date.DaysInMonth exactly. Ensure there is a dot after "Date" and that both "Date" and "DaysInMonth" start with uppercase letters.
  3. Inspect the Data Type: Even if the column looks like a date, it might be "Any" type. Force the type to Date by clicking the icon in the column header and selecting Date.
  4. Refresh the Preview: Sometimes the Power Query preview does not update immediately. Click "Refresh Preview" in the Home tab to ensure the errors aren't just cached artifacts.

Frequently Asked Questions

Does this function handle leap years automatically?

Yes. The function looks at the specific year and month of the date provided. If the date is in February of a leap year, it will return 29; otherwise, it returns 28.

Can I use this function with a Date/Time column?

Yes, it works with both Date and DateTime types. The function ignores the time component and only evaluates the month and year of the date portion.

Is there a way to get the days in the current month regardless of a column?

Yes, you can use Date.DaysInMonth(DateTime.LocalNow()). This uses the current system date to determine the current month and returns the total days for it.

What happens if the date is invalid?

If the input is not a valid date object, Power Query will return a Data.Error. You can handle this by right-clicking the column and selecting "Replace Errors" to fill those cells with a zero or a null.

Can I use this in a Power BI DAX measure instead of Power Query?

While you can achieve this in DAX using the DAY(EOMONTH(date, 0)) pattern, using Date.DaysInMonth in Power Query is generally more efficient because it calculates the value during the data load process rather than at query time in the report.