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
Anonymous
Not applicable

COUNTIF in PowerQuery

Hello,

 

I am working on a table in which I need to return the number of direct reports for each employee listed in my data table. See example below - [Number of Direct Reports] is the custom column I am looking to create:

 

ABC
Employee IDManager Employee IDNumber of Direct Reports
0010101
002

010

0
0030100
0100203
0040010

 

The formula in excel for this would be as follows:

=COUNTIF($B$2:$B$5,$A2)

 

How can I achieve this as a custom column in PowerQuery? I've tried Grouping the [Manager Employee ID] column to get a count of each Manager ID, but I need the criteria to be based on the Employee ID.

 

Thanks!

 

2 REPLIES 2
Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


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

Hi @Anonymous ,

 

I would preferrably  do this with DAX instead of Power Query as this can get very slow with large tables but here's a sample M-script

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjAwVNJRMjA0UIrVAfGMUHjGyDwgDeQZweRMQDyg7thYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Employee ID" = _t, #"Manager Employee ID" = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Source", each let 
mgr = Source[Manager Employee ID],
x = [Employee ID]
in List.Count( List.Select(mgr, each _ = x) ), Int64.Type),
    #"Renamed Columns" = Table.RenameColumns(#"Added Custom",{{"Source", "Number of Records"}})
in
    #"Renamed Columns"

danextian_0-1656030763532.png

in DAX:

Number of Records (DAX calc column) = 
CALCULATE (
    COUNTROWS ( Table_ ),
    FILTER (
        ALL ( Table_ ),
        Table_[Manager Employee ID] = EARLIER ( Table_[Employee ID] )
    )
) + 0

danextian_1-1656031148104.png

 





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.

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 Solution Authors