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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Hadi14
Regular Visitor

countrow in power query whitout help table in M language

I have a table that contains national codes. I want to add a column to this table without an auxiliary table that displays the number of occurrences of each national code.

1 ACCEPTED SOLUTION

= Table.AddColumn(Source, "Count", each List.Count(List.Select(Source[code],(x)=>x=[code])))

 

p45cal_0-1735386892471.png

 

To paste into Advanced Editor:

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSszJVNJRMlKK1YlWKkqtSgRyjMGcrPyMPLhMcWJRIoQTCwA=", BinaryEncoding.Base64), Compression.Deflate)),{"name","code"}),
AddCustom = Table.AddColumn(Source, "Count", each List.Count(List.Select(Source[code],(x)=>x=[code])))
in
AddCustom

 

 

View solution in original post

6 REPLIES 6
Omid_Motamedise
Super User
Super User

Hi Hadi Jan,

TO solve this problem, right click on the code column and select group by by the below setting.

Omid_Motamedise_0-1735439584346.png

 

Then exapnd the all row column. below is the full solution

 

 

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSszJVNJRMlKK1YlWKkqtSgRyjMGcrPyMPLhMcWJRIoQTCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [name = _t, code = _t]),
    #"Grouped Rows" = Table.Group(Source, {"code"}, {{"Rows", each _, type table [name=nullable text, code=nullable text]}, {"Count", each Table.RowCount(_), Int64.Type}}),
    #"Expanded Rows" = Table.ExpandTableColumn(#"Grouped Rows", "Rows", {"name"}, {"name"})
in
    #"Expanded Rows"

 

 


If my answer helped solve your issue, please consider marking it as the accepted solution.
dufoq3
Super User
Super User

Hi @Hadi14, another solution:

 

Output

dufoq3_0-1735394838838.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSszJVNJRMlKK1YlWKkqtSgRyjMGcrPyMPLhMcWJRIoQTCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [name = _t, code = _t]),
    CodeCount = [ codes = List.Buffer(Source[code]),
    occur = List.Transform(List.Distinct(codes), (x)=> {x, List.Count(List.Select(codes, (y)=> y = x))}),
    tbl = Table.FromRows(occur, type table[code=Int64.Type, count=Int64.Type])
  ][tbl],
    Ad_Count = Table.Join(Source, "code", CodeCount, "code")
in
    Ad_Count

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

Chewdata
Super User
Super User

I am not really sure what your question is. But let's say you have the following data:

Chewdata_0-1735383098211.png

You can select both columns, right click and choose group by:

Chewdata_1-1735383173679.png

It then standard gives you a count of the rows, counting how often each combination of name and code.

Chewdata_2-1735383211983.png

 

result:

Chewdata_3-1735383258524.png


the code:

let
    Source = YOURDATA,
    #"Grouped Rows" = Table.Group(Source, {"name", "code"}, {{"Count", each Table.RowCount(_), Int64.Type}})
in
    #"Grouped Rows"

 



Hadi14
Regular Visitor

yes

 

namecode
ali2
reza3
john2
sara2

 

result:

namecode 
ali23
reza31
john23
sara23

= Table.AddColumn(Source, "Count", each List.Count(List.Select(Source[code],(x)=>x=[code])))

 

p45cal_0-1735386892471.png

 

To paste into Advanced Editor:

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSszJVNJRMlKK1YlWKkqtSgRyjMGcrPyMPLhMcWJRIoQTCwA=", BinaryEncoding.Base64), Compression.Deflate)),{"name","code"}),
AddCustom = Table.AddColumn(Source, "Count", each List.Count(List.Select(Source[code],(x)=>x=[code])))
in
AddCustom

 

 

Chewdata
Super User
Super User

Hey,

Can you provide a workable sample of your data (not a screenshot). Please do not share sensative data.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors