Forum Discussion

ChandeepChhabra's avatar
ChandeepChhabra
Impactful Individual
1 year ago
Solved

Grouping Problem

I am not sure where am I making a mistake. PQ (M) geniuses please help!   I have this 2 columnar table (Dates are in dd-mm) WinLoss  Date Win 01-01-2024 Loss 01-01-2024 Loss 02-01-202...
  • AlienSx's avatar
    1 year ago

    Hello, ChandeepChhabra I don't think you'll solve this with Table.Group and it's 5th agrument alone. Consider index column to control number of rows in your current group.

    let
        Source = Excel.CurrentWorkbook(){[Name="WinLossWDates"]}[Content],
        idx = Table.AddIndexColumn(Source, "idx"),
        group = Table.Group(
            idx, 
            {"WinLoss", "Date", "idx"}, 
            {"Streak", Table.RowCount}, 
            GroupKind.Local, 
            (s, c) => Number.From(
                s[WinLoss] <> c[WinLoss] or 
                Duration.Days(c[Date] - s[Date]) <> (c[idx] - s[idx])
            )
        )
    in
        group

     

  • AlienSx's avatar
    AlienSx
    1 year ago

    Lets start with first 2 rows: 

    We start with

    State = [Winloss = "Win", Date = #date(2024, 01, 01)]

    and we compare this record to the very next record

    Current =  [Winloss = "Loss", Date = #date(2024, 01, 01)]

    Do we need to start new group here? YES, because State[Winloss] <> Current[Winloss].

    And we start new group with 

    State = [WinLoss = "Loss", Date = #date(2024, 01, 01)] and check for the very next record which is 

    Current = [WinLoss = "Loss", Date = #date(2024, 02, 01)]. It's "Current (1)" over here:

     

     Do we need to start new group at Current (1)? NO, because State[Winloss] = Current[Winloss] and duration between Current[Date] and State[Date] is 1 day exactly. We keep grouping so that "State" stays the same! It always stays the same withing the group! 

    Next step: we stay in the same group with the same "State" but the very next "Current" becomes our next record

    Current (2) = [WinLoss = "Loss", Date = #date(2024, 03, 01)]. Again, "State" is the same as we are in the same group. We need to make a decision: start new group or not? WinLoss field is same but what about Date? At this point we have State[Date] (which is Jan 1st) and Current[Date] (which is Jan 3rd). We don't have info about previous row with Date = Jan 2nd anymore but we need to make a decision.

    That's why we need some helper column in the form of index to calculate something like "running total" between "State" and "Current" to compare it to duration in days in this particular case. 

    The key is that the only information we have at each stage of iteration is just the very first ("state") row/record and current row. Iterator moves on. So that at each step the arguments of our desicion making function (5th argument) are 2 rows/records: the very first one and the current one. Decision is always the same: do we need to start new group or stay in the current group with the same "State" record and next "Current" record.