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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Elmoataz
New Member

Create a column using M language

Hello, I'm trying to create a column in the power query using the M language. The column that I'm trying to create is New Code column. So I want to add TT at the beginning and then the value that in column Name and then 000 and then the sequence. But I want the sequence to change once the name change as it shows in the exmple below. I also attached the formula that I used in Excel to create New Code column. ="TT"&A2&IFS(COUNTIF(A$2:A2,A2)<=9,"000"&COUNTIF(A$2:A2,A2),COUNTIF(A$2:A2,A2)<=99,"00"&COUNTIF(A$2:A2,A2),COUNTIF(A$2:A2,A2)<=999,"0"&COUNTIF(A$2:A2,A2),COUNTIF(A$2:A2,A2)<=9999,COUNTIF(A$2:A2,A2))

 

NameNew Code
William TTWilliam0001
William TTWilliam0002
William TTWilliam0003
William TTWilliam0004
William TTWilliam0005
Thomas TTThomas0001
Thomas TTThomas0002
Thomas TTThomas0003
Thomas TTThomas0004
Thomas TTThomas0005
Daniel TTDaniel0001
Daniel TTDaniel0002
Daniel TTDaniel0003

 

1 ACCEPTED SOLUTION
danextian
Super User
Super User

Hi @Elmoataz ,

 

You need to group the table by Name and the grouping should be all rows. That will create a column of tables filtered by Name. Then an index column to those tables. Here's a sample M script.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCs/MyclMzFXSUVIICYFyDAwMDJVidXBKGuGTNMYnaYJP0hQsGZKRn5tYDJGDsOHOwSplhFvKGLeUCW4piDNcEvMyU3MgUhA23BlYpYxwSwGdEQsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, #"New Code" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"New Code", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Name"}, {{"Grouped", each _, type table [Name=nullable text, New Code=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Grouped], "Index", 1)),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Index"}, {"Index"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded Custom", "NewCode", each let _index = Text.PadStart( Text.From([Index]), 3, "0")
in "TT" & [Name] & _index, type text),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Grouped", "Index"})
in
    #"Removed Columns"

 

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

2 REPLIES 2
danextian
Super User
Super User

Hi @Elmoataz ,

 

You need to group the table by Name and the grouping should be all rows. That will create a column of tables filtered by Name. Then an index column to those tables. Here's a sample M script.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCs/MyclMzFXSUVIICYFyDAwMDJVidXBKGuGTNMYnaYJP0hQsGZKRn5tYDJGDsOHOwSplhFvKGLeUCW4piDNcEvMyU3MgUhA23BlYpYxwSwGdEQsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, #"New Code" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"New Code", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Name"}, {{"Grouped", each _, type table [Name=nullable text, New Code=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Grouped], "Index", 1)),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Index"}, {"Index"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded Custom", "NewCode", each let _index = Text.PadStart( Text.From([Index]), 3, "0")
in "TT" & [Name] & _index, type text),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Grouped", "Index"})
in
    #"Removed Columns"

 

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Thanks @danextian. I also have another question. Can I use the M script for Sharepoint? Will it work or it's different from power BI. 

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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