Counting recurrence based on 2 criterias

rniculae

New Member
Joined
Apr 9, 2013
Messages
41
Hello guys,

I am struggling with a formula, I cannot think of a way to create it in a way where it doesn't need any further manual intervention.
Tried with a pivot table but I can't seem to make GETPIVOTDATA to work.

Can you please help me out?

I have a database with Ref number/ week number / country / number

[TABLE="class: grid, width: 5, align: left"]
<tbody>[TR]
[TD]Table-1[/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Country[/TD]
[TD]Ref Number[/TD]
[TD]Week Number[/TD]
[TD]#[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]UK[/TD]
[TD]123[/TD]
[TD]W26[/TD]
[TD]12[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Germany[/TD]
[TD]445[/TD]
[TD]W26[/TD]
[TD]-12[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]UK[/TD]
[TD]123[/TD]
[TD]W27[/TD]
[TD]12[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Netherlands[/TD]
[TD]555[/TD]
[TD]W27[/TD]
[TD]-12[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]UK[/TD]
[TD]342[/TD]
[TD]W28[/TD]
[TD]12[/TD]
[/TR]
</tbody>[/TABLE]













So I can have multiple countries with multiple ref numbers and in different weeks and on column D I can have only 12 or -12.


[TABLE="class: grid, width: 5, align: left"]
<tbody>[TR]
[TD]Table-2[/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]UK[/TD]
[TD]W26[/TD]
[TD]W27[/TD]
[TD]W28[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]W26[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]W27[/TD]
[TD][/TD]
[TD]1[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]W28[/TD]
[TD][/TD]
[TD][/TD]
[TD]1[/TD]
[/TR]
</tbody>[/TABLE]









The Idea is to get a count based on week number how many ref numbers are in a recurrence in the following weeks. (E.G. Table 1 Cell B2 is in week 26 and it has a recurrence in week 27 also but in week 28 ref number 123 doesn't show up anymore so that's why it is 0 in table 2 cell D2.

In table 2 Cell B2,C3,D4 shows how many ref numbers do we have new in that week.

Can I get a formula that applies to Table 2 based on Table 1 database considering that I will need the formula to expand to week 52 on rows and columns and everything is based on country level?

Right now I am doing a formula with countifs(Table1!A:A,Table2!A1,Table1!C:C,Table2!B1,Table1!C:C,Table2!,C1) This is the formula for table 2 cell C2 and for further weeks I need to keep adding ,Table2!,D1 // ,Table2!,E1 and so on.

Thank you.
Razvan
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hey guys,

Any help with it? I kept trying to create this formula and I don't have any good results.

Thanks,
Razvan
 
Upvote 0
See if this, copied across and down does what you want. If not, post more sample data where it fails and also give us the expected results again.

Excel Workbook
ABCD
1CountryRef NumberWeek Number#
2UK123W2612
3Germany445W26-12
4UK123W2712
5Netherlands555W27-12
6UK342W2812
7
8
9UKW26W27W28
10W26110
11W2710
12W281
Count Recurrence
 
Upvote 0
Hello Peter,

Thank you for trying but it doesn't work for me.

I will put here the whole file. https://we.tl/t-rnrYO9A4Dx - wetransfer link

So, for example, I have in my file - sheet Categories cell C2 59 (this means that I had 59 items with +/- 12 in week 26 then out of those 59 items, in week 27 we have a remainder of 33 and so on.

The results are shown in Categories Sheet.
I would like from this formula to apply to all weeks without needing to add another column (as you see in my formulas).



I have hidden rows on Categories sheet to hide unfilled rows. For those I would want this formula to work and as soon as I add new data in DB sheet, this should be updated automatically.



Is it doable?

Thanks,
Razvan
 
Upvote 0
A bit more clarification please. In 'Detailed' ..

1. Confirm that 12, -12 are blank are the only values that will appear in columns F onwards.

2. In your sample, no rows in F onwards contain both 12 and -12. Is that always the case? If there can be a mix of 12 and -12 on a single row, how does that affect the results?

3. In your sample, no rows in F onwards have a 'break' between the non-blank values. Is that always the case or could a single row look like this?
blank 12 12 blank blank 12 12 12 blank
 
Upvote 0
1. In my database I will always have only 12 or -12.
2. My results are not affected by 12 or -12, I just need the counting. This means that if it is easier for you you can use count instead of sum for the values so it will show up only "1" instead of 12 or -12.
3. The item that has the value of 12, it will be 12 until the recurrence stops. In a very unlikely event, after 12 weeks of blank, it can show up again as 12 or -12 ( this should be very very rare, maybe 1 in 52 weeks.) I can live without it.

Thank you!
Razvan
 
Upvote 0
Thanks for the clarifications. Re point 3, if you do get the 12/-12 reappearing for a second sequence after some blanks, I suspect my formulas will fail - I haven't been able to test.
Otherwise, see if this helps.

To keep the formulas a bit simpler I have inserted a new row 1 in 'Categories' and populated it manually as shown. That row could then be hidden. We could do without this helper row but all the formulas would be a bit more complicated.

To enter all the required formulas you would no doubt have to unhide all the rows in 'Categories'

For the top section (Total) I have drawn the results from the lower section of 'Categories' rather than reverting back to 'Detailed'
The formula in C3 is copied and Paste Special (Formulas) to the other populated cells in that section.

For 'Cereal' the formula in C32 is copied and Paste Special (Formulas) to the other populated cells in that section.

Copy/Paste the C32 formula to C61 and then change the reference from $A$31 to $A$60. C61 can then be copied & Paste Special (Formulas) to the other relevant cells in the Snakcks1 section.

Repeat for other sections.

We could avoid having to do the blue part above but the formulas would all again become a bit more complicated.


Book1
ABCDEFGHIJK
1123456789
2TotalW26W27W28W29W30W31W32W33W34
3W26593323151196
4W2741282015129
5W283728181411
6W2940261815
7W30362720
8W313123
9W3238
30
31CerealW26W27W28W29W30W31W32W33W34
32W2641201410875
33W2725181411108
34W28231812108
35W2924171311
36W30211613
37W311915
38W3217
59
60Snacks1W26W27W28W29W30W31W32W33W34
61W26131064321
62W271275421
63W28108643
64W2911743
65W301185
66W3185
67W3212
88
89Cereal BarsW26W27W28W29W30W31W32W33W34
90W260000000
91W27100000
92W2811000
93W291000
94W30000
95W3100
96W321
117
118Snacks2W26W27W28W29W30W31W32W33W34
119W265331000
120W27331000
121W2831000
122W294211
123W30432
124W3143
125W328
Categories
Cell Formulas
RangeFormula
C3=SUMIF($B$31:$B$200,$B3,C$31:C$200)
C32=SUMPRODUCT(--(Detailed!$A$3:$A$300=$A$31),--(INDEX(Detailed!$F$3:$AF$300,0,C$1-COUNT($B32:B32))<>""),--(INDEX(Detailed!$F$3:$AF$300,0,C$1)<>""))
C61=SUMPRODUCT(--(Detailed!$A$3:$A$300=$A$60),--(INDEX(Detailed!$F$3:$AF$300,0,C$1-COUNT($B61:B61))<>""),--(INDEX(Detailed!$F$3:$AF$300,0,C$1)<>""))
C90=SUMPRODUCT(--(Detailed!$A$3:$A$300=$A$89),--(INDEX(Detailed!$F$3:$AF$300,0,C$1-COUNT($B90:B90))<>""),--(INDEX(Detailed!$F$3:$AF$300,0,C$1)<>""))
C119=SUMPRODUCT(--(Detailed!$A$3:$A$300=$A$118),--(INDEX(Detailed!$F$3:$AF$300,0,C$1-COUNT($B119:B119))<>""),--(INDEX(Detailed!$F$3:$AF$300,0,C$1)<>""))
 
Last edited:
Upvote 0
Hello Peter,

This worked flawlessly. Thank you very much! I really appreciate it!
Now I have to go on youtube to learn sumproduct basics.
Never used it but after I followed your formula it looks like it is a great function.

Thanks again!

Razvan
 
Upvote 0
Cheers, glad it worked for you. It did take me quite a while to get my head around what was required. :biggrin:
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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