Counting consecutive values based on multiple criteria - excel

I want to count consecutive staff members (Col D) who service a specific vendor, based on if fees are over 20k (Col G)
I have this formula is Col G and drag it down:
{=SUMPRODUCT(($A$2:A2=A2)*($D$2:D2=D2)*($G$2:G2="Yes")*(ROW($G$2:G2)>MAX(IF(($A$2:A2=A2)*($D$2:D2=D2)*($G$2:G2="No"),ROW($G$2:G2)))))}
However, this causes an error count when a staff gets repeated on a same client after x years and doesn't reset the count.
Row 7 should show as 1, not 5.
If someone could help edit this formula to make it reset after another staff is fit between years?

#Edward's answer looks fine. Alternative (Excel 2019,O365) :
=IFS(G2="No","",OR(AND(G2="Yes",A1&D1<>A2&D2),AND(G2="Yes",SUMPRODUCT((($A$1:A1&$D$1:D1)=A2&D2)*1)=0)),1,AND(G2="Yes",SUMPRODUCT((($A$1:A1&$D$1:D1)=A2&D2)*1)>0),E1+1)
Output :
Sheet.v2
EDIT : For older versions of Excel :
=IF(G2="No","",IF(OR(AND(G2="Yes",A1&D1<>A2&D2),AND(G2="Yes",SUMPRODUCT((($A$1:A1&$D$1:D1)=A2&D2)*1)=0)),1,IF(AND(G2="Yes",SUMPRODUCT((($A$1:A1&$D$1:D1)=A2&D2)*1)>0),E1+1)))

As long as you have row 1 without data, like maybe headers. Then in E2 put
=sum(if((A2=A1)*(B2=B1)*(D2=D1)*(G2="Yes")*(DatedIF(C1,C2,"y")=1),E1+1,1))
and paste it all the way down.

Related

Calculating a running sum with reset in Excel

I have a need to calculate a running sum of column D's count data in column E. However, I only want to calculate the running sum for the appropriate categories in columns B and C. In other words, there are four combinations of categories and I need a running sum for each. The easiest way is to do what I currently have in column F (cell F3=SUM($D$2:D3)) and drag it down through F11 and manually restart it at F12. I can't do this in my full dataset though because there are about 20k rows of data. So, I'm trying to make column E dynamically calculate what's in column F. I started with =SUMIFS() and can return the final sum for each combination of the two categories, but it's not a running sum, created dynamically, that resets with the new day count in column A.
Any suggestions would be appreciated. TIA
Initial Response
If I understand the problem correctly, the solution is actually very simple.
This formula goes to E2 (and then copy down):
=IF(B1&C1<>B2&C2,D2,SUM(E1,D2))
In each case (including the first) of a change of either Cat A or Cat B, it takes the count value at that row (i.e. the new starting balance). Thereafter, it does a running balance addition (balance from row above + count at this row), until the next change of Cat A or Cat B is encountered.
Catering for a wider range of Categories:
The above assumes: Cat A and Cat B are only ever 0 or 1 (per the example in OP).
If this isn't true (and the Cats could be any range of values), change the formula per chris neilsen's suggestion (per comments):
=IF(OR(B1<>B2,&C1<>&C2),D2,SUM(E1,D2))
Catering for Previous Same-State Categories:
Both the original formula and the alternate above assume:
Should Cat A & Cat B states change, but subsequently return a 'previous same-state', the running total should still start afresh (i.e. don't 'carry forward' from 'previous same-states').
If one wanted the running balance to include the balance of previous same-states for Cat A & Cat B, use solution suggested by Apostolos55 (below)
=SUMIFS($D$1:D2,$B$1:B2,B2,$C$1:C2,C2)
all you need is a minor adjustment to the cumulative count:
=Sumifs($D$2:D2,$B$2:B2,B2,$C$2:C2,C2)
Put in Current Count and it is done. Drag/drop OR Autocomplete down as needed
Now it takes into account only above/previous than the current...
It's a bit hacky, but you can add a few columns:
consecutive numbers from 2 to end of your data (say this is in Column G).
references to every 10th cell (e.g. G2, G12) for every cell in that cluster. (say this is in Column H)
you can fill in the first few rows for these two columns and then drag it down.
a reference to the count column ('D') in a single cell (say I1).
You can then use CONCATENATE and INDIRECT inside SUM:
Cumulative Count:
SUM(INDIRECT(CONCATENATE($I$2,H2)):D2)
and drag this down.

Excel Formula to count unique values based on three different criteria

I am trying to get a row by row count of unique invoices in a spreadsheet. I want excel to do this by reading either 1 for unique or zero for duplicate.
I have had success with =IF(COUNTIF($C$3:C3,C3)>1,0,1).
This has given me an accurate count based on one specific column, but I have not had any luck advancing this beyond the one column. I would like this formula to be based on three criteria, not just two.
A B C D E F G
Vendor ID Name 1 Invoice Number Inv Date Sum Amount Acctg Date Unique#
00001 A 0000001 3/16/2015 5.00 5/11/2016 1
00010 M 0000001 9/14/2015 10.00 5/24/2016 1
00010 M 0000001 9/4/2015 15.00 5/24/2016 0
00005 K 0000285 4/8/2016 20.00 4/18/2016 1
000106 O 000042 6/7/2016 30.00 6/21/2016 1
000107 H 006333 4/5/2016 6.00 4/11/2016 1
000107 H 006333 4/5/2016 6.00 4/12/2016 1​
There are duplicates in all the columns because of how I needed to pull the report. I would like a pull down formula that would give me unique values of A, C, F in a 1,0 format on each row line by comparing each of them against a total combination of each of three columns. Please note vendor M having a duplicate invoice number vs vendor H which has two distinct invoices based on the criteria.
This will be a large drain on resources because of the size of the data. I am looking at around 20-90k lines, but maybe someone can show me a better mousetrap? VBA macro? Match Index? Anyway, onwards to the failures!
Please feel free to explain why they didn't work, or how they could. Also please ignore column locations compared to my example as I was moving things around quite frequently.
=A&C&F then use If(countif('ColumnX')), but this didn't work correctly as I found data that was listed as a repeat when it was actually unique. I think the root problem with doing this was combining the date and general formats into one cell.
=SUMPRODUCT((1/COUNTIFS(E3:E1000,E3:E1000,J3:J1000,J3:J1000,G3:G1000,G3:G1000)))
Multiple versions of AND with IF(CountIF)
Multiple versions of =A&C AND CountIF (Date)
I have also looked at the following questions in SE and found them helpful, but ultimately not what I specifically needed, or I failed at implementation.
Simple Pivot Table to Count Unique Values
I tried this unsuccessfully based on unique invoices, need three criteria not just one.
Count unique values in Excel
See above.
Excel Formula: Count Unique Values in a Row Based on Corresponding Value in Another Row
This looks like it should work, but I tried and failed to correctly adapt to my problem.
Excel - Return Count of Unique Values Based on Two Columns
This also should work perfectly with addition of third column. Formula yelled at me and called me names. Mentioned something about can't fix stupid.
Please let me know if any parts of the question are unclear. I did my best to not duplicate and trim the information down. Thanks in advance!
If I am understanding your problem correctly, basically you want column G to check if the current row is a duplicate (based on columns A, C and F) of any rows above it. If it is, return a 0, else return a 1.
If that is what you are looking to achieve, you can do so using the COUNTIFS() function to know if there are any duplicates above the row and then simply check if the count = 0 or is > 0 (=0 means it's unique, >0 means it is a duplicate).
Your formula for column G would look as follows:
G2: 1 (obviously we know it is unique since there are no values above it to be a duplicate of)
G3: =IF(COUNTIFS($A$2:A2,A3,$C$2:C2,C3,$F$2:F2,F3)=0,1,0)
then, drag G3 downwards.
Hope this is what you were looking for.

Sum row based on criteria across multiple columns

I have googled for hours, not being able to find a solution to what I need/want. I have an Excel sheet where I want to sum the values in one column based on the criteria that either one of two columns should have a specific value in it. For instance
A B C
1 4 20 7
2 5 100 3
3 100 21 4
4 15 21 4
5 21 24 8
I want to sum the values in C given that at least one of A and B contains a value of less than or equal to 20. Let us assume that A1:A5 is named A, B1:B5 is named B, and C1:C5 is named C (for simplicity). I have tried:
={SUMPRODUCT(C,((A<=20)+(C<=20)))}
which gives me the rows where both columns match summed twice, and
={SUMPRODUCT(C,((A<=20)*(C<=20)))}
which gives me only the rows where both columns match
So far, I have settled for the solution of adding a column D with the lowest value of A and B, but it bugs me so much that I can't do it with formulas.
Any help would be highly appreciated, so thanks in advance. All I have found when googling is the "multiple criteria for same column" problem.
Thanks. That works. Found another one that works, after I figured out that excel does not treat 1 + 1 = 1 as I learnt in discrete mathematics, but as you say, counts the both the trues. Tried instead with:
{=SUM(IF((A<=20)+(B<=20);C;0))}
But I like yours better.
Your problem that it is "summing twice" in this formula
={SUMPRODUCT(C,((A<=20)+(C<=20)))}
is due to addition turning first TRUE plus the second TRUE into 2. It is not actually summing twice, because for any row, if only one condition is met, it would count that row only once.
The solution is to transform either the 1 or the 2 into a 1, using an IF:
={SUMPRODUCT(C,IF((A<=20)+(C<=20))>0, 1, 0)}
That way, each value in column C would only be counted at max once.
Following this site you could build up your SUMPRODUCT() formula like this:
=SUMPRODUCT(C,SIGN((A<=20)+(C<=20)))
So, instead of a nested IF() you control your or condition with the SIGN()function.
hth
If you plan to use a large set of data then it is best to use the array formula:
{=SUM(IF((A1:A5<=20)+(B1:B5<=20),C1:C5,0))}
Obviously adjust the range to suit the data set, however if the whole of each column is to form part of the formula then you can simply adjust to:
{=SUM(IF((A:A<=20)+(B:B<=20),C:C,0))}
This will perform the calculation on all rows of data within the A, B and C columns. With either example remember to press Ctrl + Shift + Enter in order to trigger the array formula (as opposed to typing the { and }).

Excel Add up columns regardless of row

I have data like this in an excel sheet:
1 2 3
4 5 6
What I am trying to do is come up with a formal that will give me the total of each row, something like this =A+B+C, so I can use this formula on each row...I have plenty of rows, The above is just and example, I have this =A1+B1+C1 is there away in excel to get A1 + B1 + C1 without the row number?
Please help, this would save me lots of time.
I have looked into =ROW to get the row number, can I use the sum function with the row function?
=SUM(A=ROW():B=ROW())
I found a solution to my problem:
=SUM(INDIRECT("A"&ROW()):INDIRECT("C"&ROW()))
This will work
=SUM(INDIRECT("A"&ROW()):INDIRECT("C"&ROW()))

Excel - vertical look up list (without VBA)

I would like to find a formula that gives me a list of cells that contains a certain date.
Example:
name - day - amount - month (hidden)
a 01-01-2012 5 =month(01-01-2012) = 1
b 02-01-2012 4 =month(02-01-2012) = 1
c 10-01-2012 3 =month(10-01-2012) = 1
d 10-01-2012 6 =month(10-01-2012) = 1
e 11-02-2012 2 =month(11-02-2012) = 2
So in this example, I would like to get all the (unique) days of January (in my case a list with: 01-01-2012, 02-10-2012 & 10-01-2012).
Afterwards I would like to have the total of amounts on these days of the list above (but that's easy and I guess I will find that alone :p)
I first used the Vertical Lookup formula but this gives me only one day in January, and not a list of all the days in January.
In fact it's a filter that I need, but with a formula
Thanks for your time & help
For getting the list vertically:
Formula in column H of your sheet (array formula, insert with Ctrl-Shift-Enter, curled brackets inserted by Excel, not by user):
{=INDEX($B$2:$B$10;MATCH(0;COUNTIF($H$1:H1;$B$2:$B$10);0))}
Cells should be formatted as Date to get dates, not integers.
Horizontally:
{=INDEX(Data!$B$2:$B$10;MATCH(0;COUNTIF($A6:A6;Data!$B$2:$B$10);0))}
To filter out unique days in October:
=INDEX(Data!$B$2:$B$10;MATCH(0;IF(COUNTIF(A4:$A4;Data!$B$2:$B$10)=0;IF(MONTH(Data!$B$2:$B$10)=10;0;1);1);0))
Your sheet modified: http://www.bumpclub.ee/~jyri_r/Excel/filter_formula_month.xls
Select the cell you want the result to start appearing in.
Choose the Data ribbon and under filter you will find advanced
Choose the dates you want filtered, and at the bottom of that dialogue you'll find a checkbox with Unique Items only, check that one.
And Bob's youre uncle

Resources