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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
AntonG
Frequent Visitor

COUNTIF in Query Editor

Good Day,

 

What simplest formula is the equivalent for Excel Countif(s) functions in Power BI Desktop - QueryEditor - M language ? 

 

I would like to add "Custom Column" to the larger table and specify column formula. I tried proposed solution using Transform - Group By, but the solution is not feasible for larger tables with multiple columns because Table.Group would finally drop unused columns and transform the table to the specified columns only.

 

 

Thank you

Anton

1 ACCEPTED SOLUTION
MFelix
Super User
Super User

Hi @AntonG ,

 

You need to use the group column option, you will then get the result you refer of only one colum and then you should do a merge with the previous steps to have that addtional column on your data table:

 

coutn_rows.png

 

Check M code below:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTI0MDAAUlFKsToQAVMwPxLMdwKyjMH8CDjfBMwPh/PNwPwwMN8ZyDIH80OVYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Cat = _t, Value = _t, Type = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Cat", type text}, {"Value", Int64.Type}, {"Type", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Cat"}, {{"Count", each Table.RowCount(_), type number}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type",{"Cat"},#"Grouped Rows",{"Cat"},"Grouped Rows",JoinKind.LeftOuter),
    #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"Count"}, {"Count"})
in
    #"Expanded Grouped Rows"

Sorry for making the question but just want to help immprove your results and model.

Why are you adding this additional column to your model?

This will make you model bigger and if the numbers are used in the visuals due to duplication of values you can have wrong information.

 

Regards,

MFelix


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

2 REPLIES 2
MFelix
Super User
Super User

Hi @AntonG ,

 

You need to use the group column option, you will then get the result you refer of only one colum and then you should do a merge with the previous steps to have that addtional column on your data table:

 

coutn_rows.png

 

Check M code below:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTI0MDAAUlFKsToQAVMwPxLMdwKyjMH8CDjfBMwPh/PNwPwwMN8ZyDIH80OVYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Cat = _t, Value = _t, Type = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Cat", type text}, {"Value", Int64.Type}, {"Type", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Cat"}, {{"Count", each Table.RowCount(_), type number}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type",{"Cat"},#"Grouped Rows",{"Cat"},"Grouped Rows",JoinKind.LeftOuter),
    #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"Count"}, {"Count"})
in
    #"Expanded Grouped Rows"

Sorry for making the question but just want to help immprove your results and model.

Why are you adding this additional column to your model?

This will make you model bigger and if the numbers are used in the visuals due to duplication of values you can have wrong information.

 

Regards,

MFelix


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



AntonG
Frequent Visitor

Hi MFelix,

 

appreciate your explanation and comments, thank you for asking detailing question, I have next data quiz to solve with CountIf. Basically I have repeatable invoice value coming in reports from one of the suppliers, and because other suppliers are reporting Line value and I'm appending data into a bigger table, I think I have no other choice other than do data manipulation as shown below

 

1ABCD
2INVOICENOINVOICEVALUEINVOICENCLINEVALUE
3  =COUNTIF(A:A,A3)=B3/C3
45553877819604.429802.2
55553877819604.429802.2
65555486613683456
75555486613683456
85555486613683456

 

Anton

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.