Forum Discussion

Thiviya's avatar
Thiviya
Frequent Visitor
1 year ago
Solved

Combine data

Hi, How to summarise data by employee id , with the continus departure and arrival date and same location provide the total number of days away . If same employee, same location but not continuouly...
  • lbendlin's avatar
    1 year ago

    All you need to do is to create new rows for each of the days, and then use GroupKind.Local  to collect the individual contiguous ranges.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZLNCsIwEIRfpfSkUMhmk/7k6MWbUEQQKR4CLVXUFmpF6tO7PRiXntrcdgbyMbuZoggRJCgMo3DT9Je2GYLttf/UVWfvJZkn23X23Ta1JYGJgFggoCahwIlztAgjpYD0h5GxEwsxiAIMT2M4BlOZkb+3w6NtyiC33a16Bqv8cFyTvXuVdVXRQBtpF8U4sYiBwCBqcpWZDM0uK1M/xpj/DxGQ+DDcM5q1JyNmEMz8IGh4EpyUZG4SxZo2ttcLQt+bOYhyYtk6mkESv66mrO/I+n7+Ag==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Employee ID" = _t, Employee.displayName = _t, TemporaryTransferLocation = _t, DateofDeparture1 = _t, DateofArrival1 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"DateofDeparture1", type date}, {"DateofArrival1", type date}},"en-GB"),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Date", each List.Dates([DateofDeparture1],Int64.From([DateofArrival1]-[DateofDeparture1])+1,#duration(1,0,0,0))),
        #"Expanded Date" = Table.ExpandListColumn(#"Added Custom", "Date"),
        #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Date",{{"Date", type date}}),
        #"Sorted Rows" = Table.Sort(#"Changed Type1",{{"Employee ID", Order.Ascending}, {"Date", Order.Ascending}}),
        #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type),
        #"Grouped Rows" = Table.Group(#"Added Index", {"Employee ID","Date","Index"}, {{"Days Away", each Table.RowCount(_), Int64.Type}, {"Rows", each _, type table [Employee ID=nullable text, Employee.displayName=nullable text, TemporaryTransferLocation=nullable text, DateofDeparture1=nullable date, DateofArrival1=nullable date, Date=nullable date, Index=number]}},GroupKind.Local,(first,current)=>Number.From(current[Employee ID]<>first[Employee ID] or Int64.From(current[Date]-first[Date])>current[Index]-first[Index]))
    in
        #"Grouped Rows"

     

    https://www.thebiccountant.com/2018/01/21/table-group-exploring-the-5th-element-in-power-bi-and-power-query/

     

    ImkeF 

     

    Note that your "Number of days"  values are ambiguous.  Is the range from 26/05/2024 to 30/05/2024 four days? Or is it five days?