Likely the cleanest approach would be to calculate the number of units that must be retracted for each Claim ID + Date of Service, and then assign those retractions to specific claim lines. Since your rule says that any combination of codes is allowed as long as no more than 4 total units remain payable, you do not need to retract entire claim lines. You can retract a specific number of units from one line when that line has enough units to absorb the entire excess.
For your examples, Claim 2343 has 5 total units, so 1 unit must be retracted. Claim 2670 has 7 total units, so 3 units must be retracted. For Adam, if the two lines contain 5 units and 2 units, you can retract all 3 excess units from the 5-unit line. That leaves 2 units on that line plus 2 units on the other line, for a total of 4 payable units. You do not need to highlight both lines.
Assuming your columns are exactly as shown:
A = Claim ID B = Claim line C = Member Name D = Date E = Code F = Billed Units G = Total Units Paid
I would add two new columns:
H = Units to Retract I = Units Payable
The first step is to calculate the units to retract for the entire Claim ID + Date combination. You already have that information in column G, so H can start with a very simple formula:
=MAX(0,G2-4)
For Claim 2343, G is 5, so H returns 1. For Claim 2670, G is 7, so H returns 3.
However, that formula puts the same 1 or 3 on every line belonging to the claim, which is not what you want. We need to determine which specific line should carry the retraction.
If you want to use your Adam example exactly as described, where the 5-unit line absorbs all 3 excess units, my suggestion would be to use this approach. First, identify the excess:
=MAX(0,G2-4)
Then identify whether the current line has enough units to absorb that entire excess. For example, in H2:
=IF(AND(G2>4,F2>=G2-4),G2-4,0)
For Adam, the 5-unit line would receive a retraction of 3, because it has 5 units and the claim has 3 excess units. The 2-unit line would receive 0. The resulting payable units would be 2 + 2 = 4.
However, that formula has a limitation - it works when at least one individual line contains enough units to absorb the entire excess. You have thousands of claim lines, so you should account for cases where the excess cannot be taken from one line.
For example:
Claim total = 7 Line 1 = 2 units Line 2 = 2 units Line 3 = 3 units
The excess is 3, so you could retract all 3 from Line 3.
But:
Claim total = 7 Line 1 = 2 Line 2 = 2 Line 3 = 2 Line 4 = 1
The excess is still 3, but no single line contains 3 units. Therefore, the formula must retract units from multiple lines.
A more robust method is to distribute the retraction automatically based on the order of the claim lines. This should guarantee that exactly 4 units remain payable for every Claim ID + Date.
Add column H called Cumulative Units. In H2 enter:
=SUMIFS($F$2:F2,$A$2:A2,A2,$D$2:D2,D2)
Copy this formula down. This calculates the cumulative billed units for the current Claim ID and Date.
Then add column I called Units to Retract. In I2 enter:
=MIN(F2,MAX(0,H2-4))
Copy it down.
Then add column J called Units Payable. In J2 enter:
=F2-I2
Copy it down.
Using your data, the result would look conceptually like this:
| Claim |
Line |
Billed |
Total Paid |
Cumulative |
Retract |
Payable |
| 2343 |
1 |
1 |
5 |
1 |
0 |
1 |
| -------- |
-------- |
-------- |
-------- |
-------- |
-------- |
-------- |
| 2343 |
1 |
1 |
5 |
1 |
0 |
1 |
| 2343 |
2 |
1 |
5 |
2 |
0 |
1 |
| 2343 |
3 |
1 |
5 |
3 |
0 |
1 |
| 2343 |
4 |
1 |
5 |
4 |
0 |
1 |
| 2343 |
5 |
1 |
5 |
5 |
1 |
0 |
| 2670 |
2 |
5 |
7 |
5 |
1 |
4 |
| 2670 |
4 |
2 |
7 |
7 |
2 |
0 |
Notice that this produces a slightly different result from your proposed Adam example. The formula retracts 1 from the 5-unit line and 2 from the 2-unit line. The result is still exactly 4 payable units.
If you specifically want Excel to take all 3 excess units from Adam's 5-unit line, rather than splitting the retraction, that is also possible. In fact, because you stated that any line can be selected, I would use a "single-line if possible, otherwise distribute" approach. That would identify a line whose billed units are at least equal to the excess and put the entire retraction there. If no individual line can absorb the excess, Excel would distribute the retraction across multiple lines.
I would not highlight every line where Total Units Paid > 4. Column G is a claim/date-level total, so every line for Adam correctly shows 7, but that does not mean every line needs to be retracted. The retraction should be attached to specific units on specific lines.
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
hth
Marcin