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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
PauloRicardo
Advocate I
Advocate I

How to Generate a series of dates from a historical table with Power Query

Hi everyone,

 

My trouble is with a historical table that has an alteration date for every branch the employee was transfer to.

 

Basically the objective is to generate dates between every alteration date for each employee so i can merge it with others historical tables.


Alternative 1:

IDALT_DATEBRANCH
37502  01/02/2021  411090
37502  01/07/2021  410062
37502  01/11/2021  495999

 

In the table above, the employee was trasfered to another branch on the ALT_DATE date. Can i generate dates, day to day until the end of the first date to the second? The result must be something like this:

IDALT_DATEBRANCH
37502  01/02/2021  411090
37502  02/02/2021  411090
37502  03/02/2021  411090
37502  ...  411090
37502  29/06/2021  411090
37502  30/06/2021  411090
37502  01/07/2021  410062
37502  02/07/2021  410062
37502  ...  410062
37502  30/10/2021  410062
37502  01/11/2021  495999
37502  02/11/2021  495999
37502  ...  495999
37502  31/12/2099  495999


Alternative 2:

I also have done a variation of this, by having the initial and final date.

 

IDINITIAL_DATEFINAL_DATEBRANCH
37502  01/02/2021  30/06/2021  411090
37502  01/07/2021  30/10/2021  410062
37502  01/11/2021  31/12/2099  495999

 

As i don't know which one is better to get the desired result, so i gave two options.

 

Thanks!

1 ACCEPTED SOLUTION
DataNinja777
Super User
Super User

Hi @PauloRicardo ,

 

If you're starting with Alternative 2, which includes INITIAL_DATE, FINAL_DATE, and BRANCH, that's the better choice for generating a day-by-day expansion in Power Query. You can use Power Query's List.Dates function to create a list of dates for each row between the initial and final date. Then you expand that list into rows. Here's the M code to do that:

let
    Source = YourSourceTableName,
    ChangedTypes = Table.TransformColumnTypes(Source, {
        {"INITIAL_DATE", type date}, 
        {"FINAL_DATE", type date}
    }),
    AddedDateList = Table.AddColumn(ChangedTypes, "Date", each List.Dates(
        [INITIAL_DATE],
        Duration.Days([FINAL_DATE] - [INITIAL_DATE]) + 1,
        #duration(1,0,0,0)
    )),
    ExpandedDates = Table.ExpandListColumn(AddedDateList, "Date"),
    FinalTable = Table.SelectColumns(ExpandedDates, {"ID", "Date", "BRANCH"})
in
    FinalTable

This will give you one row per day for each employee-branch record, making it easy to merge with other historical tables. If you're using the first format with only alteration dates, you'd need to add a calculated next date using the next row per employee, which is more complex. Stick with the second format if you can—it’s cleaner, more reliable, and requires fewer steps.

 

Best regards,

View solution in original post

2 REPLIES 2
johnt75
Super User
Super User

Using the 2nd alternative, with both dates in the same row, you can add a custom column like

List.Dates( [InitialDate], Duration.Days([FinalDate] - [InitialDate]) + 1, #duration(1, 0, 0, 0 ) )

You can then expand the list into new rows

DataNinja777
Super User
Super User

Hi @PauloRicardo ,

 

If you're starting with Alternative 2, which includes INITIAL_DATE, FINAL_DATE, and BRANCH, that's the better choice for generating a day-by-day expansion in Power Query. You can use Power Query's List.Dates function to create a list of dates for each row between the initial and final date. Then you expand that list into rows. Here's the M code to do that:

let
    Source = YourSourceTableName,
    ChangedTypes = Table.TransformColumnTypes(Source, {
        {"INITIAL_DATE", type date}, 
        {"FINAL_DATE", type date}
    }),
    AddedDateList = Table.AddColumn(ChangedTypes, "Date", each List.Dates(
        [INITIAL_DATE],
        Duration.Days([FINAL_DATE] - [INITIAL_DATE]) + 1,
        #duration(1,0,0,0)
    )),
    ExpandedDates = Table.ExpandListColumn(AddedDateList, "Date"),
    FinalTable = Table.SelectColumns(ExpandedDates, {"ID", "Date", "BRANCH"})
in
    FinalTable

This will give you one row per day for each employee-branch record, making it easy to merge with other historical tables. If you're using the first format with only alteration dates, you'd need to add a calculated next date using the next row per employee, which is more complex. Stick with the second format if you can—it’s cleaner, more reliable, and requires fewer steps.

 

Best regards,

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors