Date.DaysInMonth Function in Power Query M

Sumit Bansal
Written by

Sumit Bansal is the founder of TrumpExcel.com and a 13-time Microsoft Excel MVP. He started this site in 2013 to share his passion for Excel through easy tutorials, tips, and training videos, helping you master Excel, boost productivity, and maybe even enjoy spreadsheets!

The Date.DaysInMonth function tells you how many days there are in the month of a date, so you get 28, 29, 30, or 31. It’s useful whenever you need to split a monthly number into days, like a daily average or a part-month rent.

I’ll show you how the function works first, and then the most common ways to use it on real data.

Follow along with the example file

Date.DaysInMonth Function Examples.xlsx

Download

Date.DaysInMonth Function Syntax

Date.DaysInMonth(dateTime as any) as nullable number
  • dateTime – the date, datetime, or datetimezone value whose month you want the number of days for.

What it returns – the number of days in that month, as a whole number from 28 to 31.

The function only looks at the year and month of the value. The day and any time part don’t change the result, and if dateTime is null, you get null back instead of an error.

When to Use the Date.DaysInMonth Function

Use this function when you need to:

  • Find how many days a month has, including February in a leap year
  • Add a days-in-month column to a table of dates
  • Get the days in a month when you only have the year and month numbers
  • Turn a monthly total into a daily average
  • Work out a part-month (prorated) amount, such as the first month’s rent
  • Check whether a date or date-and-time falls on the last day of its month
  • List the number of days in every month of a year

Example 1: Get the Number of Days in a Month From a Date

Let’s start with the simplest use of the function.

Suppose you have the date April 14, 2026, and you want to know how many days April 2026 has.

Create a new blank query (in Excel, go to Data > Get Data > From Other Sources > Blank Query) and type this formula in the formula bar:

= Date.DaysInMonth(#date(2026,4,14))

Result: 30

Date.DaysInMonth(#date(2026,4,14)) returns 30

Here, #date(2026,4,14) creates the date April 14, 2026. Date.DaysInMonth looks at its month, April, and returns 30 because April has 30 days.

The day number doesn’t matter. April 1 or April 30 would also give 30, so any date in a month works as the input.

If you want the days in the current month, give the function today’s date instead of a fixed one. Here’s how to get today’s date in Power Query.

Example 2: Check February in a Leap Year

February is the month where Date.DaysInMonth saves you the most thinking.

Suppose you want to see how many days February has in 2027 and in 2028, where 2028 is a leap year.

Create a new blank query (as in Example 1) and use this formula:

= {Date.DaysInMonth(#date(2027,2,1)),Date.DaysInMonth(#date(2028,2,1))}

Result: A list with two values, 28 and 29.

A list showing 28 days for February 2027 and 29 for February 2028

Here, the curly brackets create a list, and each item in it runs Date.DaysInMonth on the first day of February. February 2027 has 28 days, and February 2028 has 29 because 2028 is a leap year.

You don’t have to write any leap-year logic yourself. The function also gets the rare cases right, like 2100, which isn’t a leap year even though it divides by 4, so February 2100 gives 28.

Example 3: Use Date.DaysInMonth With a Date and Time

The function works with a date and time too.

Suppose you have the date and time October 9, 2026 at 2:30 PM, and you want the number of days in that month.

Create a new blank query (as in Example 1) and use this formula:

= Date.DaysInMonth(#datetime(2026,10,9,14,30,0))

Result: 31

Date.DaysInMonth(#datetime(2026,10,9,14,30,0)) returns 31

Here, #datetime(2026,10,9,14,30,0) creates October 9, 2026 at 2:30 PM. The function ignores the time and returns 31, because October has 31 days.

Since the time is ignored, it doesn’t matter whether a column in your table has the Date type or the Date/Time type. Both give the same number, so you don’t need to change the type just for this function.

A datetimezone value works the same way. For example, 11 PM on June 30 at a -5:00 offset still gives 30 for June, because the function uses the date as written and doesn’t move it to July 1 in UTC first.

Example 4: Add a Days in Month Column to a Table

In real data, you’ll usually want the days in the month for a whole column of dates.

Below is a PhoneBills table with the date of each phone bill, and I want the number of days in the bill’s month in a new Days in Month column.

PhoneBills table with Account, Customer and Bill Date, one bill without a date

To follow along, select any cell in the PhoneBills table and go to Data > From Table/Range. Power Query adds a Changed Type step.

I’ve set the Bill Date column to Date in that step, so the dates show without a time. To do this, click the type icon in the Bill Date header, pick Date, and choose Replace current.

Date/Time would give the same results, as you saw in Example 3.

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.AddColumn(#"Changed Type","Days in Month",each Date.DaysInMonth([Bill Date]),Int64.Type)

Result: A new Days in Month column shows 31, 28, null, 30, 30, and 30.

New Days in Month column, with null for the bill that has no date

Here, the Table.AddColumn function adds the Days in Month column, and the each keyword runs Date.DaysInMonth on every row. [Bill Date] picks the date from the same row, and Int64.Type makes the new column a Whole Number column.

The first argument, #"Changed Type", is the name of the Changed Type step. So the new column is added to the table as it looks after that step.

Jack’s bill on 1/15/2026 gets 31 and Diana’s bill on 2/9/2026 gets 28. The dates show as month/day/year because my computer uses US regional settings, and a UK computer would show 15/01/2026 for the same date.

Douglas’s bill has no date yet, so his row gets null instead of an error. That’s handy, because one missing date doesn’t break the whole query.

Dates stored as text are different. If your dates came in as text and the column has the Text type, every row gives an Expression.Error that says “The Date value must contain the Date component.”

In that case, change the column type to Date in the Changed Type step first, and the function works as shown here.

Example 5: Get the Days in a Month From Year and Month Columns

Sometimes you don’t have a date at all, just a year and a month number in separate columns.

Below is a BudgetPlan table with a year, a month number, and a budget for each month, and I want the number of days in each month in a new Days in Month column.

BudgetPlan table with Year, Month and Budget

Load the table with Data > From Table/Range, and make sure the Changed Type step sets the Year and Month columns to Whole Number, which is what this formula needs.

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.AddColumn(#"Changed Type","Days in Month",each Date.DaysInMonth(#date([Year],[Month],1)),Int64.Type)

Result: A new Days in Month column shows 31, 28, 29, 30, 30, and 31.

New Days in Month column built from the Year and Month columns

Here, #date([Year],[Month],1) builds the first day of the month from the two columns. Date.DaysInMonth then returns the number of days in that month.

I’ve used 1 as the day because every month has a day 1. So the date is always valid, whichever month you’re in.

Notice how February gives 28 for 2027 and 29 for 2028. The year column decides the leap-year result for you.

Follow along with the example file

Date.DaysInMonth Function Examples.xlsx

Download

Example 6: Calculate a Daily Average From Monthly Totals

Here’s a common use case. You have monthly totals and want a fair daily average, even though months have different lengths.

Below is a WebsiteVisits table with the total visits for each month (each month is stored as its first day), and I want the average visits per day in a new Daily Average column.

WebsiteVisits table with Month and total Visits

Load the table with Data > From Table/Range, and make sure the Changed Type step sets the Month column to Date and the Visits column to a number type.

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.AddColumn(#"Changed Type","Daily Average",each Number.Round([Visits]/Date.DaysInMonth([Month]),0),type number)

Result: A new Daily Average column shows 1510, 1398, 1707, 1594, 1892, and 1810.

New Daily Average column with visits per day for each month

Here, each month’s visits are divided by the number of days in that month. The Number.Round function then rounds the answer to a whole number of visits.

So January’s 46,810 visits are divided by 31 to give 1510 a day. February has fewer visits (39,150), but it also has only 28 days, so its daily average is 1398.

If you divided every month by 30 instead, short months would look worse than they really were and long months would look better. Using the real number of days keeps the months comparable.

Example 7: Prorate Rent for a Partial Month

This next one comes up whenever something is charged by the month but starts partway through it.

Below is a Leases table with each tenant’s move-in date and monthly rent, and I want the rent for their first, partial month in a new First Month Rent column.

Leases table with Unit, Tenant, Move In Date and Monthly Rent

Load the table with Data > From Table/Range, and make sure the Changed Type step sets the Move In Date column to Date and the Monthly Rent column to a number type.

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.AddColumn(#"Changed Type","First Month Rent",each Number.Round([Monthly Rent]*(Date.DaysInMonth([Move In Date])-Date.Day([Move In Date])+1)/Date.DaysInMonth([Move In Date]),2),type number)

Result: A new First Month Rent column shows 800, 750, 564.52, 450, and 990.

New First Month Rent column with the prorated rent for the move-in month

Here, Date.DaysInMonth([Move In Date])-Date.Day([Move In Date])+1 counts the days the tenant lives there in the first month. The Date.Day function gives the day number of the move-in date, and the +1 counts the move-in day itself.

The formula then multiplies the monthly rent by those days and divides by the days in the month. Number.Round keeps the answer to two decimal places, like a currency amount.

Benjamin moves in on 4/11/2026, so he stays 20 of April’s 30 days and pays 1200 × 20/30 = 800. Jill moves in on 3/18/2026, so she pays for 14 of March’s 31 days, which gives 564.52.

Hailey moves in on 8/1/2026, the first of the month, so she pays the full 990.

Example 8: Flag Dates on the Last Day of the Month

Some things only happen on the last day of a month, like a month-end shipment cutoff.

Below is a Shipments table with the date and time each shipment went out, and I want to check whether each one was shipped on the last day of its month, in a new Is Last Day column.

Shipments table with Shipment, Driver and Shipped At date and time

Load the table with Data > From Table/Range, and make sure the Changed Type step sets the Shipped At column to Date/Time. This time the time of each shipment is part of the data, so I’m keeping it.

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.AddColumn(#"Changed Type","Is Last Day",each Date.Day([Shipped At])=Date.DaysInMonth([Shipped At]),type logical)

Result: A new Is Last Day column shows TRUE, FALSE, TRUE, TRUE, FALSE, and TRUE.

New Is Last Day column showing TRUE for shipments on the last day of the month

Here, Date.Day gives the day number of each shipment, and Date.DaysInMonth gives the number of days in that month. On the last day of a month, the two numbers are the same, so you get TRUE.

Cynthia’s shipment on 2/28/2026 gets TRUE because February 2026 has 28 days. Rebecca’s shipment on 2/27/2026 and Veronica’s on 5/30/2026 get FALSE, because each is one day short of the month end.

Because the check compares only day numbers, the shipment time doesn’t get in the way.

A check like [Shipped At]=Date.EndOfMonth([Shipped At]) would give FALSE on every row here. On a Date/Time value, the Date.EndOfMonth function returns 11:59:59 PM on the last day, and no shipment went out at exactly that time.

Example 9: List the Number of Days in Every Month of a Year

This last example builds the days for all twelve months of a year, which is handy for a calendar or a monthly planning table.

Suppose you want the number of days in each month of 2026.

Create a new blank query (as in Example 1) and use this formula:

= List.Transform({1..12},each Date.DaysInMonth(#date(2026,_,1)))

Result: A list of 12 numbers: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, and 31.

A list of the number of days in each month of 2026

Here, {1..12} is a list of the month numbers 1 to 12. The List.Transform function runs the formula on each number, where _ stands for the month number being used.

So for each month, #date(2026,_,1) creates the first day of that month, and Date.DaysInMonth returns how many days it has. February shows 28, because 2026 isn’t a leap year.

If you add up the list with the List.Sum function, you get 365, the number of days in 2026. To turn the list into a table you can load to a sheet, click To Table on the List Tools Transform tab.

Other Excel Articles You May Also Like:

All Power Query Functions

Sumit Bansal

Sumit Bansal

13x Microsoft Excel MVP

Hey! I'm Sumit Bansal, founder of trumpexcel.com and a Microsoft Excel MVP. I started this site in 2013 because I genuinely love Microsoft Excel (yes, really!) and wanted to share that passion through easy Excel tutorials, tips, and Excel training videos. My goal is straightforward: help you master Excel skills so you can work smarter, boost productivity, and maybe even enjoy spreadsheets along the way!

Get the FREE 51 Excel Tips Ebook

Enter your details and the free PDF is on its way to your inbox.

Hmm, that didn't go through. Please check your email and try again.

No spam. You'll also get my weekly Excel newsletter. Unsubscribe anytime.

Check your inbox!

The ebook is on its way to your email. It usually lands within a couple of minutes.