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
Anonymous
Not applicable

Index Column By Category

Hi all,

 

I have the following column

 

Category
Bear
Bear
Pig
Pig
Parrot
Parrot

 

And I want to add an index by category, like this:

 

Category Index
Bear 1
Bear 1
Pig 2
Pig 2
Parrot 3
Parrot 3

 

I know I could make a custom column with an if statement, but I need it to work dynamically. Could someone advice me on the M code? 

 

Best regards,

 

Morten 

1 ACCEPTED SOLUTION
TeigeGao
Solution Sage
Solution Sage

Hi @Anonymous ,

Please refer to the following M query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WckpNLFKK1UFiBGSmo9KJRUX5JSjMWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Category = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Category", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Category"}, {{"Category Index", each _, type table [Category=text]}}),
    #"Added Index" = Table.AddIndexColumn(#"Grouped Rows", "Index", 1, 1),
    #"Expanded Category Index" = Table.ExpandTableColumn(#"Added Index", "Category Index", {"Category"}, {"Category Index.Category"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Category Index",{{"Index", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type1", "Category Index", each [Category Index.Category] &" " &[Index]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Category", "Category Index.Category", "Index"})
in
    #"Removed Columns"

The result will like below:

PBIDesktop_4LaPNGlRlg.png

Best Regards,

Teige

View solution in original post

3 REPLIES 3
TeigeGao
Solution Sage
Solution Sage

Hi @Anonymous ,

Please refer to the following M query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WckpNLFKK1UFiBGSmo9KJRUX5JSjMWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Category = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Category", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Category"}, {{"Category Index", each _, type table [Category=text]}}),
    #"Added Index" = Table.AddIndexColumn(#"Grouped Rows", "Index", 1, 1),
    #"Expanded Category Index" = Table.ExpandTableColumn(#"Added Index", "Category Index", {"Category"}, {"Category Index.Category"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Category Index",{{"Index", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type1", "Category Index", each [Category Index.Category] &" " &[Index]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Category", "Category Index.Category", "Index"})
in
    #"Removed Columns"

The result will like below:

PBIDesktop_4LaPNGlRlg.png

Best Regards,

Teige

Anonymous
Not applicable

Hi @TeigeGao and @Zubair_Muhammad , 

 

Thank you for that. 


I was actually looking for the following table (sorry for sloppy explenation):

 

CategoryIndex
Bear1
Bear1
Pig2
Pig2
Parrot3
Parrot3

 

But I could still use your code to create the column. 

 

Thank you for helping me out on this one!

 

Have a good day

 

Best ,

 

Morten

Zubair_Muhammad
Community Champion
Community Champion

Hi @Anonymous 

 

Using Query Editor..

You can Groupby the Category column >>> then add an index Column>>>then expand it

 

This way same category items will have same index number


Regards
Zubair

Please try my custom visuals

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.