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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

Turning dates that span multiple months into multiple records

Hi,

 

I have some data that shows the start and end date for a process, but I was wondering if there was a way to split these dates into monthly intervals. So I could get a line for each month.

 

Like with the sample data below:

 

Unique IDStart DateEnd Date 
AA2020-05-052020-06-05
AB2020-04-032020-06-19

AC

2020-05-15

2020-05-25

 

Would turn into:

 

Unique IDStart DateEnd Date 
AA2020-05-052020-05-31
AA2020-06-012020-06-05
AB2020-04-032020-04-30
AB2020-05-012020-05-31
AB2020-06-012020-06-19
AC2020-05-152020-05-25

 

I was wondering if there is an easy eay to do this in Power Query or DAX? I want to try avoiding doing this in Excel or VBA.

1 ACCEPTED SOLUTION

Thanks, @ImkeF  for highlighting it, in fact, I have learned a lot from you.
I somehow missed verifying it thoroughly.

I created this function to bring the date range and it works fine. I actually blogged recently https://excelfort.com/allocate-amount-monthly-excel-powerquery/

let
    Source = (pStart as date, pEnd as date) =>
let

Source = List.Generate(
                ()=> [fDate = pStart, fDay= if Date.EndOfMonth(fDate) < pEnd then Date.EndOfMonth(fDate) else pEnd ],      
                each [fDate] <= pEnd,
                each 
                let EoM = Date.EndOfMonth( Date.AddMonths([fDate],1)) in 
                [fDate= Date.AddDays(Date.EndOfMonth([fDate]),1) , fDay= if EoM > pEnd then pEnd else Date.EndOfMonth(fDate)]       
        ),
        #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"fDate", "fDay"}, {"fDate", "fDay"}),
        #"Changed Type" = Table.TransformColumnTypes(#"Expanded Column1",{{"fDate", type date}, {"fDay", type date}}),
        #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"fDate", "Date Start"}, {"fDay", "Date End"}})
    in
        #"Renamed Columns"
in

Source

 
Thanks again

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

View solution in original post

4 REPLIES 4
Fowmy
Super User
Super User

@Anonymous 

Please find attached the file which splits the date to ranges as you required.

You can download the file: HERE


I created a function to provide start and end date then it will get you the range in between, then it is a matter of expanding into rows.
More on date splitting in my blog: https://excelfort.com/allocate-amount-monthly-excel-powerquery/

 

let
    Source = (pStart as date, pEnd as date) =>
let

Source = List.Generate(
                ()=> [fDate = pStart, fDay= if Date.EndOfMonth(fDate) < pEnd then Date.EndOfMonth(fDate) else pEnd ],      
                each [fDate] <= pEnd,
                each 
                let EoM = Date.EndOfMonth( Date.AddMonths([fDate],1)) in 
                [fDate= Date.AddDays(Date.EndOfMonth([fDate]),1) , fDay= if EoM > pEnd then pEnd else Date.EndOfMonth(fDate)]       
        ),
        #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"fDate", "fDay"}, {"fDate", "fDay"}),
        #"Changed Type" = Table.TransformColumnTypes(#"Expanded Column1",{{"fDate", type date}, {"fDay", type date}}),
        #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"fDate", "Date Start"}, {"fDay", "Date End"}})
    in
        #"Renamed Columns"
in

Source

 




Fowmy_0-1596091646824.png

 

 

________________________

Did I answer your question? Mark this post as a solution, this will help others!.

Click on the Thumbs-Up icon on the right if you like this reply 🙂

YouTube, LinkedIn

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

ImkeF
Community Champion
Community Champion

Hi @Fowmy 

it doesn't look as if your results equals the request.

 

To @Anonymous ,

I've created a function some time ago that creates these kind of intervals: https://www.thebiccountant.com/2017/12/11/date-datesbetween-retrieve-dates-between-2-dates-power-bi-power-query/

I will just return the end date of each month (or other interval that you need).

But the other columns can be worked out quite easily:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRU0lEyMjAy0DUwBSI4xwzEidUBKnCCi5noGhgjKTC0hChwRjLB0BSJYwQ0IRYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Unique ID" = _t, #"Start Date" = _t, #"End Date " = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Unique ID", type text}, {"Start Date", type date}, {"End Date ", type date}}),
    #"Invoked Custom Function" = Table.AddColumn(#"Changed Type", "fnDatesBetwen", each fnDatesBetwen([Start Date], [#"End Date "], "Month")),
    #"Expanded fnDatesBetwen" = Table.ExpandListColumn(#"Invoked Custom Function", "fnDatesBetwen"),
    AddStartOfMonth = Table.AddColumn(#"Expanded fnDatesBetwen", "StartOfMonth", each Date.StartOfMonth([fnDatesBetwen])),
    #"Inserted Latest" = Table.AddColumn(AddStartOfMonth, "Start", each List.Max({[StartOfMonth], [Start Date]}), type date),
    #"Inserted Earliest" = Table.AddColumn(#"Inserted Latest", "End", each List.Min({[#"End Date "], [fnDatesBetwen]}), type date),
    Cleanup = Table.SelectColumns(#"Inserted Earliest",{"Unique ID", "Start", "End"})
in
    Cleanup

 

Please also find the file attached.

 

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

Thanks, @ImkeF  for highlighting it, in fact, I have learned a lot from you.
I somehow missed verifying it thoroughly.

I created this function to bring the date range and it works fine. I actually blogged recently https://excelfort.com/allocate-amount-monthly-excel-powerquery/

let
    Source = (pStart as date, pEnd as date) =>
let

Source = List.Generate(
                ()=> [fDate = pStart, fDay= if Date.EndOfMonth(fDate) < pEnd then Date.EndOfMonth(fDate) else pEnd ],      
                each [fDate] <= pEnd,
                each 
                let EoM = Date.EndOfMonth( Date.AddMonths([fDate],1)) in 
                [fDate= Date.AddDays(Date.EndOfMonth([fDate]),1) , fDay= if EoM > pEnd then pEnd else Date.EndOfMonth(fDate)]       
        ),
        #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"fDate", "fDay"}, {"fDate", "fDay"}),
        #"Changed Type" = Table.TransformColumnTypes(#"Expanded Column1",{{"fDate", type date}, {"fDay", type date}}),
        #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"fDate", "Date Start"}, {"fDay", "Date End"}})
    in
        #"Renamed Columns"
in

Source

 
Thanks again

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Greg_Deckler
Community Champion
Community Champion

@Anonymous - Sure, you can GENERATESERIES and UNION to do that in DAX. I'm sure there is a way to do it in Power Query too @ImkeF .



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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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 Solution Authors