Forum Discussion

SebaSpotti's avatar
SebaSpotti
Icon for Advocate II rankAdvocate II
1 year ago
Solved

Unpivot a table with dates beetwen

Hi all!

 

I have a table like this :

Start_DateEnd_DateType_of_CarNumber
20/10/202425/10/2024Car A2
08/02/202412/02/2024Car B3

 

and I want to obtain (with power query or dax) a table like this:

 

DateType_of_CarNumber
20/10/2024Car A2
21/10/2024Car A2
22/10/2024Car A2
23/10/2024Car A2
24/10/2024Car A2
25/10/2024Car A2
08/02/2024Car B3
09/02/2024Car B3
10/02/2024Car B3
11/02/2024Car B3
12/02/2024Car B3

 

any suggestions?!

 

Thank you all!

3 Replies

  • hi SebaSpotti 

    You don't really unpivot but expand the start and end dates into rows of dates within the range. The custom column below will generate a list of dates whicn can be expanded into rows.

    let 
    //generate a series of numbers from the start and end dates as a list
    series = {Number.From([Start_Date])..Number.From([End_Date])},
    //convert the numbers back to dates
    todates = List.Transform(series, Date.From)
    in 
    todates

     

     

  • Hi SebaSpotti ,

    Make sure that your Start_Date and End_Date the data type was set to date in power query.

    After that, you can add a new custom column using this M code:

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



    Expand the Date List:

    • Click the small icon with arrows at the top-right of the Date_List  column.
    • Select Expand to New Rows. This will create a row for each date in the range.

     

    Now your data look like this:

     

    Rename the new column to Date.

    Remove the original Start_Date and End_Date columns if they are no longer needed.

     

    Your final result should look like this: