Forum Discussion

gordgord1's avatar
gordgord1
Regular Visitor
6 months ago
Solved

Sequencing Numbering within a Group Based off Unique Values

Hello, 

Company I am at is decommissioning Alteryx.

Need some guidance to perform some sequence numbering in Power Bi/Power Query by group.

 

1. Filtering out products that already have an end date.

2. Sequence number based off a group and distinct dates.

 

In rare cases two events happen on the same date (B2 and B3 below). Need to keep.

So simple removal of duplicates would cause B3 to be removed.

 

Need to keep the rows that have a sequence number of 1 in example below. 

 

Any recommendations?

 

ProductEventStart DateEnd DateSequence Numbering
A11/2/20261/2/2026 
A21/5/2026 1
A31/7/2026 2
A41/9/2026 3
B12/2/20262/3/2026 
B22/9/2026 1
B32/9/2026 1
B42/14/2026 2
C13/15/2026 1
C23/16/2026 2
C34/1/2026 3
C44/2/2026 4
  • I do not know if the most elegant solution. Yesterday I found an independent solution thru brute force.

     

    1. Filter out Product Event that an End Date (Keep the nulls).

    2. Concat Product Name and Program Name plus Start Date (converted date to a number) and Sorted. Earlier Dates would be higher up. (Product plus Date Number below)

    3. Grouped by Products. (groups below)

    4. Added costom column  Rank = Table.AddRankColumn([Groups], "Rank", {"Product plus Date Number"}).

    5. Then expanded grouped data.

    6. Then filtered where Rank equal 1. In rare case where Product and Event had two rows were on the same day I could show.

4 Replies

  • To rephrase your question...

    You want to only keep entries where:
    - There is no end date
    - It's the earliest start date for a Product

    If that's true, you could try the following flag that would return 1 for rows you'd want to keep and 0 otherwise.

    Flag_Earliest_NoEndDate = 
    VAR CurrentProduct = SELECTEDVALUE(Products[Product])
    VAR CurrentStartDate = SELECTEDVALUE(Products[StartDate])
    VAR EarliestNoEndDate = 
        CALCULATE(
            MIN(Products[StartDate]),
            FILTER(
                ALL(Products),
                Products[Product] = CurrentProduct &&
                ISBLANK(Products[EndDate])
            )
        )
    RETURN
        IF(
            ISBLANK(Products[EndDate]) && 
            CurrentStartDate = EarliestNoEndDate,
            1,
            0
        )
  • gordgord1's avatar
    gordgord1
    Regular Visitor

    Sorry If I was unclear.

    I already filtered out rows if they had an end date. 
    For the remaining data, I was trying add increment/sequence numbering based dates in the Start Date column.
    Watching this video. Might help. (9) Create Index Column By Group in Power Query - YouTube
    Currently seeing if that will work.

     

    ProductEventStart DateEnd DateSequence Numbering
    A21/5/2026 1
    A31/7/2026 2
    A41/9/2026 3
    B22/9/2026 1
    B32/9/2026 1
    B42/14/2026 2
    C13/15/2026 1
    C23/16/2026 2
    C34/1/2026 3
    C44/2/2026 4
    • Ashish_Mathur's avatar
      Ashish_Mathur
      Icon for Super User rankSuper User

      Hi,

      This M code works

      let
          Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
          #"Grouped Rows" = Table.Group(Source, {"Product"}, {{"Count", each Table.AddRankColumn(_,"Sequence numbering",{"Start Date", Order.Ascending},[RankKind = RankKind.Dense])}}),
          #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Event", "Start Date", "End Date", "Sequence numbering"}, {"Event", "Start Date", "End Date", "Sequence numbering"}),
          #"Changed Type" = Table.TransformColumnTypes(#"Expanded Count",{{"Product", type text}, {"Event", Int64.Type}, {"Start Date", type date}, {"End Date", type date}, {"Sequence numbering", Int64.Type}})
      in
          #"Changed Type"

      Hope this helps.

  • gordgord1's avatar
    gordgord1
    Regular Visitor

    I do not know if the most elegant solution. Yesterday I found an independent solution thru brute force.

     

    1. Filter out Product Event that an End Date (Keep the nulls).

    2. Concat Product Name and Program Name plus Start Date (converted date to a number) and Sorted. Earlier Dates would be higher up. (Product plus Date Number below)

    3. Grouped by Products. (groups below)

    4. Added costom column  Rank = Table.AddRankColumn([Groups], "Rank", {"Product plus Date Number"}).

    5. Then expanded grouped data.

    6. Then filtered where Rank equal 1. In rare case where Product and Event had two rows were on the same day I could show.