Count unique values with sum-frequency array? multiple countifs? or other formulas?

dexiw

New Member
Joined
Dec 31, 2019
Messages
10
Hi everyone,

Sorry if I am overlooking other resources on this forum regarding a similar question.

I am currently using a sum-frequency array formula to calculate how many unique police precincts:
1) have operational interventions in a given year; and
2) see a decrease in burglaries from one year to the next.

Please note the following about the police precincts:
1) it is possible that there could be smaller intervention sites in a police precinct, which could have operational or inoperable interventions at the end of each year; and
2) the # of burglaries might only be reported at the level of a police precinct (which you can think of as the largest police jurisdiction type). This means that smaller intervention sites, in certain cases, won't have any burglary data because the police force can only report for all intervention sites at the level of a police precinct.

The array formula that I have drafted thus far is counting the unique police precincts per year correctly (when that is the only criteria in the formula)... but not the # of unique precincts that see a decrease in burglaries from one year to the next. Instead, the array formula seems to look at the # of burglaries in precincts with interventions for a given year, aggregates the # of burglaries for that year, and then compares that result to the # of burglaries for the next year. However, this does provide the correct result.

I have pasted what the raw data looks like for two police precincts below.

This is the raw data, for Precinct #1:
precinct 1.PNG


This is the raw data, for Precinct #2:
precinct 2.PNG


I am pasting different versions of the array formulas (which are more or less the same from one another) below. The arrays formula looks at all the raw data provided above in order to calculate the result. The word "DCT" in each array formula below is just the name of the tab (which I'm sure that you already knew/could have surmised).

Array #1:
=SUM(--(FREQUENCY(IF((DCT!$C$2:$C$9997<>"")*(DCT!$A$2:$A$9997="CY2016")*(DCT!$E$2:$E$9997="Operational")*(DCT!$F$2:$F$9997>=0)*(DCT!$F$2:$F$9997<>"")*(SUMIFS(DCT!$F$2:$F$9997,DCT!$A$2:$A$9997,"CY2015",DCT!$E$2:$E$9997,"Operational",DCT!$F$2:$F$9997,">=0")>SUMIFS(DCT!$F$2:$F$9997,DCT!$A$2:$A$9997,"CY2016",DCT!$E$2:$E$9997,"Operational",DCT!$F$2:$F$9997,">=0")),MATCH(DCT!$C$2:$C$9997,DCT!$C$2:$C$9997,0)),ROW(DCT!$C$2:$C$9997)-ROW(DCT!$C$2)+1)>0))
Result from Array #1 for CY2016 = 2

Array #3:

=SUM(--(FREQUENCY(IF((DCT!$C$2:$C$9997<>"")*(DCT!$A$2:$A$9997="CY2016")*(DCT!$E$2:$E$9997="Operational")*(DCT!$F$2:$F$9997>=0)*(DCT!$F$2:$F$9997<>"")*(IF(SUMIFS(DCT!$F$2:$F$9997,DCT!$A$2:$A$9997,"CY2015",DCT!$E$2:$E$9997,"Operational",DCT!$F$2:$F$9997,">=0")>SUMIFS(DCT!$F$2:$F$9997,DCT!$A$2:$A$9997,"CY2016",DCT!$E$2:$E$9997,"Operational",DCT!$F$2:$F$9997,">=0"),"1","0")),MATCH(DCT!$C$2:$C$9997,DCT!$C$2:$C$9997,0)),ROW(DCT!$C$2:$C$9997)-ROW(DCT!$C$2)+1)>0))
Result from Array #2 for CY2016 = 2

The CY2016 result for each array formula, based on the raw data, should be 1, not 2. This is because Precinct #2 saw its burglaries decrease from CY2015 to CY2016, whereas Precinct #1 saw its burglaries increase in the same time period.

If it is not possible to have just *one* formula that calculates the result, that's fine. Similarly, if what I need to use is a completely different formula type, please direct me in the right direction.

Thanks!
 

Attachments

  • precinct 1.PNG
    precinct 1.PNG
    42.7 KB · Views: 6
  • precinct 2.PNG
    precinct 2.PNG
    43.5 KB · Views: 6
  • precinct 2.PNG
    precinct 2.PNG
    39.8 KB · Views: 8
  • precinct 1.PNG
    precinct 1.PNG
    41.4 KB · Views: 4
  • precinct 2.PNG
    precinct 2.PNG
    41.4 KB · Views: 6
  • precinct 2.PNG
    precinct 2.PNG
    41.1 KB · Views: 4

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi,

Welcome to MrExcel.
I have two questions:

1. Does the following formula

=IF(SUMIFS(DCT!$F$2:$F$9997,DCT!$A$2:$A$9997,"CY2015",DCT!$E$2:$E$9997,"Operational",DCT!$C$2:$C$9997,"Precinct #1")>SUMIFS(DCT!$F$2:$F$9997,DCT!$A$2:$A$9997,"CY2016",DCT!$E$2:$E$9997,"Operational",DCT!$C$2:$C$9997,"Precinct #1"),"Yes","No")
correctly answer whether Precinct #1 should be included in the count or not?

2. What version of Excel are you using? In particular, does it support UNIQUE function?


Best,
J.Ty.
 
Upvote 0
Hi,

Welcome to MrExcel.
I have two questions:

1. Does the following formula

=IF(SUMIFS(DCT!$F$2:$F$9997,DCT!$A$2:$A$9997,"CY2015",DCT!$E$2:$E$9997,"Operational",DCT!$C$2:$C$9997,"Precinct #1")>SUMIFS(DCT!$F$2:$F$9997,DCT!$A$2:$A$9997,"CY2016",DCT!$E$2:$E$9997,"Operational",DCT!$C$2:$C$9997,"Precinct #1"),"Yes","No")
correctly answer whether Precinct #1 should be included in the count or not?

2. What version of Excel are you using? In particular, does it support UNIQUE function?


Best,
J.Ty.

Hi J.Ty.

Thanks for the support/questions.

1. The formula does include Precinct #1. Are you thinking that that formula, in some fashion, should be included in the array formula?
2. I have Excel 2016 and am using version 1908. It does not support the UNIQUE function, unfortunately, from what I can see.

Thanks again.
 
Upvote 0
Let us assume for the moment that you have a list of all Precincts, located in column A of a new worksheet.
Then the solution is as follows, and is based on the formula from my previous post:
Book1
ABC
1Precinct #1No0
2Precinct #2No
3Precinct #3No
Analysis
Cell Formulas
RangeFormula
C1C1=COUNTIF(B1:B3,"Yes")
B1:B3B1=IF(SUMIFS(DCT!$F$2:$F$9997,DCT!$A$2:$A$9997,"CY2015",DCT!$E$2:$E$9997,"Operational",DCT!$C$2:$C$9997,A1)>SUMIFS(DCT!$F$2:$F$9997,DCT!$A$2:$A$9997,"CY2016",DCT!$E$2:$E$9997,"Operational",DCT!$C$2:$C$9997,A1),"Yes","No")


Now, about the list. It can be created in Excel 2016 in the following way: copy column C of your DCT worksheet and paste it into a new worksheet. Keep it selected and go to Ribbon->Data->Remove duplicates. It will create the list you need. Please tell me if this solves your problem.

If you cannot or do not want to rely on this manual method, I can tell you another, more complex solution to compute what you need.

J.Ty.
 
Upvote 0
Let us assume for the moment that you have a list of all Precincts, located in column A of a new worksheet.
Then the solution is as follows, and is based on the formula from my previous post:
Book1
ABC
1Precinct #1No0
2Precinct #2No
3Precinct #3No
Analysis
Cell Formulas
RangeFormula
C1C1=COUNTIF(B1:B3,"Yes")
B1:B3B1=IF(SUMIFS(DCT!$F$2:$F$9997,DCT!$A$2:$A$9997,"CY2015",DCT!$E$2:$E$9997,"Operational",DCT!$C$2:$C$9997,A1)>SUMIFS(DCT!$F$2:$F$9997,DCT!$A$2:$A$9997,"CY2016",DCT!$E$2:$E$9997,"Operational",DCT!$C$2:$C$9997,A1),"Yes","No")


Now, about the list. It can be created in Excel 2016 in the following way: copy column C of your DCT worksheet and paste it into a new worksheet. Keep it selected and go to Ribbon->Data->Remove duplicates. It will create the list you need. Please tell me if this solves your problem.

If you cannot or do not want to rely on this manual method, I can tell you another, more complex solution to compute what you need.

J.Ty.

Hi J.Ty.

Thanks for that.

To my understanding, most of the precincts stay fairly constant over time and across different localities. However, I am wary of using a list when there could be unforeseen changes due to political demands/pressure to change any of the precincts in their current form from one year to the next.

In that case, could you provide a more complex recommendation for the formula?

Thanks again.
 
Upvote 0
In this case you can automatically determine in each row if you have seen that Precinct already. You can do it at as shown below. The additional column can be added to the right of your raw data. Then you compute the burglaries only for the first occurrence of each Precinct.

Book1
ABCD
1Precinct #81No0
2Precinct #71No
3Precinct #11No
4Precinct #41No
5Precinct #82 
6Precinct #101No
7Precinct #61No
8Precinct #12 
9Precinct #102 
10Precinct #13 
11Precinct #91No
Analysis
Cell Formulas
RangeFormula
B1, B2:B11B1=COUNTIF($A$1:$A1,A1)
C1, C2:C11C1=IF(B1=1,IF(SUMIFS(DCT!$F$2:$F$9997,DCT!$A$2:$A$9997,"CY2015",DCT!$E$2:$E$9997,"Operational",DCT!$C$2:$C$9997,A1)>SUMIFS(DCT!$F$2:$F$9997,DCT!$A$2:$A$9997,"CY2016",DCT!$E$2:$E$9997,"Operational",DCT!$C$2:$C$9997,A1),"Yes","No"),"")
D1D1=COUNTIF(C1:C3,"Yes")


It might be slow, but my Excel is different and I do not have the data, so my tests are not likely to demonstrate how it behaves on your machine. I can think of a method to make it faster.

J.Ty.
 
Upvote 0
Sorry, COUNTIF in D1 should apply to C1:C9997.

J.Ty.
 
Upvote 0
Hi J.Ty.

Thanks for the information.

The problem that I am encountering with your suggestion (if I am understanding its implementation correctly) is that, if I were to use your formula from Column C with my own data and modify/paste it in another column for years 2016 and 2017, the results are incorrect. For example, when I modified your formula in Column C for my data but for years 2016 and 2017, the results in that new column were the exact same as the results for 2015 and 2016 (similar to your Column B above).

My suspicion (and correct me if I'm wrong) is that the formula in Column B also needs to test whether the years are {"Year 1" or "Year 2"}. If the variable Years was Column E in your table above, perhaps modifying the formula to something like the following (array formula) would help: {=SUM(COUNTIFS($A$1:$A1,A1,$E1,{"CY2015","CY2016"}))}. However, while the first cell with this modified array formula showed the correct result of 1, I got a value error for each cell after the first row. I'm not quite sure why the above modified array formula didn't work.

Any thoughts/recommendations you have to modify the formula for Column B (and/or Column C) would be appreciated.
 
Upvote 0
How is your data organized? Do you have a common worksheet with all historical data in it, identified by "CY20XX" tags?

J.Ty.
 
Upvote 0
Hi J.Ty.

Yes, all data will be in the same tab together. There will be a Years variable with the specific year per row/precinct.
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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