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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

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
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.