Replicate COUNTIFS in Power Query ("M")

Veritan

Active Member
Joined
Jun 21, 2016
Messages
383
Hi all,

I've searched around a bunch, but have so far been unable to find a solution. I am trying to replicate the COUNTIFS formula in the Power Query M language. I've gone through several articles, and I keep running into some kind of roadblock with each one. My goal is to replicate the results in the CountIfs column below, but do so using Power Query instead of a formula. Does anyone know how to accomplish this? The best search result that I've found so far was at StackOverflow, but it seems to only be applicable to COUNTIF, and not to COUNTIFS. I would prefer to keep the Company and Subledger data types as Integers, but I'm willing to convert them to text if that is necessary. Any suggestions are greatly appreciated!

Sample Data Set:
ICO Remeasurement Tool Test.xlsm
ABCD
1Object AccountCompanySubledgerCountIfs
21240.1551704
31240.1553154
41240.1553854
51240.1554214
61240.1701552
71240.1702402
81240.2401704
91240.2403154
101240.2403854
111240.2404214
121240.3123140
131240.3143152
141240.3151553
151240.3152403
161240.3153143
171240.3851552
181240.3852402
191240.4211552
201240.4212402
211241.16018511
221241.16019511
231241.16019611
241241.16020511
251241.16021011
261241.16029111
271241.16030011
281241.16030111
291241.16030511
301241.16031011
311241.16037511
321241.16041011
331241.1722402
341241.1722452
351241.1851602
361241.1853122
371241.1951602
381241.1953122
391241.1961601
401241.2051602
411241.2052452
421241.2101602
431241.2103122
441241.2401722
451241.2403862
461241.2451723
471241.2452053
481241.2453863
491241.2911602
501241.2913122
511241.3001602
521241.3003122
531241.3051602
541241.3053122
551241.3101602
561241.3103122
571241.3121859
581241.3121959
591241.3122109
601241.3122919
611241.3123009
621241.3123059
631241.3123109
641241.3124109
651241.3153120
661241.3751601
671241.3862402
681241.3862452
691241.4101602
701241.4103122
Raw Data Table (2)
Cell Formulas
RangeFormula
D2:D70D2=COUNTIFS([Object Account],[@[Object Account]],[Subledger],[@Company])
 
Last edited:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Power Query:
= Table.AddColumn(Source, "Countif", each Table.RowCount(Table.SelectRows(Source, (s)=> (s[Object Account] = _[Object Account]) and (s[Company] = _[Subledger]))))
 
Upvote 0
Solution
Thanks for the response, JGordon. However, I don't think it's working correctly. I've checked the results and they're different than what are showing in my CountIfs column. Do you know what might be causing that? This looks really close, just not quite what I need.
 
Upvote 0
Actually, that worked perfectly. All I had to do was switch the Subledger and Company fields and it gave me exactly what I needed. Thank you!!
 
Upvote 0

Forum statistics

Threads
1,215,151
Messages
6,123,319
Members
449,094
Latest member
Chestertim

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top