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
Syndicate_Admin
Administrator
Administrator

Index column

Good morning, I need help!

How can I in Power Query create a conditional index column, I explain:

nataliaguzman02_0-1636559362416.png

I need to generate a conditional index column, so that it only lists the columns that have non-zero values, so in the image data it should have from 1 to 5 and the values that are 0 do not have a number. Can anyone help me?

1 ACCEPTED SOLUTION
PhilipTreacy
Super User
Super User

Hi @Syndicate_Admin  @nataliaguzman02 

 

Download example PBIX file with the following code

 

This query does what you want

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WsjA3MDBQitWJVkImzYxMDdBZENLI2ACbBqikoRkOyVgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Cportafolio_t.Rendimento Mercado" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Cportafolio_t.Rendimento Mercado", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each if [Cportafolio_t.Rendimento Mercado] <> 0 then List.Count(List.RemoveMatchingItems(List.RemoveLastN(#"Added Index"[Cportafolio_t.Rendimento Mercado],List.Count(#"Added Index"[Cportafolio_t.Rendimento Mercado])-[Index]),{0}) ) else ""),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"})
in
    #"Removed Columns"

condind.png

Regards

 

Phil

 



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


View solution in original post

2 REPLIES 2
PhilipTreacy
Super User
Super User

Hi @Syndicate_Admin  @nataliaguzman02 

 

Download example PBIX file with the following code

 

This query does what you want

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WsjA3MDBQitWJVkImzYxMDdBZENLI2ACbBqikoRkOyVgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Cportafolio_t.Rendimento Mercado" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Cportafolio_t.Rendimento Mercado", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each if [Cportafolio_t.Rendimento Mercado] <> 0 then List.Count(List.RemoveMatchingItems(List.RemoveLastN(#"Added Index"[Cportafolio_t.Rendimento Mercado],List.Count(#"Added Index"[Cportafolio_t.Rendimento Mercado])-[Index]),{0}) ) else ""),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"})
in
    #"Removed Columns"

condind.png

Regards

 

Phil

 



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Thank you very much for your help!

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.