Forum Discussion

johnlhaase's avatar
johnlhaase
Helper I
5 years ago
Solved

Grouping Date sets within a month

Hello   I work for an airline and I trying to group On days together to get the Min and Max Dates within each date set. The problem is there are more than one On/Off days within each month. So, jus...
  • Jimmy801's avatar
    5 years ago

    Hello johnlhaase 

     

    check out this solution. I hope I got you right. The group-function with GroupKind.Local does the trick here

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jdOxDYMwGEThXVwj2b7f4KTNAhkAsQL7l1iCQ1TJkyy78Nc93bqmmmtWUUlTij6P+9NbG88499c2/XGCLqBr0M3QLdB16F7QvaGrhUJQ5LufFEQxBV1MQRpTUMcUBDIFjUxBJlNQ6qICrUx5LfFa4rXEa4nXEq8luijRSYluKnineHTSL6rMpbAMLJ1zOwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Start of Month" = _t, EmpID = _t, Fleet = _t, OnOff = _t, #"Rotation Date" = _t]),
        TransformDate = Table.TransformColumns(Source,{{"Rotation Date", each Date.From(_,"en-US"), type date},{"Start of Month", each Date.From(_,"en-US"), type date}}),
        Group = Table.Group(TransformDate, {"EmpID", "OnOff"}, {{"AllRows", each _, type table [Start of Month=date, EmpID=text, Fleet=text, OnOff=text, Rotation Date=date]}}, GroupKind.Local),
        AddMin = Table.AddColumn(Group, "Min", each List.Min([AllRows][Rotation Date]), type date),
        AddMax = Table.AddColumn(AddMin, "Max", each List.Max([AllRows][Rotation Date]), type date),
        ExpandGroupedTable = Table.ExpandTableColumn(AddMax, "AllRows", {"Start of Month", "Fleet", "Rotation Date"}, {"Start of Month", "Fleet", "Rotation Date"})
    
    in
        ExpandGroupedTable

     

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

  • johnlhaase's avatar
    johnlhaase
    5 years ago

    Hello

     

    You are correct. I did ask about the same month. This is a large data set and when I was going through the restlts I realized some of the On/Off days transition into the following month. 

     

    The first solution is fantasitic and is very useful. I wish I realized the data set had the transition issue priro to my first post. The law of unintended consequences. Anyways I am trying to get the scripts to ignore the start of month all together and just get the min/max for each On/Off sets.

     

    Thanks for your time

     

    John