COUNTIFS on multiple columns - excel

I would like to return the count of the number of rows for which ANY of the columns have the number 1. The table is three columns, and each column can contain either 0, 1, 2, or 3. Example:
Col1 | Col 2 | Col 3
0 | 1 | 0
1 | 2 | 3
0 | 0 | 0
3 | 1 | 1
2 | 2 | 2
etc.
I would like the formula to return 3.

Here you go:
=SUMPRODUCT(--((A1:A10=1)+(B1:B10=1)+(C1:C10=1)>0))
Change the 10s to however far down you need to go.

Related

Excel formula to sum cell values between variable number of rows containing specific text string

Have been looking for a formula that will add up cell values contained in variable number of rows delimited with same text string (A and End) and within fixed number of columns(A,B,C). Result to be 3 in example, but could be 4 if additional row added with cell value = 1. Thanks.
| A | B | C
| 0 | 1 | 0
| 0 | 0 | 1
| 0 | 0 | 1
|End

Spark Aggregating a single column based on several overlapping windows

Let's say I have the following table with money spent per day (timestamp)
timestamp| spent
0 | 0
1 | 0
2 | 1
3 | 4
4 | 0
5 | 0
6 | 1
7 | 3
The result I'm looking for is a table adding columns for the cummulative money spent in the last "n" days, for example the last 2 days and the last 5 days. Resulting in something like this.
timestamp | spent | spent-2d |spent-5d | ....
0 | 0 | null | null | ...
1 | 0 | 0 | null | ...
2 | 1 | 1 | null | ...
3 | 4 | 5 | null | ...
4 | 0 | 4 | 5 | ...
5 | 0 | 0 | 5 | ...
6 | 1 | 1 | 6 | ...
7 | 3 | 4 | 8 | ....
One possible solution is to add lagged columns and then sum but for say, 180 days I would need to add 180 columns and I want to to this process with not just one but several columns in the dataframe. For example for 100-500 columns I want the lagged sum over 1,2,5,7,15,30,90 and 180 days. So adding 180*500 columns seems to be a bad idea.
Any other ideas to make this in a very efficient way?
Window "rangeBetween" method can be used, example for 5 days column:
val lastFiveDaysWindow = Window
.orderBy("timestamp")
.rangeBetween(Window.currentRow - 4, Window.currentRow)
df
.withColumn("spent-5d",
when(
$"timestamp" >= 4,
sum("spent").over(lastFiveDaysWindow)
)
)
Note: Only for small Dataframes, warning exists:
No Partition Defined for Window operation! Moving all data to a single partition, this can cause serious performance degradation.
For bigger DataFrames, inner join can be used, like in answer here:
Count of all element less than the value in a row

Compare Data In Excel Not Working

I am working on an Excel Spread Sheet. Where one column has tiers and another column has ratings, so basically what I am trying to do is:
+---+-------+--------+
| | A | B |
+---+-------+--------+
| 1 | Tiers | Rating |
| 2 | 1 | 1 |
| 3 | 1 | 1 |
| 4 | 3 | 1 |
| 5 | 3 | 2 |
| 6 | 2 | 3 |
| 7 | 1 | 3 |
| 8 | 1 | 3 |
+---+-------+--------+
I count each tiers
4 1's
1 2's
2 3's
so to get the maximum rating for each tier I multiply them by 3 so
4*3 = 12 for tier 1
1*3 = 3 for tier 2
2*3 = 6 for tier 3
now comparing that to the rating columns I add up all the values for tier one so 1+1+3+3 = 8
Now to get the percentage I take the 8/12 get 66.67%
so how can I achieve this in Excel? Please Help :(
You can use the following to calculate the 8
=SUMIF(A2:A8,1,B2:B8)
and the following to calculate the 12
=COUNTIF(A2:A8,1)*3
Then you can just calculate the percentage using:
=SUMIF(A2:A8,1,B2:B8)/(COUNTIF(A2:A8,1)*3)
Here's the formula you need. The formula calculates for tier 1
=sumif(A2:A8,1,B2:B8)/(countif(A2:A8,1)* max(if(A2:A8=1,B2:B8)))
Broken down
Count the tier 1s. Count the cells in column A if they contain a 1
=countif(A2:A8,1)
Get the maximum rating. Return the maximum value in column B if column A contains a 1
=max(if(A2:A8=1,B2:B8))
Sum the ratings for Tier 1. Sum if column A contains a 1
=sumif(A2:A8,1,B2:B8)
You can replace the 1 in the formula with a cell reference e.g D1 and enter 1 in D1. Lock the ranges in the formula so that you can autofill across and put 2 in E1 and 3 in F1 etc.
=sumif($A$2:$A$8,D1,$B$2:$B$8)/(countif($A$2:$A$8,D1)* max(if($A$2:$A$8=D1,$B$2:$B$8)))

Count row that have value in two separate columns

So i'm trying to count who sold something at the end of the day doesn't matter how many item that person sold.
Name Shoes Shirts Hat
A 1 2
B 1
C 1 3
D 1 1
E
So if A sold then should count as 1 person sold something
If E is not selling anything that not count as anything
Example spreadsheet with data from C6 through F10:
| C | D | E | F |
5 |name|shoe|shirt|hat|
6 | a | 1 | 1 | 0 |
7 | b | 0 | 1 | 0 |
8 | c | 1 | 1 | 0 |
9 | d | 1 | 1 | 0 |
10| e | 0 | 0 | 0 |
You could use this formula on the data example above:
=SUM(IF($D$6:$D$10+$E$6:$E$10+$F$6:$F$10>0,1,0))
but you have to hit Ctrl+Shift+Enter, not just Enter (because it is an array formula). You will know you did this correctly because it automatically adds { } around the formula, so it will look like this:
{=SUM(IF($D$6:$D$10+$E$6:$E$10+$F$6:$F$10>0,1,0))}
What this formula does:
IF(D6+E6+F6 > 0, 1,0) for each row until 10+E10+F10, which leaves you with either a 1 or 0 for each row; 1 if the sum of the row > 0 and 0 if it was not > 0. It then adds up the info and gives you a count of any rows that had at least 1 sale of either a shoe, shirt or hat.

How to use vlookup in excel

I have a sheet something like this
A B C D
1 2 2
2 3 3
4 5 5
5 7 9
10
11
12
I would like column D to show values of col A if col B values exist in col C
Example:
A B C D
1 2 2 1
5 7 9 -
D would have a value of 1 since Col b val is in Col C and in row 4 Col D would have no value at all
Yes A,B,C,D are labels as per the comments
You don't need VLOOKUP here. I think MATCH is a better choice.
Try this:
D1:D4 =IF(ISERROR(MATCH(B1,$C$1:$C$7,0)),"",A1)
(This assumes that your numerical values start in row 1.)
The output looks like this:
+---+---+---+----+---+
| | A | B | C | D |
+---+---+---+----+---+
| 1 | 1 | 2 | 2 | 1 |
| 2 | 2 | 3 | 3 | 2 |
| 3 | 4 | 5 | 5 | 4 |
| 4 | 5 | 7 | 9 | |
| 5 | | | 10 | |
| 6 | | | 11 | |
| 7 | | | 12 | |
+---+---+---+----+---+
You can do this with a combination of vlookup, offset and iserror like so:
=IFERROR(IF(VLOOKUP(B2,C:C,1,0)=B2,OFFSET(B2,0,-1)),"-")
offset used with the -1 parameter will return the cell one column to the left, so you do not need to rearrange the columns in your actual worksheet. iserror will check if the lookup failed, and return the specified default value. Finally, you can also specify the exact range to be looked up, in this case as
VLOOKUP(B2,$C$2:$C$8,1,0)

Resources