Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Kassie72
Frequent Visitor

Transform start and end date to a list of months to be expanded

Hi,

I would like to get a list of months between start and end date on a row and expand it with the number months in between.

 

I am using the following table sample:

EmployeeStartDateEndDate
John01/01/202313/12/2024
Theresa15/03/202431/12/2025


All days within the start date are considered to count for a full month. So I have used a formula to determine the first day of the month: Date.StartOfMonth([StartDate]). This works fine.

Then I have made a calculation of the months in between the StartDate and the EndDate: MonthsInBetween
(Date.Year([EndDate]) - Date.Year([StartDate])) * 12 + (Date.Month([EndDate]) - Date.Month([StartDate])). This works fine.

Now I would like to make a list to expand the StartDate with the MonthsInBetween. This is what does not work.
List.Transform(
List.Numbers(0, [MonthsInBetween] + 1),
each Date.AddMonths(Date.StartOfMonth([StartDate]), _)
)

Generating the values seem to work fine (a green line below the column header), but when I expand I get Errors in the field. When double clicking on the error message, I get the following:
Expression.Error: Cannot apply field access to the type Number.
Details:
Value=0
Key=StartDate

 

Can someone help me out here?

1 ACCEPTED SOLUTION
slorin
Super User
Super User

Hi @Kassie72 

 

(x) => List.Transform(
List.Numbers(0, x[MonthsInBetween] + 1),
each Date.AddMonths(Date.StartOfMonth(x[StartDate]), _)
)

or

List.Transform(
List.Numbers(0, [MonthsInBetween] + 1),
(x) => Date.AddMonths(Date.StartOfMonth([StartDate]), x)

 

Stéphane

View solution in original post

4 REPLIES 4
SundarRaj
Super User
Super User

Hi @Kassie72 , here's a solution you could take a look at. Thanks

SundarRaj_0-1742486125761.png

SundarRaj_1-1742486145612.png

 

 

Sundar Rajagopalan
slorin
Super User
Super User

https://learn.microsoft.com/en-us/powerquery-m/m-spec-functions#simplified-declarations

each _ + 1 
<=>
(_) => _ + 1


each [A]
<=>
(_) => _[A]

then

each 
List.Transform(

List.Numbers(0, [MonthsInBetween] + 1),
each Date.AddMonths(Date.StartOfMonth([StartDate]), _)
)

is equivalent to

 

(_) => 
List.Transform(

List.Numbers(0, _[MonthsInBetween] + 1),
(_) => Date.AddMonths(Date.StartOfMonth(_[StartDate]), _)
)

You must distinguish between two _ 

 

Stéphane

 

 

 

Kassie72
Frequent Visitor

Yes it works, thanks.
Could you briefly elaborate the diffence between your and my solution?

slorin
Super User
Super User

Hi @Kassie72 

 

(x) => List.Transform(
List.Numbers(0, x[MonthsInBetween] + 1),
each Date.AddMonths(Date.StartOfMonth(x[StartDate]), _)
)

or

List.Transform(
List.Numbers(0, [MonthsInBetween] + 1),
(x) => Date.AddMonths(Date.StartOfMonth([StartDate]), x)

 

Stéphane

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors