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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
CSRiotech
Frequent Visitor

Split rows based on start & end date

Suppose I have following data: 

ProductStart DateEnd Date
131 October 202125 December 2021
231 October 202110 January 2022
321 October 202115 February 2023

 

But I want to split the rows in which time period between 'Start Date' & 'End Date' spans more than one year(They are recognised Date variables). The table above would then be changed to:

 

ProductStart Date End Date
131 October 202125 December 2021
231 October 202131 December 2021
21 January 202210 January 2022
321 October 202131 December 2021
31 January 202231 December 2022
31 January 202315 February 2023

 

How can I do this in powerBI? Any help would be much appriciated! 

Best regards

1 ACCEPTED SOLUTION
AntrikshSharma
Super User
Super User

@CSRiotech You can use this:

 

let
    Source = Table.FromRows (
        Json.Document (
            Binary.Decompress (
                Binary.FromText (
                    "i45WMlTSUTI2VPBPLslPSi1SMDIwAokYmSq4pCan5sKFYnWilYywKjU0UPBKzCtNLKoEiRiBVRqDjMBUaarglppUBFNqrBQbCwA=",
                    BinaryEncoding.Base64
                ),
                Compression.Deflate
            )
        ),
        let
            _t = ( ( type nullable text ) meta [ Serialized.Text = true ] )
        in
            type table [ Product = _t, #"Start Date" = _t, #"End Date" = _t ]
    ),
    ChangedType = Table.TransformColumnTypes (
        Source,
        { { "Product", Int64.Type }, { "Start Date", type date }, { "End Date", type date } }
    ),
    Result = Table.AddColumn (
        Source,
        "Repeat",
        ( CurrentRow ) =>
            let
                YearCount = 
                    Date.Year ( CurrentRow[End Date] )
                        - Date.Year ( CurrentRow[Start Date] )
                        + 1,
                GenerateDates = 
                    List.Generate (
                        () =>  [
                            Start = CurrentRow[Start Date],
                            End   = List.Min ( { CurrentRow[End Date], Date.EndOfYear ( Start ) } ),
                            Stop  = CurrentRow[End Date]
                        ],
                        each [Start] <= [Stop],
                        each [
                            Start = Date.StartOfYear ( Date.AddYears ( [Start], 1 ) ),
                            End   = List.Min ( { [Stop], Date.EndOfYear ( Start ) } ),
                            Stop  = [Stop]
                        ],
                        each { CurrentRow[Course], [Start], [End]  }
                    ),
                Result = 
                    Table.FromRows ( 
                        GenerateDates, 
                        type table [Course = text, Start Date = date, End Date = date] 
                    )
            in
                Result,
        type table
    ),
    Combined = Table.Combine ( Result[Repeat] )
in
    Combined

 

 

 

View solution in original post

1 REPLY 1
AntrikshSharma
Super User
Super User

@CSRiotech You can use this:

 

let
    Source = Table.FromRows (
        Json.Document (
            Binary.Decompress (
                Binary.FromText (
                    "i45WMlTSUTI2VPBPLslPSi1SMDIwAokYmSq4pCan5sKFYnWilYywKjU0UPBKzCtNLKoEiRiBVRqDjMBUaarglppUBFNqrBQbCwA=",
                    BinaryEncoding.Base64
                ),
                Compression.Deflate
            )
        ),
        let
            _t = ( ( type nullable text ) meta [ Serialized.Text = true ] )
        in
            type table [ Product = _t, #"Start Date" = _t, #"End Date" = _t ]
    ),
    ChangedType = Table.TransformColumnTypes (
        Source,
        { { "Product", Int64.Type }, { "Start Date", type date }, { "End Date", type date } }
    ),
    Result = Table.AddColumn (
        Source,
        "Repeat",
        ( CurrentRow ) =>
            let
                YearCount = 
                    Date.Year ( CurrentRow[End Date] )
                        - Date.Year ( CurrentRow[Start Date] )
                        + 1,
                GenerateDates = 
                    List.Generate (
                        () =>  [
                            Start = CurrentRow[Start Date],
                            End   = List.Min ( { CurrentRow[End Date], Date.EndOfYear ( Start ) } ),
                            Stop  = CurrentRow[End Date]
                        ],
                        each [Start] <= [Stop],
                        each [
                            Start = Date.StartOfYear ( Date.AddYears ( [Start], 1 ) ),
                            End   = List.Min ( { [Stop], Date.EndOfYear ( Start ) } ),
                            Stop  = [Stop]
                        ],
                        each { CurrentRow[Course], [Start], [End]  }
                    ),
                Result = 
                    Table.FromRows ( 
                        GenerateDates, 
                        type table [Course = text, Start Date = date, End Date = date] 
                    )
            in
                Result,
        type table
    ),
    Combined = Table.Combine ( Result[Repeat] )
in
    Combined

 

 

 

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.