Spreadsheet: 875442d1722004783-formula-to-sum-column-based-on-several-other-columns-countdistinctrecordsformula-v2-addedsumformula-
Sheet: Sheet1

Column H has correct formula to find unique ID based on Source, Error and YearMonth |  |  |  |  |  |  |  | ***NEW Formula needed (column G) which I believe will need to piggy back off of "The Correct Formula"
 | Requirements |  |  |  |  |  |  | Requirements
 |  | Source must = P |  |  |  |  |  | 1. Sum column "Deducts"
 |  | Error = 1 |  |  |  |  |  | 2. having the same ID #
 |  | then count distinct ID within the same YearMonth |  |  |  |  |  | 3. and having the same YearMonth value
Doesn't matter if the first or the last record is the one that is "counted", but only 1 can be counted |  |  |  |  |  |  |  | 4. The SUM must show on the same row as "The Correct Formula" where this value is 1 ("The Correct Formula" already counts the Unique ID)
The data is "generally" input in somewhat of a chronological order, but it's not always.  YearMonth dates are generally in order
The data is input into a table, has 1000's of rows, with new rows added every day.  When I input the "Desired Distinct Count" formula, I will need the table to automatically insert the new formula into the new ID row (Excel usually does this automatically)
(you can ignore the A-B-E-S values in this row, this is for my cross-reference back to my original spreadsheet)
A | B | E | S |  |  |  | Columns H, I & J go together
ID | Source | Error | YearMonth | Deducts | Desired Result for Column G | NEW Formula Needed | The Correct Formula | Comments (these comments are only for "The Correct Formula" | Desired Result for Column H
22 | A | 0 | 202404 | 0 | 0 |  |  | source isn't P | 0
19 | B | 1 | 202404 | 0 | 0 |  |  | source isn't P | 0
33 | C | 0 | 202404 | 0 | 0 |  |  | source isn't P | 0
41 | P | 1 | 202404 | 5 | 5 |  |  | count of ID 41, where Source = P, Error = 1, for the YearMonth=202404 | 1
57 | A | 0 | 202405 | 0 | 0 |  |  | source isn't P | 0
66 | P | 1 | 202405 | 5 | 15 |  |  | count of ID 66, where Source = P, Error = 1, for the YearMonth=202405 | 1
66 | P | 1 | 202405 | 10 | 0 |  |  | doesn't count since ID 66 above was already accounted for | 0
17 | B | 0 | 202406 | 0 | 0 |  |  | source isn't P | 0
41 | P | 1 | 202407 | 10 | 10 |  |  | even though this is the same ID (41) as above, it still counts as 1 since the YearMonth is different than the one above | 1
88 | C | 0 | 202407 | 0 | 0 |  |  | source isn't P | 0
19 | P | 0 | 202407 | 0 | 0 |  |  | doesn't count since Error=0 | 0
110 | A | 1 | 202407 | 0 | 0 |  |  | source isn't P | 0
36 | P | 0 | 202407 | 0 | 0 |  |  | doesn't count since Error=0 | 0
120 | P | 1 | 202407 | 10 | 20 |  |  | count of ID 120, where Source = P, Error = 1, for the YearMonth=202407 | 1
73 | B | 1 | 202407 | 0 | 0 |  |  | source isn't P | 0
120 | P | 1 | 202407 | 10 | 0 |  |  | doesn't count since ID 120 above was already accounted for | 0
79 | P | 0 | 202407 | 0 | 0 |  |  | doesn't count since Error=0 | 0
52 | A | 1 | 202407 | 0 | 0 |  |  | source isn't P | 0
66 | P | 1 | 202407 | 5 | 5 |  |  | even though this is the same ID (66) as above, it still counts as 1 since the YearMonth is different than the one above | 1
58 | C | 0 | 202407 | 0 | 0 |  |  | source isn't P | 0
33 | P | 1 | 202404 | 10 | 10 |  |  | count of ID 33, where Source = P, Error = 1, for the YearMonth=202404 (notice the ID=33 on Row 13 which is Source = C, but regardless, 33 here counts as 1 because Source=P, Error=1, and it's the only record for that month | 1
48 | A | 1 | 202405 | 0 | 0 |  |  |  | 0
48 | P | 1 | 202405 | 10 | 20 |  |  | one of these 2 records s/b 1 | 1
48 | P | 1 | 202405 | 10 | 0 |  |  | one of these 2 records s/b 1 | 0