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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
davidgray42
Frequent Visitor

Index column for only certain rows?

Hi all, 

Can anyone please tell me how I can add an index colum but only for certain rows?  So for example in the data below 

I would like rows 4 and 5 to be 1 and 2 respectively.  

davidgray42_0-1712924577911.png

Thanks 

1 ACCEPTED SOLUTION
dufoq3
Community Champion
Community Champion

Hi @davidgray42, check this.

 

Result:

dufoq3_0-1713160063497.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCkgtSS1ScC/KTEvLzFOK1YlWCi5JzFMIzs0syQBzPfJzgQqCM3MLivMhCsIyizNL8osw2F75eQou+amo4rEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Person = _t]),
    #"Grouped Rows" = Table.Group(Source, {"Person"}, {{"All", each _, type table [Person=nullable text]}}),
    Ad_AllWithIndex = Table.AddColumn(#"Grouped Rows", "All With Index", each if Text.StartsWith([Person], "Visitor", Comparer.OrdinalIgnoreCase) then Table.AddIndexColumn([All], "Index", 1, 1, Int64.Type) else [All], type table),
    ExpandedAllWithIndex = Table.ExpandTableColumn(Ad_AllWithIndex, "All With Index", {"Index"}, {"Index"}),
    RemovedColumns = Table.RemoveColumns(ExpandedAllWithIndex,{"All"})
in
    RemovedColumns

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

View solution in original post

4 REPLIES 4
dufoq3
Community Champion
Community Champion

Hi @davidgray42, check this.

 

Result:

dufoq3_0-1713160063497.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCkgtSS1ScC/KTEvLzFOK1YlWCi5JzFMIzs0syQBzPfJzgQqCM3MLivMhCsIyizNL8osw2F75eQou+amo4rEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Person = _t]),
    #"Grouped Rows" = Table.Group(Source, {"Person"}, {{"All", each _, type table [Person=nullable text]}}),
    Ad_AllWithIndex = Table.AddColumn(#"Grouped Rows", "All With Index", each if Text.StartsWith([Person], "Visitor", Comparer.OrdinalIgnoreCase) then Table.AddIndexColumn([All], "Index", 1, 1, Int64.Type) else [All], type table),
    ExpandedAllWithIndex = Table.ExpandTableColumn(Ad_AllWithIndex, "All With Index", {"Index"}, {"Index"}),
    RemovedColumns = Table.RemoveColumns(ExpandedAllWithIndex,{"All"})
in
    RemovedColumns

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

davidgray42
Frequent Visitor

Hi, 

 

This is a turnstile card system, so visitors are given temparay cards to enter the building.  I would like this to show on reports as Visitor1, Visitor2 etc but there's no need to append a number to the end of a named card. 

 

 

HotChilli
Community Champion
Community Champion

What's the logic behind applying the index to rows 4 and 5?  Only Index the 'Visitor' rows? or specifically rows 4,5 regardless of data?

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.