Forum Discussion

TH20020520's avatar
TH20020520
Frequent Visitor
1 year ago
Solved

How to differentiate between different rows in different columns

I have table that contains for examples  Employee Id- Start date of assignment- End date of assignment- position Id  and there is possibility that the same employee can be at the same position for ...
  • Akash_Varuna's avatar
    1 year ago

    Hi TH20020520 Could you try these please 

    1. Sort the Table:
      In Power Query, sort by Employee ID, Position, and Start Date.

    2. Add Index Column:
      Add an Index column to track row order.

    3. Add Custom Column:
      if [Employee Id] = Table.PreviousRow("Employee Id") and [Position] = Table.PreviousRow("Position") and Duration.Days(Date.From([Start Date]) - Date.From(Table.PreviousRow("End Date"))) <= 90 then PreviousGroup else NewGroup

    4. Group Rows:
      Generate unique group indices for rows meeting the criteria.
      If this post helped please do give a kudos and accept this as a solution
      Thanks In Advance

  • dufoq3's avatar
    1 year ago

    Hi TH20020520, be more pricise for next time please:

     

    1. once the datetime is in "d-m-yyyy" format and once in "m-d-yyyy"
    2. the date 30th of Feb doesn't exist
    3. you should provide sample data as a table (if you don't know how - read note below my post please)

     

    I'm not sure what should be your output - you should provide expected output based on sample data, but I gues you want something like this:

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dcq5DQAgEAPBXhwDwubtBV3/bUDEkZCNtLsWqJIQwMiorHao7C4iLNxLz3bwuTg8aX43eWF319ZhtgE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"employee id" = _t, #"start date" = _t, #"end date" = _t, position = _t]),
        ChangedType = Table.TransformColumnTypes(Source,{{"start date", type date}, {"end date", type date}, {"position", Int64.Type}}, "sk-SK"),
        L = List.Buffer(Table.ToRows(ChangedType)),
        Gen = List.Skip(List.Generate(
            ()=> [ x = -1, y = 0 ],
            each [x] < List.Count(L),
            each [ x = [x]+1, r = [r]+1, y = try (if {L{x}{0}, L{x}{3}} = {L{[x]}{0}, L{[x]}{3}} and Duration.Days(L{x}{1} - L{[x]}{2}) <= 90 then [y] else r) otherwise [y]+1 ],
            each [y])),
        Merged = Table.FromColumns(Table.ToColumns(ChangedType) & {Gen}, Value.Type(Table.FirstN(ChangedType, 0) & #table(type table[Index=Int64.Type], {})))
    in
        Merged