Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
Tamillllll
Frequent Visitor

Create a column with entries based on one with ID and another with sequence

I want to create a new column as 'Type' based on two columns with one column being an ID and the other being a sequence of numbers. For each ID, the starting number should be entered as the First type, the following numbers as Intermediate, and the last number should be entered as the Last type. below is the sample table, the original table will have many unique IDs with this sequence. Can you please help me with the steps/ideas on how this can be achieved? Any help is appreciated.

 

Tamillllll_0-1705288892703.png

 

4 REPLIES 4
dufoq3
Super User
Super User

Hi @Tamillllll, different approach here.

 

Result

dufoq3_0-1711291961678.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTJUitWBsIzgLGM4ywTOMgWznOA6nOA6nOA6nOA6nFB0mMFZ5kqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Id = _t, #"Stop Sequence" = _t]),
    // Added [Type] to intermediate [All] table
    GroupedRows = Table.Group(Source, {"Id"}, {{"All", each 
        Table.AddColumn(_, "Type", (x)=>
              if x[Stop Sequence] = List.Min([Stop Sequence]) then "First" else
              if x[Stop Sequence] = List.Max([Stop Sequence]) then "Last"
              else "Intermediate", type text), type table}} ),
    CombinedAll = Table.Combine(GroupedRows[All])
in
    CombinedAll

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Tamillllll
Frequent Visitor

Hi @AlienSx 

Thank you so much for the solution, sorry if my table was confusing, its based on two columns grouping Id's together and from the stop sequence column - taking 1 as 'first' and last highest as 'Last' and intermediate numbers as 'Intermediate'. Let me know if below helps.

Tamillllll_0-1705294994159.png

 

@Tamillllll then sort by sequence column first. If you want to retain original sorting order then add index column before that and sort by this column in the end.

let
    Source = your_table,
    f = (tbl) =>
        [a = Table.Sort(tbl, "Step Sequence"),
        recs = List.Buffer(Table.ToRecords(a)),
        count = List.Count(recs),
        gen = List.Generate(
            () => [i = 0, r = recs{0} & [Type = "First"]],
            (x) => x[i] < count,
            (x) => [i = x[i] + 1, r = recs{i} & [Type = if i + 1 = count then "Last" else "Intermediate"]],
            (x) => x[r]
        )][gen],
    g = Table.Group(Source, {"id"}, {{"a", f}}),
    exp = Table.FromRecords(List.Combine(g[a]))
in
    exp

 

AlienSx
Super User
Super User

@Tamillllll 

let
    Source = your_table,
    f = (tbl) =>
        [recs = List.Buffer(Table.ToRecords(tbl)),
        count = List.Count(recs),
        gen = List.Generate(
            () => [i = 0, r = recs{0} & [Type = "First"]],
            (x) => x[i] < count,
            (x) => [i = x[i] + 1, r = recs{i} & [Type = if i + 1 = count then "Last" else "Intermediate"]],
            (x) => x[r]
        )][gen],
    g = Table.Group(Source, {"id"}, {{"a", f}}),
    exp = Table.FromRecords(List.Combine(g[a]))
in
    exp

Helpful resources

Announcements
October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors