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

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

Reply
qwertyh
Frequent Visitor

Adding in a Date Column based on Start Date & End Date

Hello,

I am trying to add in an inforce date column which will be the date that drives the calculations for this table.  This will expand the table duplicating each row for each unique month-year that falls between the start and end date inclusively and always be the 1st of each month.

For example, with this table,

Policy IDStart DateEnd DateOthers Column etc..
ABC5231/5/20201/5/2021 
BAT3252/1/20215/20/2022 
CAT5426/3/20196/3/2020 

I would like to have this:

Policy IDStart DateEnd DateInforce DateOthers Column etc..
ABC5231/5/20201/5/20211/1/2020 
ABC5231/5/20201/5/20212/1/2020 
ABC5231/5/20201/5/20213/1/2020 
ABC5231/5/20201/5/20214/1/2020 
ABC5231/5/20201/5/20215/1/2020 
ABC5231/5/20201/5/20216/1/2020 
ABC5231/5/20201/5/20217/1/2020 
ABC5231/5/20201/5/20218/1/2020 
ABC5231/5/20201/5/20219/1/2020 
ABC5231/5/20201/5/202110/1/2020 
ABC5231/5/20201/5/202111/1/2020 
ABC5231/5/20201/5/202112/1/2020 
ABC5231/1/20201/1/20211/1/2021 
BAT3252/1/20215/20/20222/1/2021 
BAT3252/1/20215/20/20223/1/2021 

etc..

Thanks for the help.

1 ACCEPTED SOLUTION
Jakinta
Solution Sage
Solution Sage

You can try this...

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRyNjUyVtJRMtQ31TcyMDJAMA2VYnWilZwcQ4yNTIGiRvqGEFEdJZA0iG0EVuHsGGJqYgQUNtM3BooaWsKZQNNiYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Policy ID" = _t, #"Start Date" = _t, #"End Date" = _t]),
    DateType = Table.TransformColumnTypes(Source,{{"Start Date", type date}, {"End Date", type date}}),
    DateList = Table.AddColumn(DateType, "Inforce Date", each List.Distinct(List.Transform({Number.From([Start Date]) .. Number.From([End Date])}, each Date.StartOfMonth(Date.From(_))))),
    FINAL = Table.ExpandListColumn(DateList, "Inforce Date")
in
    FINAL

View solution in original post

4 REPLIES 4
edhans
Super User
Super User

Try this @qwertyh 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRyNjUyVtJRMtQ31TcyMDJAMA2VYnWilZwcQ4yNTIGiRvqGEFEdJZA0iG0EVuHsGGJqYgQUNtM3BooaWsKZQNNiYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Policy ID" = _t, #"Start Date" = _t, #"End Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Start Date", type date}, {"End Date", type date}}),
    #"Added Date List" = 
        Table.AddColumn(
            #"Changed Type", 
            "Date List", 
            each 
                let
                    varStartDate = Date.StartOfMonth([Start Date]),
                    varEndDate = Date.StartOfMonth([End Date]) + #duration(1,0,0,0),
                    varDates = List.Dates(varStartDate, Duration.TotalDays(varEndDate - varStartDate), #duration(1,0,0,0))
                in
                    List.Select(varDates, each _ = Date.StartOfMonth(_))
        ),
    #"Expanded Date List" = Table.ExpandListColumn(#"Added Date List", "Date List")
in
    #"Expanded Date List"

 

It does this:

  1. Gets your Start Date and finds the first day of the nonth.
  2. Gets your End Date and finds the first day of that month, then adds 1 day.
  3. Creates a list of all dates in between.
  4. Keeps only those dates in that are the start of the month.
  5. Expands them to a new row.

Here is some of the data:

edhans_1-1628551909530.png

 

How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
Jakinta
Solution Sage
Solution Sage

You can try this...

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRyNjUyVtJRMtQ31TcyMDJAMA2VYnWilZwcQ4yNTIGiRvqGEFEdJZA0iG0EVuHsGGJqYgQUNtM3BooaWsKZQNNiYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Policy ID" = _t, #"Start Date" = _t, #"End Date" = _t]),
    DateType = Table.TransformColumnTypes(Source,{{"Start Date", type date}, {"End Date", type date}}),
    DateList = Table.AddColumn(DateType, "Inforce Date", each List.Distinct(List.Transform({Number.From([Start Date]) .. Number.From([End Date])}, each Date.StartOfMonth(Date.From(_))))),
    FINAL = Table.ExpandListColumn(DateList, "Inforce Date")
in
    FINAL

Both this response & Ed Han's response solve my question. I chose this because of it's simplicity and beauty. EdHan's response is much easier to follow and I love the instruction and explanation given to help understand the code.

Thank you both!

Great @qwertyh - glad you got a solution that works. 

For future reference, you can mark multiple responses as "the solution" to a thread should you find that more than one response works.

I tend to go for a bit more verbose and formatted code, which takes up a few more lines, but easier for me to walk through. 😁



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Helpful resources

Announcements
October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.

Top Kudoed Authors