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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
rsbin
Super User
Super User

Create Column as List of Values using M

Hello,

Am new to Power Query so looking for a guiding hand.

I have a table with Contract Start Date and  Contract End Date.  I have converted these dates to text values YYYYMM (but not sure if this was the right thing to do).

I have a Duration (months) that I have calculated.

Record No.  Duration  Start YearMonth  End YearMonth

129202003202011
2712202004202103
9715201910202012

 

I believe what I need next is to generate a Column with a list of values so for each Record ID I have the entire list of applicable YYYYMM values.  In the end, I am trying to calculate a monthly revenue stream for each contract no.

Any help or guidance would be very much appreciated.

Kind Regards,

1 ACCEPTED SOLUTION
ImkeF
Community Champion
Community Champion

Hi @rsbin 

you're nearly there!

 

You have to make an adjustment in the red area:

 

= Table.AddColumn(Source, "ListofDates", each Date.DatesBetween([Contract Start Date],[Contract Expiration Date],"Month"))

 

That has to match the name of the query/function where you've posted my code. "." will not be allowed in query names, so it must be something else 🙂

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

7 REPLIES 7
Greg_Deckler
Super User
Super User

Not entirely certain what you are trying to ultimately accomplish. Sometimes it is good to just show your source data and what you are trying to achieve and let people get creative. However, it kind of sounds like you are dealing with date intervals (start and end dates) and need to extrapolate what is in the middle. See if these two posts help. They are DAX but both were designed to deal with such situations.

 

https://community.powerbi.com/t5/Quick-Measures-Gallery/Open-Tickets/m-p/409364#M147

 

https://community.powerbi.com/t5/Quick-Measures-Gallery/Periodic-Revenue-Reverse-YTD/m-p/373185#M111



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Hi @rsbin ,

you can use this function to create the months between your date fields: https://www.thebiccountant.com/2017/12/11/date-datesbetween-retrieve-dates-between-2-dates-power-bi-... 

 

Please check out this video on how to use such a function code in Power Query: 

Reuse M code (M functions) created by others easily 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hello @ImkeF ,

After watching Curbal's video link, I copied and pasted your raw function code into a new blank query.  I invoked the function to test it and it works just fine.

As a newbie to this community, I haven't yet figured out how to share my pbix file so will try to describe best I can

I created a new query with the following syntax:

=Table.SelectColumns(Import_Recur,{"Record No.","Project Amount","Contract Start Date","Contract Expiration Date"}) and generated a small table with 9 records.  Sample as follows:

Record No.Project AmountContract Start DateContract Expiration Date

121015203/1/202011/30/2020
272000004/1/20203/31/2021
7610869811/1/202010/31/2025
7718596011/1/202010/31/2025
9712500010/1/201912/31/2020

I then followed with the code from the blog post that I think is what I need to use.

= Table.AddColumn(Source, "ListofDates", each Date.DatesBetween([Contract Start Date],[Contract Expiration Date],"Month"))

But I get the following error message.

Expression Error: The name 'Date.DatesBetween wasn't recognized.  Make sure it is spelled correctly.

I must be missing a step somewhere, but am stuck as to where I turn to next.  Not sure if it has to do with my Column Names or placing the function code in the right place.

 

Based on invoking the function, this is exactly the solution I am looking for.  I just need some guidance on how to get it to execute on my data table.

Thank you so much for your help.

 
 

 

ImkeF
Community Champion
Community Champion

Hi @rsbin 

you're nearly there!

 

You have to make an adjustment in the red area:

 

= Table.AddColumn(Source, "ListofDates", each Date.DatesBetween([Contract Start Date],[Contract Expiration Date],"Month"))

 

That has to match the name of the query/function where you've posted my code. "." will not be allowed in query names, so it must be something else 🙂

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

@ImkeF 

Worked!   Thank you so very much!!! 

ImkeF
Community Champion
Community Champion

BTW: Yesterday I've published an instruction on how to provide sample data here: https://community.powerbi.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-... 

 

So if you cannot make it work, just post the link to your file according to the instructions above.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Thank you both for the replies.  I will study them over the weekend and let you know

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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