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
cgkas
Helper V
Helper V

How to add sequence number to group of same words?

Hello everyone,

 

I have the column "Words" that is the input and I´d like to get the strings in this column, sorted alphabetically and adding a sequence number in such way that the resulting Column "Sorted Words" be like below:

 

WORDSSORTED WORDS
BBBBBBBB_1
GGGGBBBB_2
DDDDBBBB_3
BBBBCCCC_1
GGGGDDDD_1
CCCCDDDD_2
KKKKDDDD_3
CCCCDDDD_4
GGGGGGGG_1
CCCCGGGG_2
KKKKGGGG_3
CCCCGGGG_4
KKKKGGGG_5
DDDDKKKK_1
BBBBKKKK_2
KKKKKKKK_3
DDDDKKKK_4

 

My current codes does the easiest part, sort the column, but I don´t know how to add the sequence number to each group of words.

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcgICpVidaCV3IAAzXIAAzMCUcgYCMMMbCFBFiFEDF8G0Ak0qFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [WORDS = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"WORDS", type text}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"WORDS", Order.Ascending}})
in
    #"Sorted Rows"

 

2 ACCEPTED SOLUTIONS
MariusT
Helper II
Helper II

This article show you how to create row numbers for groups.

 

https://radacad.com/create-row-number-for-each-group-in-power-bi-using-power-query

 

After doing this you can create a custom column [SORTED WORDS]&"-"&[RowNumberColumn]

View solution in original post

Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    Partition = Table.Group(Source, {"WORDS"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
    #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Index"}, {"Index"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Partition", "Custom", each [WORDS]&"_"&Number.ToText([Index])),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"}),
    #"Sorted Rows" = Table.Sort(#"Removed Columns",{{"WORDS", Order.Ascending}, {"Custom", Order.Ascending}})
in
    #"Sorted Rows"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

5 REPLIES 5
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    Partition = Table.Group(Source, {"WORDS"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
    #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Index"}, {"Index"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Partition", "Custom", each [WORDS]&"_"&Number.ToText([Index])),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"}),
    #"Sorted Rows" = Table.Sort(#"Removed Columns",{{"WORDS", Order.Ascending}, {"Custom", Order.Ascending}})
in
    #"Sorted Rows"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

@Ashish_Mathur Thanks so much for your help. It helps a lot.

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
MariusT
Helper II
Helper II

This article show you how to create row numbers for groups.

 

https://radacad.com/create-row-number-for-each-group-in-power-bi-using-power-query

 

After doing this you can create a custom column [SORTED WORDS]&"-"&[RowNumberColumn]

@MariusT 

Thanks so much for the link shared. It help me to get the desired output.

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.