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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
eng_123
Frequent Visitor

Create start date and end date from date

Hi All please let me know if you we can create a start date and end date from a single column Date

 

Existing Data:

 

PhaseDate
Phase1 15/10/2024
Phase1 16/10/2024
Phase1 17/10/2024
Phase 2 16/10/2024
Phase 2 17/10/2024
Phase1 31/10/2024
Phase1 1/11/2024
Phase1 2/11/2024
Phase 2 22/10/2024
Phase 2 23/10/2024

 

 

Create a new Dax calculation for start date and end date

start date - Is the start of continuous date from Date column

end date - is the end of continuous date from Date column

Example:

 

 

PhaseStart DateEnd Date
Phase 1 15/10/2024 17/10/2024
Phase 1 31/10/2024 2/11/2024
Phase 2 16/10/2024 17/10/2024
Phase 2 22/10/2024 23/10/2024
4 ACCEPTED SOLUTIONS
tharunkumarRTK
Super User
Super User

@eng_123 

 

I think implementing this solution in power bi would be a much easier, please find the PBIX file attached.

Screenshot 2024-10-13 at 12.17.22 PM.png

 

Need a Power BI Consultation? Hire me on Upwork

 

 

 

Connect on LinkedIn

 

 

 








Did I answer your question? Mark my post as a solution!
If I helped you, click on the Thumbs Up to give Kudos.

Proud to be a Super User!


PBI_SuperUser_Rank@2x.png

View solution in original post

ValtteriN
Super User
Super User

Hi,

Here is one way to do this. Due to the table not containing ids this took a bit of trial and error:

EVALUATE
VAR _vtable1 =
    ADDCOLUMNS(
        'Table (44)',
        "PreviousDate",
        VAR _phase = [Phase]
        VAR _date = [Date]
        VAR _previous =
            CALCULATE(
                MAX('Table (44)'[Date]),
                FILTER(
                    'Table (44)',
                    [Phase] = _phase && 'Table (44)'[Date] < _date
                )
            )
        RETURN IF(_previous = _date - 1, _previous, BLANK())
    )

-- Compute the start date for each continuous block
VAR _startdate =
    ADDCOLUMNS(
        _vtable1,
        "StartDate",
        IF(
            ISBLANK([PreviousDate]),
            [Date],
            BLANK()
        )
    )

-- Identify the end date by checking for gaps in the next date
VAR _enddate =
    ADDCOLUMNS(
        _startdate,
        "EndDate",
        VAR _phase = [Phase]
        VAR _date = [Date]
        VAR _next =
            CALCULATE(
                MIN('Table (44)'[Date]),
                FILTER(
                    'Table (44)',
                    [Phase] = _phase && 'Table (44)'[Date] > _date
                )
            )
        RETURN IF(_next <> _date + 1 || ISBLANK(_next), _date, BLANK())
    )

-- Summarize the result to get start and end dates for each continuous range
VAR _result =
    VAR _vtable2 = GROUPBY(_enddate, [Phase], [StartDate], [EndDate])
    RETURN
        ADDCOLUMNS(
            _vtable2,
            "edate",
            VAR _phase = [Phase]
            VAR calculatedEdate =
            var _sdate = [StartDate] RETURN
                CALCULATE(
                    MINX(FILTER(_vtable2, [Phase] = _phase && [EndDate]>_sdate), [EndDate])
                )
            RETURN
                IF(
                    calculatedEdate < [EndDate],
                    [EndDate],  -- Return the original EndDate if calculatedEdate is smaller
                    calculatedEdate  -- Otherwise, return calculatedEdate
                )
        )
       

   
RETURN
GROUPBY(FILTER(_result,NOT(ISBLANK([StartDate]))),[Phase],[StartDate],[edate])




End result shown in query view:

ValtteriN_0-1728802634267.png




I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!

My LinkedIn: https://www.linkedin.com/in/n%C3%A4ttiahov-00001/








Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

Jihwan_Kim
Super User
Super User

Hi, 

If your expected result is to have a new table, please check the below picture and the attached pbix file.

 

Jihwan_Kim_0-1728803085189.png

 

 

expected result table = 
	VAR _t = ADDCOLUMNS(
		Data,
		"@prevdate", MAXX(
			OFFSET(
				-1,
				Data,
				ORDERBY(
					Data[Date],
					ASC
				),
				,
				PARTITIONBY(Data[Phase]),
				MATCHBY(
					Data[Phase],
					Data[Date]
				)
			),
			Data[Date]
		)
	)
	VAR _condition = ADDCOLUMNS(
		_t,
		"@condition", IF(
			INT(Data[Date] - [@prevdate]) = 1,
			0,
			1
		)
	)
	VAR _group = ADDCOLUMNS(
		_condition,
		"@group", SUMX(
			WINDOW(
				1,
				ABS,
				0,
				REL,
				_condition,
				ORDERBY(
					Data[Date],
					ASC
				),
				,
				PARTITIONBY(Data[Phase]),
				MATCHBY(
					Data[Phase],
					Data[Date]
				)
			),
			[@condition]
		)
	)
	VAR _result = ADDCOLUMNS(
		SUMMARIZE(
			_group,
			Data[Phase],
			Data[Date],
			[@group]
		),
		"StartDate", MINX(
			FILTER(
				_group,
				Data[Phase] = EARLIER(Data[Phase]) && [@group] = EARLIER([@group])
			),
			Data[Date]
		),
		"EndDate", MAXX(
			FILTER(
				_group,
				Data[Phase] = EARLIER(Data[Phase]) && [@group] = EARLIER([@group])
			),
			Data[Date]
		)
	)

	RETURN
		SUMMARIZE(
			_result,
			Data[Phase],
			[StartDate],
			[EndDate]
		)

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

View solution in original post

ThxAlot
Super User
Super User

Whether in DAX or PQ, either is easy.

 

ThxAlot_0-1728848419333.png

 

let
    Source = DATA,
    #"Sorted Rows" = Table.Sort(Source,{{"Date", Order.Ascending}}),
    #"Grouped per Phase" = Table.Group(#"Sorted Rows", "Phase", {"grp per Phase", each Table.Group(Table.AddIndexColumn(_, "Index"), {"Date","Index"}, {"grp per Date", each _}, 0, (x,y) => Byte.From(Duration.TotalDays(y[Date]-x[Date])<>y[Index]-x[Index]))}),
    #"Expanded grp per Phase" = Table.ExpandTableColumn(#"Grouped per Phase", "grp per Phase", {"grp per Date"}),
    #"Transformed Columns" = Table.TransformColumns(#"Expanded grp per Phase", {"grp per Date", each [Start = [Date]{0}, End = List.Last([Date])]}),
    #"Expanded grp per Date" = Table.ExpandRecordColumn(#"Transformed Columns", "grp per Date", {"Start", "End"})
in
    #"Expanded grp per Date"

 

ThxAlot_0-1728848559079.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LeanAndPractise(Everyday)


)



View solution in original post

5 REPLIES 5
ThxAlot
Super User
Super User

Whether in DAX or PQ, either is easy.

 

ThxAlot_0-1728848419333.png

 

let
    Source = DATA,
    #"Sorted Rows" = Table.Sort(Source,{{"Date", Order.Ascending}}),
    #"Grouped per Phase" = Table.Group(#"Sorted Rows", "Phase", {"grp per Phase", each Table.Group(Table.AddIndexColumn(_, "Index"), {"Date","Index"}, {"grp per Date", each _}, 0, (x,y) => Byte.From(Duration.TotalDays(y[Date]-x[Date])<>y[Index]-x[Index]))}),
    #"Expanded grp per Phase" = Table.ExpandTableColumn(#"Grouped per Phase", "grp per Phase", {"grp per Date"}),
    #"Transformed Columns" = Table.TransformColumns(#"Expanded grp per Phase", {"grp per Date", each [Start = [Date]{0}, End = List.Last([Date])]}),
    #"Expanded grp per Date" = Table.ExpandRecordColumn(#"Transformed Columns", "grp per Date", {"Start", "End"})
in
    #"Expanded grp per Date"

 

ThxAlot_0-1728848559079.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LeanAndPractise(Everyday)


)



DataNinja777
Super User
Super User

Hi @eng_123 ,

 

You've already been provided with several solutions, and here’s another one. Since the DAX engine doesn't inherently preserve the original data order, I used Power Query (M) to lock in the data’s original sequence.

let
    // Reference the user's existing query/table (Replace "YourTableName" with the actual table/query name)
    Source = YourTableName,

       // Change the types of columns
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Phase", type text}, {"Date", type date}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Phase] <> "Phase")),

    // Add a Group Number based on Phase changes using List.Accumulate
    GroupedWithPhaseChange = List.Accumulate(
        Table.ToRecords(#"Filtered Rows"), 
        { [GroupNumber = 1, PreviousPhase = null, Rows = {}] }, 
        (state, current) => 
            let 
                currentGroup = if state{0}[PreviousPhase] <> null and current[Phase] <> state{0}[PreviousPhase] 
                    then state{0}[GroupNumber] + 1 
                    else state{0}[GroupNumber],
                newRow = Record.AddField(current, "PhaseGroupIndex", currentGroup),
                updatedState = { 
                    [GroupNumber = currentGroup, PreviousPhase = current[Phase], Rows = List.Combine({state{0}[Rows], {newRow}})]
                }
            in 
                updatedState
    ){0}[Rows],

    // Convert the list of records back to a table, respecting the original order
    #"Grouped Table" = Table.FromRecords(GroupedWithPhaseChange)
    
in
    #"Grouped Table"

The formula above will preserve the order in the original data, as shown below:

DataNinja777_0-1728810996445.png

You can then summarize the OriginalTable by using the following DAX table formula:

Table = SUMMARIZE('OriginalTable',OriginalTable[Phase],OriginalTable[Start Date],OriginalTable[End Date])

This will generate the output shown below:

DataNinja777_1-1728811148343.png

I have attached an example pbix file.

 

Best regards,

 

 

Jihwan_Kim
Super User
Super User

Hi, 

If your expected result is to have a new table, please check the below picture and the attached pbix file.

 

Jihwan_Kim_0-1728803085189.png

 

 

expected result table = 
	VAR _t = ADDCOLUMNS(
		Data,
		"@prevdate", MAXX(
			OFFSET(
				-1,
				Data,
				ORDERBY(
					Data[Date],
					ASC
				),
				,
				PARTITIONBY(Data[Phase]),
				MATCHBY(
					Data[Phase],
					Data[Date]
				)
			),
			Data[Date]
		)
	)
	VAR _condition = ADDCOLUMNS(
		_t,
		"@condition", IF(
			INT(Data[Date] - [@prevdate]) = 1,
			0,
			1
		)
	)
	VAR _group = ADDCOLUMNS(
		_condition,
		"@group", SUMX(
			WINDOW(
				1,
				ABS,
				0,
				REL,
				_condition,
				ORDERBY(
					Data[Date],
					ASC
				),
				,
				PARTITIONBY(Data[Phase]),
				MATCHBY(
					Data[Phase],
					Data[Date]
				)
			),
			[@condition]
		)
	)
	VAR _result = ADDCOLUMNS(
		SUMMARIZE(
			_group,
			Data[Phase],
			Data[Date],
			[@group]
		),
		"StartDate", MINX(
			FILTER(
				_group,
				Data[Phase] = EARLIER(Data[Phase]) && [@group] = EARLIER([@group])
			),
			Data[Date]
		),
		"EndDate", MAXX(
			FILTER(
				_group,
				Data[Phase] = EARLIER(Data[Phase]) && [@group] = EARLIER([@group])
			),
			Data[Date]
		)
	)

	RETURN
		SUMMARIZE(
			_result,
			Data[Phase],
			[StartDate],
			[EndDate]
		)

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

ValtteriN
Super User
Super User

Hi,

Here is one way to do this. Due to the table not containing ids this took a bit of trial and error:

EVALUATE
VAR _vtable1 =
    ADDCOLUMNS(
        'Table (44)',
        "PreviousDate",
        VAR _phase = [Phase]
        VAR _date = [Date]
        VAR _previous =
            CALCULATE(
                MAX('Table (44)'[Date]),
                FILTER(
                    'Table (44)',
                    [Phase] = _phase && 'Table (44)'[Date] < _date
                )
            )
        RETURN IF(_previous = _date - 1, _previous, BLANK())
    )

-- Compute the start date for each continuous block
VAR _startdate =
    ADDCOLUMNS(
        _vtable1,
        "StartDate",
        IF(
            ISBLANK([PreviousDate]),
            [Date],
            BLANK()
        )
    )

-- Identify the end date by checking for gaps in the next date
VAR _enddate =
    ADDCOLUMNS(
        _startdate,
        "EndDate",
        VAR _phase = [Phase]
        VAR _date = [Date]
        VAR _next =
            CALCULATE(
                MIN('Table (44)'[Date]),
                FILTER(
                    'Table (44)',
                    [Phase] = _phase && 'Table (44)'[Date] > _date
                )
            )
        RETURN IF(_next <> _date + 1 || ISBLANK(_next), _date, BLANK())
    )

-- Summarize the result to get start and end dates for each continuous range
VAR _result =
    VAR _vtable2 = GROUPBY(_enddate, [Phase], [StartDate], [EndDate])
    RETURN
        ADDCOLUMNS(
            _vtable2,
            "edate",
            VAR _phase = [Phase]
            VAR calculatedEdate =
            var _sdate = [StartDate] RETURN
                CALCULATE(
                    MINX(FILTER(_vtable2, [Phase] = _phase && [EndDate]>_sdate), [EndDate])
                )
            RETURN
                IF(
                    calculatedEdate < [EndDate],
                    [EndDate],  -- Return the original EndDate if calculatedEdate is smaller
                    calculatedEdate  -- Otherwise, return calculatedEdate
                )
        )
       

   
RETURN
GROUPBY(FILTER(_result,NOT(ISBLANK([StartDate]))),[Phase],[StartDate],[edate])




End result shown in query view:

ValtteriN_0-1728802634267.png




I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!

My LinkedIn: https://www.linkedin.com/in/n%C3%A4ttiahov-00001/








Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




tharunkumarRTK
Super User
Super User

@eng_123 

 

I think implementing this solution in power bi would be a much easier, please find the PBIX file attached.

Screenshot 2024-10-13 at 12.17.22 PM.png

 

Need a Power BI Consultation? Hire me on Upwork

 

 

 

Connect on LinkedIn

 

 

 








Did I answer your question? Mark my post as a solution!
If I helped you, click on the Thumbs Up to give Kudos.

Proud to be a Super User!


PBI_SuperUser_Rank@2x.png

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.