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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Dom87326
Helper II
Helper II

Index column based on other column

Hello, 

 

I'd like to add an index column to the table below. It should take into account 'Attributes' columns and needs to be incremented by  +1 when a new value i.e. Question appreas. 

Dom87326_0-1651574698234.png

 

 I hope that someone can help here. Thank you. 

 

1 ACCEPTED SOLUTION
Vijay_A_Verma
Super User
Super User

Group on Attribute column - Insert the required Index - Ungroup

See the working here for 2 columns table - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTJUitWBsIzgLGM4ywTMcgKyTOEsMzjLEM6C6HWG63CGy7rAZV3hJrvCZd0QrJg8KCcWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Attribute = _t, Answer = _t]),
    #"Grouped Rows" = Table.Group(Source, {"Attribute"}, {{"Temp", each _, type table [Attribute=nullable text, Answer=nullable text]}}),
    #"Added Index" = Table.AddIndexColumn(#"Grouped Rows", "Index", 1, 1, Int64.Type),
    #"Expanded Temp" = Table.ExpandTableColumn(#"Added Index", "Temp", {"Answer"}, {"Answer"})
in
    #"Expanded Temp"

 

View solution in original post

1 REPLY 1
Vijay_A_Verma
Super User
Super User

Group on Attribute column - Insert the required Index - Ungroup

See the working here for 2 columns table - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTJUitWBsIzgLGM4ywTMcgKyTOEsMzjLEM6C6HWG63CGy7rAZV3hJrvCZd0QrJg8KCcWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Attribute = _t, Answer = _t]),
    #"Grouped Rows" = Table.Group(Source, {"Attribute"}, {{"Temp", each _, type table [Attribute=nullable text, Answer=nullable text]}}),
    #"Added Index" = Table.AddIndexColumn(#"Grouped Rows", "Index", 1, 1, Int64.Type),
    #"Expanded Temp" = Table.ExpandTableColumn(#"Added Index", "Temp", {"Answer"}, {"Answer"})
in
    #"Expanded Temp"

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors