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
Mederic
Post Patron
Post Patron

Replace Index Column by others values

Hello all,

I have an Index column as shown below (1) :

And I would like to replace the values by the following result in Power query (2) :

Thank you in advance

Best regards

(1)

 

Index
1
2
3
4
5
1
2
3
4
5

1
2
3
1
2
3

1
2
1
2

1
2
3
4
1
2
3
4

 

 (2)

 

Index
1
2
1
2
1
2
1
2
1
2

1
2
1
2
1
2

1
2
1
2

1
2
1
2
1
2
1
2

 

3 REPLIES 3
Anonymous
Not applicable

Hi @Mederic ,

 

Here's my solution.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMgKTxmDSBEyagkn8sjiUYIpgCCOz8VqEVTwWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index.1", 1, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each if [Index]=null then [Index.1] else null),
    #"Filled Up" = Table.FillUp(#"Added Custom",{"Custom"}),
    #"Replaced Value" = Table.ReplaceValue(#"Filled Up",null,1,Replacer.ReplaceValue,{"Custom"}),
    #"Grouped Rows" = Table.Group(#"Replaced Value", {"Custom"}, {{"All Rows", each _, type table [Index=nullable number, Index.1=number, Custom=number]}}),
    #"Added Custom1" = Table.AddColumn(#"Grouped Rows", "Custom.1", each Table.AddIndexColumn([All Rows],"GroupIndex",1,1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"All Rows"}),
    #"Expanded Custom.1" = Table.ExpandTableColumn(#"Removed Columns", "Custom.1", {"Index", "Index.1", "GroupIndex"}, {"Index", "Index.1", "GroupIndex"}),
    #"Added Custom2" = Table.AddColumn(#"Expanded Custom.1", "Custom.1", each if [Index]=null then null else if Number.Mod([GroupIndex],2)=0 then 2 else 1)
in
    #"Added Custom2"

You can download my attachment for more details.

vstephenmsft_8-1690438803137.png

 

                                                                                                                                                         

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.           

Hello @Anonymous ,

I'm really sorry, I hadn't seen your reply and thank you for this alternative solution,
My problem is solved in another thread.

Have a nice evening

Best regards

Mederic
Post Patron
Post Patron

Hello,

This code seems to work,
If you have another solution, I'd be grateful

Best regards

 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type",each [Index], each try if 
     Number.IsEven([Index]) then 2 else 1 otherwise null, Replacer.ReplaceValue,{"Index"})
in
    #"Replaced Value"

 

 

 

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