Complex Unique Count - Is This Possible?

AndyPandy27

Board Regular
Joined
Jul 17, 2012
Messages
142
Hi All,

I'm ideally looking for a one-cell solution, happy with a formula or VBA function (it needs to be able to update as the background data updates though).

I have a data set in the background similar to the below (assume "Name" is in Cell A1):

NameAccountItemGroupSales
Person AAccount 1Item 1Group 1$1,500
Person AAccount 2Item 1Group 1$500
Person AAccount 1Item 2Group 2$500
Person AAccount 2Item 2Group 2$500
Person AAccount 3Item 1Group 1$2,500
Person AAccount 1Item 3Group 1$1,500
Person AAccount 2Item 3Group 1$2,500
Person BAccount 4Item 1Group 1$500
Person BAccount 5Item 1Group 1$700
Person BAccount 4Item 2Group 2$500
Person BAccount 5Item 2Group 2$500
Person BAccount 4Item 3Group 1$500
Person BAccount 4Item 4Group 2$2,500
Person BAccount 5Item 1Group 1$1,500

<tbody>
</tbody>

(The above would be updated on a monthly basis; it would remain in the same format, but the values and the number of rows would change.)

There is then a separate worksheet, which contains a data validation drop-down, allowing a user to select a "Name".

I need to be able to calculate the unique number of Accounts, linked to that selected Name, which have total sales for Group 1 >$2,000.

Using the above example, if a user had selected Person A, the result would be: 3 (the 2 x orange font lines for Account 1, the 2 x blue font lines above for Account 2 and the 1 x red font line above for Account 3).

Is there a way to perform the aggregation of sales at Name/Account/Group level, then calculate the number of those aggregations which meet a set criterion (in this case >$2,000)...?

Any help greatly appreciated.

AP
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi Aladin,



That is correct.

Thanks,

AP

~
Row\Col
A​
B​
C​
D​
E​
F​
G​
1​
NameAccountItemGroupSalesperson A
2​
Person AAccount 1Item 1Group 1
$1,500​
2000​
3​
Person AAccount 2Item 1Group 1
$500​
3​
4​
Person AAccount 1Item 2Group 2
$500​
5​
Person AAccount 2Item 2Group 2
$500​
6​
Person AAccount 3Item 1Group 1
$2,500​
7​
Person AAccount 1Item 3Group 1
$1,500​
8​
Person AAccount 2Item 3Group 1
$2,500​
9​
Person BAccount 4Item 1Group 1
$500​
10​
Person BAccount 5Item 1Group 1
$700​
11​
Person BAccount 4Item 2Group 2
$500​
12​
Person BAccount 5Item 2Group 2
$500​
13​
Person BAccount 4Item 3Group 1
$500​
14​
Person BAccount 4Item 4Group 2
$2,500​
15​
Person BAccount 5Item 1Group 1
$1,500​

In G3 control+shift+enter, not just enter:
Rich (BB code):

=SUM(IF(FREQUENCY(IF($A$2:$A$15=G$1,IF(1-($B$2:$B$15=""),
    IF(SUMIFS($E$2:$E$15,$A$2:$A$15,$A$2:$A$15)>G$2,
    MATCH($B$2:$B$15,$B$2:$B$15,0)))),ROW($B$2:$B$15)-ROW($B$2)+1),1))
<strike></strike>
 
Upvote 0
WOW! I was thinking something with Frequency, but I am not as familiar with it. Aladin, that is awesome. I knew you would figure it out.
 
Upvote 0
Hi Aladin,

This is an awesome formula - thank you so much for taking the time; however, it does not take into consideration the Group element, which is part of the criteria.

This is *so* close; I just need to ensure that the relevant Group (in this case Group 1), is also considered within the calculation.

The steps would be:

1. Create a sum of just the Group 1 sales for each Account, for the chosen Name.
2. Count the number of the sums from Step 1 (above), which are >$2000.

So using the above table for Person A and Group 1, you would get the following totals for each Account belonging to Person A (this would be Step 1):

£3,000 (for Account 1); £3,000 (for Account 2); £2,500 (for Account 3)

Then, you would run through these totals, and count how many were >$2000 (Step 2) - which gives you the answer we're looking for: 3

Can that be done in a single formula?

Thanks again.
 
Upvote 0
Rich (BB code):
=SUM(IF(FREQUENCY(IF($A$2:$A$15=G$1,IF(1-($B$2:$B$15=""),
    IF(SUMIFS($E$2:$E$15,$A$2:$A$15,$A$2:$A$15)>G$2,
    MATCH($B$2:$B$15,$B$2:$B$15,0)))),ROW($B$2:$B$15)-ROW($B$2)+1),1))
<strike></strike>

I think the logic is flawed here, since one of the conditional statements (IF($A$2:$A$15=G$1) is outside the SUMIFS. For example, for this dataset:


Row\Col
A​
B​
C​
D​
E​
F​
G​
1​
NameAccountItemGroupSalesperson A
2​
Person AAccount 2Item 1Group 2
$1,500​
2000​
3​
Person BAccount 5Item 1Group 1
$500​
4​
4​
Person BAccount 3Item 2Group 1
$500​
5​
Person BAccount 4Item 2Group 1
$500​
6​
Person BAccount 5Item 1Group 2
$2,500​
7​
Person BAccount 5Item 3Group 2
$1,500​
8​
Person BAccount 3Item 3Group 1
$2,500​
9​
Person AAccount 5Item 1Group 2
$500​
10​
Person AAccount 3Item 1Group 2
$700​
11​
Person AAccount 3Item 2Group 1
$500​
12​
Person AAccount 1Item 2Group 1
$500​
13​
Person BAccount 4Item 3Group 2
$500​
14​
Person BAccount 4Item 4Group 1
$2,500​
15​
Person BAccount 5Item 1Group 1
$1,500​

<tbody>
</tbody>


your formula returns 4, though the correct result, I believe, should be 0.

Regards
 
Upvote 0
Hi.

With your table in A1:E15 (with headers in row 1), your person (e.g. "Person A") in G1, your group (e.g. "Group 1") in H1 and your value (e.g. 2000) in I1, array formula**:

=SUM(IF(FREQUENCY(IF(SUMIFS(E2:E15,A2:A15,G1,B2:B15,B2:B15,D2:D15,H1)>I1,MATCH(B2:B15,B2:B15,0)),ROW(B2:B15)-MIN(ROW(B2:B15))+1),1))

Regards


**Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).
 
Upvote 0
Hi Aladin,

This is an awesome formula - thank you so much for taking the time; however, it does not take into consideration the Group element, which is part of the criteria.

This is *so* close; I just need to ensure that the relevant Group (in this case Group 1), is also considered within the calculation.

The steps would be:

1. Create a sum of just the Group 1 sales for each Account, for the chosen Name.
2. Count the number of the sums from Step 1 (above), which are >$2000.

So using the above table for Person A and Group 1, you would get the following totals for each Account belonging to Person A (this would be Step 1):

£3,000 (for Account 1); £3,000 (for Account 2); £2,500 (for Account 3)

Then, you would run through these totals, and count how many were >$2000 (Step 2) - which gives you the answer we're looking for: 3

Can that be done in a single formula?

Thanks again.

Row\Col
A​
B​
C​
D​
E​
F​
G​
1​
NameAccountItemGroupSalesperson A
2​
Person AAccount 1Item 1Group 1
$1,500​
2000​
3​
Person AAccount 2Item 1Group 1
$500​
Group 1
4​
Person AAccount 1Item 2Group 2
$500​
3​
5​
Person AAccount 2Item 2Group 2
$500​
6​
Person AAccount 3Item 1Group 1
$2,500​
7​
Person AAccount 1Item 3Group 1
$1,500​
8​
Person AAccount 2Item 3Group 1
$2,500​
9​
Person BAccount 4Item 1Group 1
$500​
10​
Person BAccount 5Item 1Group 1
$700​
11​
Person BAccount 4Item 2Group 2
$500​
12​
Person BAccount 5Item 2Group 2
$500​
13​
Person BAccount 4Item 3Group 1
$500​
14​
Person BAccount 4Item 4Group 2
$2,500​
15​
Person BAccount 5Item 1Group 1
$1,500​

In G4 control+shift+enter, not just enter:
Rich (BB code):

=SUM(IF(FREQUENCY(IF($A$2:$A$15=G$1,IF(1-($B$2:$B$15=""),
    IF($D$2:$D$15=G$3,IF(SUMIFS($E$2:$E$15,$A$2:$A$15,A2:A15)>G$2,
    MATCH($B$2:$B$15,$B$2:$B$15,0))))),ROW($B$2:$B$15)-ROW($B$2)+1),1))
<strike></strike>
 
Upvote 0
Hi Guys,

@XOR LX - you were exactly right, and your solution is working flawlessly. Thank you very much indeed. Really neat formula - a fantastic help.

@Aladin - thank you for getting back to me again (and so quickly). Your updated solution works perfectly for the original dataset, however, when I update any of the "input" values (e.g. the Group, or the cut-off value of $2000), the formula no longer returns the correct result...? I'm not sure if that is down to me or not, plus I appreciate I did not mention anything about these "input" values changing, so perhaps your solution does not take that into consideration. That being said, XOR LX's formula does continue to update the result to be correct even when certain elements change (e.g. when using the amended data table he (or she) posted, or if I were to change the cut-off value from $2000 to $400).

Regardless, I really appreciate all the help and the incredible responsiveness of everyone.

Thank you very much indeed,

AP
 
Upvote 0
Hi Guys,


@Aladin - thank you for getting back to me again (and so quickly). Your updated solution works perfectly for the original dataset, however, when I update any of the "input" values (e.g. the Group, or the cut-off value of $2000), the formula no longer returns the correct result...? I'm not sure if that is down to me or not, plus I appreciate I did not mention anything about these "input" values changing, so perhaps your solution does not take that into consideration.[...]

I failed to update the SumIfs bit with the additional condition...

=SUM(IF(FREQUENCY(IF($A$2:$A$15=G$1,IF(1-($B$2:$B$15=""),IF($D$2:$D$15=G$3,IF(SUMIFS($E$2:$E$15,$D$2:$D$15,G$3,$A$2:$A$15,A2:A15)>G$2,MATCH($B$2:$B$15,$B$2:$B$15,0))))),ROW($B$2:$B$15)-ROW($B$2)+1),1)
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,822
Members
449,469
Latest member
Kingwi11y

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