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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Brij
Helper III
Helper III

Create row sequence based on row id column but retaining other column

Hi,

 

I really need help with row sequencing based on a column (row_id) while retaining another column called 'split_desc'.

There are two columns in the table - 'row_id' and 'split_desc' and they look like the below sample:

Brij_0-1740650465420.png

 

I have tried using group by on row_id and then adding index will give me perfect result that I want but in that case I lose splic_desc column which is vital for me to keep in this table. When I include row_id and split_desc both in the group by and then add index it does not giving me the correct result as group by will consider both the column and grouped them and index will sequece accordingly which is what I don't want.

 

Currenlty I'm getting the below result, which is not correct, row_sequence highlighted in yellow should produce index # 2 but it is giving me #1 when adding both column in group by statement! 

 

Brij_2-1740651111383.png

 

A new row_sequence column should be purly based on row_id column while retaining the split_desc column. Here is the desire result that I am trying to achieve.

 

Brij_1-1740650813868.png

 

 Any help is appreicated.

 

Pat

1 ACCEPTED SOLUTION
SundarRaj
Super User
Super User

Hi @Brij. Here's a solution you could take a look at! Thanks

SundarRaj_0-1740654895714.png

SundarRaj_1-1740654914577.png

 

 

Sundar Rajagopalan

View solution in original post

8 REPLIES 8
dufoq3
Super User
Super User

Hi @Brij, another 2 solutions here:

 

Output

dufoq3_0-1740753792913.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJ3VorVgTIjwEwjIBPMMIYxTGAMUxjDDMgIC0Iww8BMc5i0BZBRUQFmWsLEDA2ArKoqpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Row_ID = _t, Split_Desc = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Row_ID", Int64.Type}, {"Split_Desc", type text}}),
    v1_TableGroup = Table.Combine(Table.Group(ChangedType, {"Row_ID"}, {{"T", each Table.AddIndexColumn(_, "Row Sequence", 1, 1, Int64.Type), type table}})[T]),
    v2_ListGenerate = [ L = List.Buffer(v1_TableGroup[Row_ID]),
    LG = List.Generate(
                ()=> [x=0, y=L{x}, z=1],
                each [x] < List.Count(L),
                each [x=[x]+1, y=L{x}, z={1, [z]+1}{Byte.From(y=[y])} ],
                each [z] ),
    T = Table.FromColumns(Table.ToColumns(ChangedType) & {LG}, Value.Type(Table.FirstN(ChangedType, 0) & #table(type table[Row Sequence=Int64.Type], {})))
  ][T]
in
    v2_ListGenerate

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

Omid_Motamedise
Super User
Super User

Hi,

Use the following formula in the advanced editor, or see the attached file

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJ3VorVgTIjwEwjIBPMMIYxTGAMUxjDDMgIC0Iww8BMc5i0BZBRUQFmWsLEDA2ArKoqpdhYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Row_ID = _t, Split_Desc = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Row_ID", Int64.Type}, {"Split_Desc", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Row_ID"}, {{"Count", each Table.AddIndexColumn(_,"Row_Sequence",1)}}),
    #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Row_ID"}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Removed Columns", "Count", {"Row_ID", "Split_Desc", "Row_Sequence"}, {"Row_ID", "Split_Desc", "Row_Sequence"})
in
    #"Expanded Count"
If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
Brij
Helper III
Helper III

Hi @Akash_Varuna ,

 

Thanks for your quick response.

 

I have done exact steps that you mentioned. However with your step 'Group Data', you mentioned only one column 'row_id' hence it will group and after adding index, it will produce an accurate row sequencing. However, in this process, we are missing out on the 'split_desc' column which is what I am having trouble with!

 

I hope you get my point.

SundarRaj
Super User
Super User

Hi @Brij. Here's a solution you could take a look at! Thanks

SundarRaj_0-1740654895714.png

SundarRaj_1-1740654914577.png

 

 

Sundar Rajagopalan

Hi @SundarRaj ,

 

This is perfect solution. It works for me.

 

Not sure what mistake I was doing? Could you please explain the last step (Soln) that you created. I was trying to get this step done through UI and was not getting the desire result.

 

Appreciate if you can explain.

 

Thank you

Brijesh

Sure @Brij
1. Consider the second image. That Table shows you the step before the Indexing.

2. Table.TransformColumns helps me to get into a table, specify the column (the "All" column) on which I'd like to make alterations (in this case, add the index column).

3. After the indexing, what I'd like to do is combine all the tables and get the desired result. Table.Combine accepts Tables as list and combines them ( [ColumnName] gives me the Tables in a list format, just what Table.Combine accepts ).

 

SundarRaj_0-1740657605099.png

SundarRaj_1-1740657627443.png

If you still need clarifications, don't hesitate to ping back. Thanks!

 

Sundar Rajagopalan

Thank you @SundarRaj 

Akash_Varuna
Super User
Super User

Hi @Brij , After the index did you expand it Could you try this please 

  • Sort the Data:

    • Sort the table first by the Row_Id column in ascending order, then by Split_Desc in ascending order. This ensures a proper order for generating the sequence.
  • Group the Data:

    • Group the data by Row_Id:
      • Go to Transform → Group By.
      • Group by Row_Id.
      • Create an All Rows operation to retain the grouped table.
  • Add an Index for Sequencing:

    • Within each group, add an index to generate the Row_Sequence:
      • After grouping, click the small table icon for the grouped column.
      • Add an Index Column starting from 1.
      • Rename this index column as Row_Sequence.
  • Expand the Table:

    • After adding the index, expand the grouped table to flatten it back into a single table:
      • Click on the expand icon next to the grouped column.
      • Include all original columns and the newly created Row_Sequence column.

 

let
  
    Source = Table.FromRows(
        {
        //Data
        },
        {"Row_Id", "Split_Desc"}
    ),
    
    // Convert data types
    ChangeTypes = Table.TransformColumnTypes(Source, {{"Row_Id", Int64.Type}, {"Split_Desc", Text.Type}}),
    
    // Sort by Row_Id and Split_Desc
    SortedTable = Table.Sort(ChangeTypes, {{"Row_Id", Order.Ascending}, {"Split_Desc", Order.Ascending}}),
    
    // Group by Row_Id
    GroupedTable = Table.Group(
        SortedTable,
        {"Row_Id"},
        {
            {"AllData", each Table.AddIndexColumn(_, "Row_Sequence", 1, 1, Int64.Type)}
        }
    ),
    
    // Expand grouped data
    ExpandedTable = Table.ExpandTableColumn(GroupedTable, "AllData", {"Row_Id", "Split_Desc", "Row_Sequence"})
in
    ExpandedTable
​

 

If this post helped please do give a kudos and accept this as a solution
Thanks In Advance

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors