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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

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
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.