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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
osama_ayoub
Helper III
Helper III

Creating Index Column

I have a table with 2 column Code and substitute and there are some iterrated codes in the two columns, 

this means maybe row 1 has code a and Substitue b and 

row 2 has Code b and Substitue C and

row 3 has Code F and Substitue D and so on ,

What I want to create a Index Column

and this Index Column will put the same Index for row 1 and row 2 because the a and B and C are alternatives and

put another Index for line 3 and so on 

osama_ayoub_0-1691011917097.png

 

3 REPLIES 3
wdx223_Daniel
Community Champion
Community Champion

NewStep=#table(Table.ColumnNames(PreviousStepName)&{"Index"},List.Accumulate(Table.ToRows(PreviousStepName),{{},[],1},(x,y)=>let a=List.Max(List.Transform(y,each Record.FieldOrDefault(x{1},_,null))) in if a=null then {x{0}&{y&{x{2}+1}},Record.TransformFields(x{1},List.Transform(y,(z)=>{z,each x{2}+1}),2),x{2}+1} else {x{0}&{y&{a}},Record.TransformFields(x{1},List.Transform(y,(z)=>{z,each a}),2),x{2}}){0})

Thank you for your support but I still have this errror 

 

osama_ayoub_0-1691049073145.png

 

This is the code of the query 

 

let
Source = Table1,
#"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
#"Filtered Rows" = Table.SelectRows(#"Added Index", each [Index] < 100),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Index"}),
#"Added Custom" = Table.AddColumn(#"Removed Columns", "Custom", each #"Removed Columns"(Table.ColumnNames(#"Removed Columns")&{"Index"},List.Accumulate(Table.ToRows(#"Removed Columns"),{{},[],1},(x,y)=>let a=List.Max(List.Transform(y,each Record.FieldOrDefault(x{1},_,null))) in if a=null then {x{0}&{y&{x{2}+1}},Record.TransformFields(x{1},List.Transform(y,(z)=>{z,each x{2}+1}),2),x{2}+1} else {x{0}&{y&{a}},Record.TransformFields(x{1},List.Transform(y,(z)=>{z,each a}),2),x{2}}){0}))
in
#"Added Custom"

 

let
Source = Table1,
#"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
#"Filtered Rows" = Table.SelectRows(#"Added Index", each [Index] < 100),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Index"}),
#"Added Custom" = #table(Table.ColumnNames(#"Removed Columns")&{"Index"},List.Accumulate(Table.ToRows(#"Removed Columns"),{{},[],1},(x,y)=>let a=List.Max(List.Transform(y,each Record.FieldOrDefault(x{1},_,null))) in if a=null then {x{0}&{y&{x{2}+1}},Record.TransformFields(x{1},List.Transform(y,(z)=>{z,each x{2}+1}),2),x{2}+1} else {x{0}&{y&{a}},Record.TransformFields(x{1},List.Transform(y,(z)=>{z,each a}),2),x{2}}){0})
in
#"Added Custom"

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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 Solution Authors