Create a formula to find the count in Excel column - excel

I not very use to use excel complex formula.
Here is request
Column has few number (-ve/+ve). I have to count these based on intervals. These interval are not pre-decided.
See screen Shot
Values in Label col can change in run time. A is less than -15, B is between -15 to 6 and so on. I have to create a formula to add a count in the Count col.
Please guide
Thank you
Regards

You will save yourself a lot of maintenance headaches by re-formatting the "Labels" Table:
D
E
F
G
6
Greater Than or Equal To
Less Than or Equal To
Count
7
Label A
-99
-16
(formula below goes here)
8
Label B
-15
-6
9
Label C
-5
5
10
Label D
6
15
The formula to place in the first count cell (G7 in my example) is:
=COUNTIFS(A$1:A$19,">="&E7,A$1:A$19,"<="&F7)
And then fill it down the length of the table. (In this example 4 rows). Be mindful of the $ that lock the rows of your values column.

I would strongly advise to follow the approach outlined by #Max as it is easier to maintain and less error-prone. However, as you stated, that you are looking for a solution that takes your format into account, I come up with this.
Note:
I split up the complex formula in different pieces to make it easier to explain. You can paste the different pieces together in a single cell per row if you like.
Step 1: Getting rid of unnecessary descriptions
I use a combination of 'SUBSTITUTE', 'MID' and 'FIND' to extract our operators and values from your labels: F3: =SUBSTITUTE(MID(D3,FIND("(",D3)+1,FIND(")",D3)-FIND("(",D3)-1),"interval ","").
The cell D3 contains your label, e.g. "A (interval <-15).
Step 2: Getting the lower threshold
I check whether the label contains two thresholds or not by looking for the "/" character. Next, I handle both situations.
G3: =IF(ISERR(FIND("/",F3)),MID(F3,1,LENGTH(F3)),">="&MID(F3,1,FIND("/",F3)))
The cell F3contains the result of Step 1
Step 4: Getting the upper threshold
Similar to Step 2, except for the operators.
H3: =IF(ISERR(FINN("/",F3)),MID(F3,1,LENGTH(F3)),"<="&MID(F3,FIND("/",F3),LENGTH(F3)-FIND("/",F2)))
Step 5: Counting
I use MID to include the single pieces of Step 3 and Step 4 as text into the formula. If you paste everything together, the use of it will not be necessary.
I3: =COUNTIFS($A$1:$A$19,MID(G3,1,LENGTH(G3)),$A$1:$A$19,MID(H3,1,LENGTH(H3)))
Update: Here's the screenshot.

Related

Excel: Dynamic SUM with OFFSET calculation

I need to create a dynamic sum of data chunks out of a large data set contained in a csv file (>100k rows). The data is planned to be displayed in PowerBI but I have literately no idea of the DAX coding language or VBA. So I hope I can preformat the data in excel.
The way to distinguish the data sub-sets I want to sum up is a counting row. The rows starts with every new subset from 1 but the final number >= 1 is totally ‘random’.
The first row is the countingRow the second row is the dataRow.
> 1 45
2 20
3 20
4 10 -> SUM 95
> 1 30
2 5 -> SUM 35
> 1 X -> new SUM
I think it is possible to work with the SUM, IF and OFFSET function.
My plan was to check whether a cell contains a 1 or not. Check the range between two true values minus one cell, then calculate the offset sum in the other column.
But when I thought I found the solution I realized that I have no way to bring my pointer to a new data subset.
Which function do I need to move my calculation threw the column?
Is it even possible to do an calculation of this scale in excel?
PS: I'm although thankful for a DAX or VBA tutorial which could bring me to a solution.
In the following sample data image, use the following formula in D2.
=IF(OR(A3={1,""}), SUM(INDEX(B:B, AGGREGATE(14, 6, ROW($1:2)/(A$1:A2=1), 1)):INDEX(B:B, ROW())), TEXT(,))
Fill down.
A slightly shorter formula without array type formula:
=IF(OR(A3={1,""}),SUM($B$2:B2)-SUM($C$1:C1),"")

Excel, Sumproduct, multiple conditions search in {}

I run into several postings on the internet (incl. stackowerflow) with code like this
=SUMPRODUCT((A1:A10="Marketing")*(B1:B10={"North","South"})*(C1:C10))
Conditions for search are neatly put into {}. I have 28 such conditions to search for, so I'm looking for a way to make the formula easier to read. If I try it, i get N/A.
Is there a trick I'm missing?
I'm aware that it can be written
(B1:B10="North") + (B1:B10="South")
but with 28 items it is going to be long.
Thank you in advance
EDIT1: (Disregard)
Tried Axel's suggestion
Simple example
- A B C D
1 1 2 3
2 1 2 2 3
3 2 4 4 6
4 3 6 6 9
=SUMPRODUCT((A2:A4={2,3})*(B2:D4))
Returns Sumproduct(({1,2,3}={2,3})*(B2:D4)) -> I still get N/A for last column when you continue in process
Same for
=SUMPRODUCT((A2:A4=A6:B6)*(B2:D4))
where A6:B6 is list of conditions
or
=SUMPRODUCT((A2:A4=testrange)*(B2:D4))
I'm trying to put all conditions within formula {"case1","case2",...} and so but can't make it work.
Edit 2:
Ok, I see the difference now.
Initial formula is column by column by column
What I'm trying to solve
Column A- list of accounts, I need to find 28 of them
Row 1 - months (conditions varies)
Range B2:AA462 - values
I can write it all with (A2:A462="account1")+(A2:A462="acount2")... up to 28 cases, but I'm asking whether there is a way to write it more simpler
Something like initial A2:A462={"North","South"}
Something like
=Sumproduct((A2:A462={"account1","account2",...})*(B1:AA1="June")*(B2:AA462))
Is there a way write this somehow?
EDIT 4:
Few weeks later inspired by Axel's inputs
=SUMPRODUCT(MMULT(--(A2:A7=G1:J1),ROW(1:4)/ROW(1:4))*(B1:E1=G4)*B2:E7)
Can be grown into
{=SUMPRODUCT(MMULT(--(A2:A7=TRANSPOSE(namedrange)),ROW(OFFSET(A1,0,0,COUNTA(namedrange)))/ROW(OFFSET(A1,0,0,COUNTA(namedrange))))*(B1:E1=G4)*(B2:E7))}
Ok, named range, has conditions within column, more natural way to keep a list of conditions you want to filter for. Also MMULT is now flexible, and counts number of conditions and adjust number of rows to multiply by.
Whole formula must be entered as array formula.
{"North","South"} is the array literal for a row vector. That means it is as if "North" and "South" is placed in juxtaposed cells in one row. So if "North" is in E1 and "South" is in F1, then the formula could also be:
=SUMPRODUCT((A1:A13="Marketing")*(B1:B13=E1:F1)*C1:C13)
With more criteria it could be:
=SUMPRODUCT((A1:A13="Marketing")*(B1:B13=E1:H1)*C1:C13)
It is important that the criterias are in a row vector (one row, multiple columns) since the B1:B13 is a column vector.
Answer to your Edit 2:
The approach:
=SUMPRODUCT(((A2:A462="account1")+(A2:A462="account2")+...+(A2:A462="account28"))*(B1:AA1="June")*B2:AA462)
, which will work, is different from (A2:A462={"account1","account2",...,"account28"}). The latter cannot work since it creates a matrix of 461 rows and 28 columns while the working one ((A2:A462="account1")+(A2:A462="account2")+...+(A2:A462="account28")) is only a vector of 461 rows in one column.
The equivalent could be:
=SUMPRODUCT(MMULT(--(A2:A462={"account1","account2",...,"account28"});ROW(1:28)/ROW(1:28))*(B1:AA1="June")*B2:AA462)
and if "account1","account2",...,"account28" are in AC1:BD1 then also:
=SUMPRODUCT(MMULT(--(A2:A462=AC1:BD1);ROW(1:28)/ROW(1:28))*(B1:AA1="June")*B2:AA462)
What is this doing? It uses MMULT to transform the matrix of 461 rows and 28 columns into a vector of 461 rows by multiplying the matrix with a row vector of 28 rows filled with 1.
So if there is a 1 in one of the 28 columns in each row of the matrix, then there will also be a 1 as the row value of the resulting vector of 461 rows.
Example:
Formula in H3:
=SUMPRODUCT(((A2:A7=G1)+(A2:A7=H1)+(A2:A7=I1)+(A2:A7=J1))*(B1:E1=G3)*B2:E7)
Formula in H4:
=SUMPRODUCT(MMULT(--(A2:A7=G1:J1),ROW(1:4)/ROW(1:4))*(B1:E1=G4)*B2:E7)
To be complete, there also would be an approach using SUMIF inside SUMPRODUCT which would be the better approach in my opinion.
So the Formula in H4 would be:
=SUMPRODUCT(SUMIF(A2:A7,G1:J1,INDEX(B2:E7,0,MATCH(G5,B1:E1,0))))
Your formula would be:
=SUMPRODUCT(SUMIF(A2:A462,AC1:BD1,INDEX(B2:AA462,0,MATCH("June",B1:AA1,0))))

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 }).

Compare 2 set of cells and count

I'm trying to make something of a pattern recognize counting cell and calculate their probability of occurrence.
Set 1 is from a database that looks like this (consists of 6+1 columns):
ABABAB 1
CACACA 2
CACACA 2
CACACA 2
CACACA 1
ABABAB 1
Set 2 is what I input manually (only 6 columns, without the 7th column information)
CACACA
If I want to know the probability of the 7th column as "2", pattern recognize counting cell should return 75, where if its "1" it should return 25.
Been cracking my brain using countifs function but nothing seems to work.
From my understanding you have seven columns, like this:
You would like to make a ratio between a combination that includes all seven columns (from A to G in the picture), and the combination of the first six columns (from A to F in the picture).
In this case you may use COUNTIFS as follows (I will use your same example CACACA):
=COUNTIFS(A1:A6,"C",B1:B6,"A",C1:C6,"C",D1:D6,"A",E1:E6,"C",F1:F6,"A",G1:G6,"2")/COUNTIFS(A1:A6,"C",B1:B6,"A",C1:C6,"C",D1:D6,"A",E1:E6,"C",F1:F6,"A")
The result will be 0.75, so if you want 75 you can just multiply the result by 100.
If you want to check the result also for CACACA 1, you just need to change the last value of the first COUNTIFS in the formula from "2" to "1" and you will get 0.25.
In case my understanding was wrong or you need more support do not hesitate to drop me a note!

Permutations in Excel

I am have a string with 6 spaces, e.g. 000000. Each space can hold one of three digits - 0, 1, or 2. I know that I can get a total of 120 permutations using the Permut function in Excel, i.e. =PERMUT(6,3) = 120. But I would actually like to have each individual permutation in a cell, e.g. 000001, 000010, etc.. Ideally, the end result would be 120 rows of unique 6-digit IDs.
Please help if you know a faster way of accomplishing this without entering the figures manually.
Thanks!
There is a VBA functionin the last post on this page. Copy it into a VBA module, then in Excel, create a column of integers from 0 to n where n = the number of IDs you want. In the next column, call the VBA function with the value from the first column as the first argument, and 3 as the second argument. Something like
Column A Column b
0 =baseconv(A1, 3)
1 =baseconv(A2, 3)
2 =baseconv(A3, 3)
... etc.
Your IDs are really just incremental values using a base 3 counting system. You can format the output to get leading zeros with a custom format of '000000'.
Incidentally, with 6 positions and 3 available values, you can get 3 ^ 6, or 729 unique IDs
First, I don't think you're using PERMUT correctly here. What PERMUT(6,3) gives you is the total number of ways to arrange three things picked out of a set of six things. So the result is 120 because you could have 6*5*4 possible permutations. In your case you have 3^6 = 729 possible strings, because each position has one of three possible characters.
Others have posted perfectly fine VBA-based solutions, but this isn't that hard to do in the worksheet. Here is an array formula that will return an array of the last six digits of the ternary (base-3) representation of a number:
=FLOOR(MOD(<the number>,3^({5,4,3,2,1,0}+1))/(3^{5,4,3,2,1,0}),1)
(As WarrenG points out, just getting a bunch of base-3 numbers is one way to solve your problem.)
You would drag out the numbers 0 through 728 in a column somewhere, say $A$1:$A$729. Then in $B$1:$G$1, put the formula:
=FLOOR(MOD(A1,3^({5,4,3,2,1,0}+1))/(3^{5,4,3,2,1,0}),1)
remembering to enter it as an array formula with Ctrl-Shift-Enter. Then drag that down through $B$729:$G$729.
Finally in cell $H$1, put the formula:
=CONCATENATE(B1,C1,D1,E1,F1,G1)
and drag that down through $H$729. You're done!

Resources