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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
htsvhwave
Helper II
Helper II

How to create Start Date and End Date columns from one Date Column in a new tabel

need to make the start date and end date columns from the date column, in a new tabel. I have a table of employees and the dates on which they are absent because of sickness but I need to reduce the table so that there is no separate row for each date if that date is part of a absent period that lasts more than one day.  Furthermore i need to filter "type of absence so the new tabel only contains absence regarding sickness forexample and not vacation.

Example Input: 

Name    Date        Type of absence   
Ivana   12/25/2020  Kids are sick
Ivana   12/26/2020  Kids are sick
Ivana   12/27/2020  Kids are sick
Ivana   4/20/2020   Sick
Ivana   4/21/2020   Sick
Ivana   5/1/2020    Vacation
Jelena  4/20/2020   Vacation
Jelena  4/21/2020   Vacation

 Expected Output:

Name    Start Date  EndDate     Type of absence
Ivana   12/25/2020  12/27/2020  Kids are sick
Ivana   4/20/2020   4/21/2020   Sick
Ivana   5/1/2020    5/1/2020    Vacation
Jelena  4/20/2020   4/21/2020   Vacation

How to do it? 

1 ACCEPTED SOLUTION
15 REPLIES 15
Ahmedx
Super User
Super User

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

https://1drv.ms/u/s!AiUZ0Ws7G26RiQUndIQmY11n3Mic?e=8LMOVL

Thank you for the help. would it be possible for you to take screenshot at the different steps as i am quite new with power bi and want to implement the steps in my own data?

see my video
https://1drv.ms/v/s!AiUZ0Ws7G26RiQiBLn8mE73iJOM5?e=Ioyci9

YearMonth =  Date.Year([Date])*100 + Date.Month([Date])

--------------------
{{"StartDate", (x)=> List.Min(x[Date])}, {"EndDate", (x)=> List.Max(x[Date])

 

Thank you it seem to work, however in my dataset i have employees who have been on sickleave for a couple of month, now that employees absence looks like this

NameabsenceStart dateEnd date
JelenaSick01-03-202331-01-2023
JelenaSick03-04-202328-04-2023
JelenaSick01-05-202331-05-2023

 

Where the wanted output should be 

NameabsenceStart dateEnd date
JelenaSick01-03-202331-05-2023

It has further split the dates

htsvhwave_0-1687260232505.png

 

first sort the table by Name and by date then repeat my video
or
Share sample pbix file to help you.

 

Hi

Im still having som issues, it seem as it is grouping some dates and others not

htsvhwave_0-1687870166164.png

 

htsvhwave
Helper II
Helper II

Thank you for the response, but i also want to see if a person is sick multiple times that it is shown in rows

Name    Start Date  EndDate     Type of absence
Ivana   12/25/2020  12/27/2020  Sick
Ivana   4/20/2020   4/21/2020   Sick
Ivana   5/1/2020    5/1/2020    Sick

Hi , @htsvhwave 

Thanks for your quick response!

According to your description,  You want to merge the data from top to bottom by row number, right?

This is my test data and my understanding for your end result :

vyueyunzhmsft_0-1687227106077.png

 

We can realize it in Power Query Editor.You can create a blank query and enter this in "Advanced Editor":

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8ixLzEtU0lEyNNI3MtU3MjAyAHK8M1OKFRKLUhWKM5OzlWJ1UJWZEafMnLAyE6ASmKpgLJKGOCVN9eFyYYnJiSWZ+Xlgea/UnFQMo3GqwG4G3BMQO4yw2Q+UwZCDm4wkiel3bKoQ1scCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Date = _t, #"Type of absence" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Date", type date}, {"Type of absence", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", (x)=> let absence= try #"Added Index"{x[Index]-2}[Type of absence] otherwise null in if absence =x[Type of absence] then   
1 else 0
  ),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom2", (x)=>
Table.Sort(Table.SelectRows(#"Added Custom" ,(y)=>y[Index]<=x[Index] and y[Custom]=0),{"Index",1}){0}[Index]
  ),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Custom"}),
    Custom1 = Table.Group(#"Removed Columns",{"Custom2","Name","Type of absence"},{
{"Start Date",(x)=>Table.Sort(x,"Date"){0}[Date]    },{"End Date",(x)=>Table.Sort(x,{"Date",1}){0}[Date] }

}),
    #"Removed Columns1" = Table.RemoveColumns(Custom1,{"Custom2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns1",{{"Start Date", type date}, {"End Date", type date}})
in
    #"Changed Type1"

 

Then we can get this Table:

vyueyunzhmsft_1-1687227199983.png

 

 

 

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

Thank you for the help @v-yueyunzh-msft. would it be possible for you to take screenshot at the different steps as i am quite new with power bi and want to implement the steps in my own data?

Hi , @htsvhwave 

Of course! Thanks for your quick response!

You can first create a blank query :

vyueyunzhmsft_1-1687248878316.png

 

Then you can go to the new blank query and you can click this in the Power Query Editor:

vyueyunzhmsft_0-1687248521452.png

Then you can put this in the code content:

 

let
    Source = Table,
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", (x)=> let absence= try #"Added Index"{x[Index]-2}[Type of absence] otherwise null in if absence =x[Type of absence] then   
1 else 0
  ),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom2", (x)=>
Table.Sort(Table.SelectRows(#"Added Custom" ,(y)=>y[Index]<=x[Index] and y[Custom]=0),{"Index",1}){0}[Index]
  ),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Custom"}),
    Custom1 = Table.Group(#"Removed Columns",{"Custom2","Name","Type of absence"},{
{"Start Date",(x)=>Table.Sort(x,"Date"){0}[Date]    },{"End Date",(x)=>Table.Sort(x,{"Date",1}){0}[Date] }

}),
    #"Removed Columns1" = Table.RemoveColumns(Custom1,{"Custom2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns1",{{"Start Date", type date}, {"End Date", type date}})
in
    #"Changed Type1"

 

For the "Source= Table" the Table is your table you need to convert.

vyueyunzhmsft_1-1687248634784.png

 

The Table is like this :

vyueyunzhmsft_2-1687248664336.png

 

You can check the correctness of each step of this new table, maybe there is an inconsistency in our column names, you can modify it.

vyueyunzhmsft_0-1687248855924.png

You can view each step of data conversion in each step in case there is an error.

 

 

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

Could you step by step show how to do it

htsvhwave_0-1687249132429.png

 

Hi, @htsvhwave 

Sorry, this may not be very clear to you.The best thing is that you can keep your step names the same as mine to avoid other problems.

Here are the detailed steps you can refer to :

First we need to create a blank query in Power Query Editor:

vyueyunzhmsft_11-1687250379542.png

 

(1)The first step(Source):

vyueyunzhmsft_12-1687250395955.png

 

 

 

= Table

 

 

vyueyunzhmsft_0-1687249624992.png

(2)The second step(Added Index):

vyueyunzhmsft_1-1687249657127.png

We can click the "From 1" in the Add Column Pane.

 

(3)The 3nd step(Added Custom):
We can click the "Custom Column" in Add Column Pane.

vyueyunzhmsft_2-1687249768417.png

And we can enter this in it:

 

 

let absence= try #"Added Index"{_[Index]-2}[Type of absence] otherwise null in if absence =_[Type of absence] then   
1 else 0

 

 

vyueyunzhmsft_3-1687249797187.png

 

(4)The 4nd step(Added Custom1):
We can click the "Custom Column" in Add Column Pane.

And we can enter this in it:

 

 

Table.Sort(Table.SelectRows(#"Added Custom" ,(y)=>y[Index]<=_[Index] and y[Custom]=0),{"Index",1}){0}[Index]

 

 

vyueyunzhmsft_4-1687249901569.png

 

(5)The 5nd step(Removed Columns):

vyueyunzhmsft_5-1687249935156.png

We can click "remove" button for the [Custom] in 4nd step, it will generate a new step automatically.

 

(6)The 6nd step(create a new step called "Custom1"):

We can click "Insert Step After" after the 5nd step. The default of this step is the "Custom1".

vyueyunzhmsft_6-1687250017637.png

We can put this code on the M code imput above the table:

 

 

= Table.Group(#"Removed Columns",{"Custom2","Name","Type of absence"},{
{"Start Date",(x)=>Table.Sort(x,"Date"){0}[Date]    },{"End Date",(x)=>Table.Sort(x,{"Date",1}){0}[Date] }
})

 

 

vyueyunzhmsft_7-1687250126853.png

 

(7)The 7nd step(Removed Columns1):

vyueyunzhmsft_8-1687250147259.png

We can click "Remove" of the [Custom2] to remove this column in 6nd step.

vyueyunzhmsft_9-1687250213983.png

 

(8)In the last step we can update the date type for all the columns we want to be :

vyueyunzhmsft_10-1687250263717.png

I hope this makes it clear for you to show each process.

 

 

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

iceparrot
Advocate II
Advocate II

Hi @htsvhwave , 

to solve your Problem you can open the Power Query Editor.

iceparrot_0-1686837067175.png


As you said you want to create a new table:

Duplicate your existing table with the example input by right clicking on the table namen in the left column and select Duplicate

iceparrot_1-1686837265714.png

 

Afterwards select the duplicated table and select Group By in the top navigation

iceparrot_2-1686837335567.png

 

In the window which opens switch to Advanced and set the inputs to the following:

iceparrot_3-1686837462030.png


When you're done press enter and then Save the result in the Power Query editor.

--- 
This is what the Power Query M looks like:

 

= Table.Group(#"Changed Type", {"Name", "Type of absence"}, {{"Start_Date", each List.Min([Date]), type nullable date}, {"End_Date", each List.Max([Date]), type nullable date}})

 


This is the final result: 

iceparrot_4-1686837669117.png

 



Let me know if this fixes your problem. If yes, please mark it as the solution to help others as well.

 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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