Forum

Cleaning Data Situa...
 
Notifications
Clear all

Cleaning Data Situation - Leave Tracking Software

4 Posts
2 Users
0 Reactions
79 Views
(@cwagner0124)
Posts: 8
Active Member
Topic starter
 

Hello,

I use a leave tracking software where employees enter leave requests.  Each request creates a record and a group of records can be exported to a .csv file. The problem: when a multi-day request is made the record appears as follows:

StartDate EndDate Employee Hours Type
1/23/2017 1/26/2017 EEName 32 Vacation

I would like the data to list each day individually:

StartDate EndDate Employee Hours Type
1/23/2017 1/26/2017 EEName 8 Vacation
1/24/2017 1/26/2017 EEName 8 Vacation
1/25/2017 1/26/2017 EEName 8 Vacation
1/26/2017 1/26/2017 EEName 8 Vacation

Any tips?

 
Posted : 28/12/2017 10:00 pm
(@catalinb)
Posts: 1937
Member Admin
 

Hi Chris,

You can use this query, that will expand the rows where there are more than 8 hours:

let
    Source = Csv.Document(File.Contents("C:UsersCatalinDesktopSampleData.csv"),[Delimiter=",", Encoding=1252, QuoteStyle=QuoteStyle.None]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"StartDate", type date}, {"EndDate", type date}, {"Employee", type text}, {"Hours", Int64.Type}, {"Type", type text}}),

    Records = Table.ToRecords(#"Changed Type"),
    NumberOfHours=8,
    Expand = (x) => List.Generate(
        () => Record.Combine({x, [EndDate=x[StartDate],Hours=NumberOfHours]}),
        (record) => record[StartDate] <= x[EndDate],
        (record) => let
            NextStart = Date.StartOfDay(Date.AddDays(record[StartDate], 1)),
            NextEnd = Date.EndOfDay(NextStart),
            ThisEnd = List.Min({NextEnd, x[EndDate]})
        in
            Record.Combine({record, [StartDate=NextStart, EndDate=ThisEnd,Hours=NumberOfHours]})
            ),
    Transformed = List.Transform(Records, each if [Hours] <=8 then {_} else Expand(_)),

    Combined = List.Combine(Transformed),
    Result = Table.FromRecords(Combined),
    #"Changed Type1" = Table.TransformColumnTypes(Result,{{"StartDate", type date}, {"EndDate", type date}})

in
   #"Changed Type1"

I attached the sample file used, and the file with the query, so you can test it.

You should change the file path in the first line of the query:

Source = Csv.Document(File.Contents("C:UsersCatalinDesktopSampleData.csv")

 

The solution is inspired from this topic: multiply-rows-based-on-date-and-time

 
Posted : 29/12/2017 10:34 am
(@cwagner0124)
Posts: 8
Active Member
Topic starter
 

Hello Catalin,

Thanks so much for the help.  I appreciate the thorough response as well as the references and sample files.

Kind regards,

Chris

 
Posted : 29/12/2017 6:26 pm
(@catalinb)
Posts: 1937
Member Admin
 

You're wellcome 🙂

Keep in mind that the solution is based on the StartDate and EndDate, it ignores the Hours column, the new rows will always have 8 hours. If there is a record with a 4 days span and 28 hours instead of 32 (4*8), you will get 8 hours for all new rows, the initial 28 hours will not be distributed.

Depending on your needs, the solution needs to be adjusted if you need to distribute the exact amount from Hours column.

 
Posted : 30/12/2017 1:43 am
Share: