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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
mzrkwcz
Helper I
Helper I

Generating a list of months problem

I need to generate a list of dates having:

- [start_date]

- [number_of_months]

 

I tested that this works fine:

List.Transform({0..[number_of_months]-1}, each Date.AddMonths(DateFrom("2017-05-05"), _))

And this works fine:

Date.AddMonths([invoices_start],_)

However this does not work:

List.Transform({0..[number_of_months]-1}, each Date.AddMonths([invoices_start],_))

I get the message:

Expression.Error: We cannot apply field access to the type Number.

 

What's the problem??? It's driving me crazy.

1 ACCEPTED SOLUTION
MarcelBeug
Community Champion
Community Champion

Out of my head (so not tested), I'm pretty sure this should work:

 

List.Transform({0..[number_of_months]-1}, (x) => Date.AddMonths([invoices_start],x))

 

By using (x) => instead of each, you can refer to the list item (i.e. the number) within the Date.AddMonths function.

Specializing in Power Query Formula Language (M)

View solution in original post

3 REPLIES 3
prathy
Advocate III
Advocate III

Hi,

 

Can you try casting _ as number
something like this - List.Transform({1, 2}, each Date.AddMonths(Date.From("10/02/2017"), _ as number))

 

P

MarcelBeug
Community Champion
Community Champion

Out of my head (so not tested), I'm pretty sure this should work:

 

List.Transform({0..[number_of_months]-1}, (x) => Date.AddMonths([invoices_start],x))

 

By using (x) => instead of each, you can refer to the list item (i.e. the number) within the Date.AddMonths function.

Specializing in Power Query Formula Language (M)

It works! Thanks!

 

I have not seen (x) => f(x) in Power Query M Reference on MSDN, but probably I did not dig deep enough 😕

 

I still do not understand why solution with "each" works when I am increasing a constant date and does not work with the use of another field to deliver the date to be incremented.

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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