Is it possible to create a formula that would sum multiple columns by looking up two parameters? - excel

The question may sound very odd, so let me explain.
E.g. I have a table that looks like this:
1 2 3 3
Soda 1 2 1 0
Beer 2 0 0 0
Beer 2 1 2 2
Soda 0 2 0 2
I would like to sum each "drink number" for every category, like this:
1 2 3
Soda 1 4 3
Beer 4 1 4
I have used the IFS and SUMIF formula sequence for this (where B$1 = "Column number",$A2 = "Drink name", rest is just columns corresponding to value locations):
=IFS(B$1=“1”,SUMIF($A$2:$A$5,$A2,$B$2:$B$5),B$1=“2”,SUMIF($A$2:$A$5,$A2,$C$2:$C$5),B$1=“3”,sum(SUMIF($A$2:$A$5,$A2,$D$2:$D$5),SUMIF($A$2:$A$5,$A2,$E$2:$E$5)))
But it is very robust, and needs to be changed manually if the initial table is changed.
I would like to make my formula adaptable to changes. Say, it "looks up" row and column values and sums up every column values by them. For example, if one more column "2" is added, and "Beer" is changed to "Milk" (but the "column length" remains the same);
1 2 2 3 3
Soda 1 2 1 1 0
Milk 2 0 1 0 0
Milk 2 1 0 2 2
Soda 0 2 1 0 2
The formula automatically adjusts to this change, and calculate new sum:
1 2 3
Soda 1 6 3
Milk 4 2 4
Is it possible to create such formula? Because I'm not even sure if something like this can be done with basic Excel formulas. Thank you beforehand.

Yes. For example:
=SUMPRODUCT(($A$2:$A$5=$G2)*($B$1:$E$1=H$1),$B$2:$E$5)
where G2 has Soda and H1 has 1. Then copy across and down (assuming Beer in G3 and 2, 3 in I1:J1)

Related

Group by name and count unique values

I have an Excel file like this, where column A and B are given. I want to add column C and D that represent days. D is pretty easy, because it is always one day. C is tricky, because I want to count only "unique" days, where a branch can be one day maximum, where D counts all days.
A B C D
Row Name Branch Unique Overall
1 Jack Health 1 1
2 Jack Health 0 1
3 Jack Food 1 1
4 Jolie Tech 1 1
5 Jolie Food 1 1
6 Jolie Tech 0 1
7 Jolie Health 1 1
I need column C and D for a pivot table like this:
Branch Unique Overall
Health 2 3
Food 2 2
Tech 1 2
I also could add names as a sub position.
Branch Unique Overall
Health 2 3
-Jack 1 2
-Jolie 1 1
Food 2 2
-Jack 1 1
-Jolie 1 1
Tech 1 2
-Jolie 1 2
But that´s something, that can be done after preparing the data and what comes with the program anyway. So how can I design a formula that counts only unique branches for a data set of hundreds of rows?
Thank you!
In C2 put:
=--(COUNTIFS($A$2:A2,A2,$B$2:B2,B2)=1)
Then copy down

How can I split a single dataframe column, containing multiple values, into a transposed table of 'flags'

I'm trying to separate a single column in a dataframe, that contains multiple comma separated values, into a transposed table that contains columns which flag every potential comma separated value.
e.g.
name purchased total_value
-----------------------------------------------
John Eggs, bread, milk 100
Steve Milk, cheese, wine 140
Susan Beer, cheese, milk 120
Needs to become:
name total_value eggs bread milk cheese wine beer
-----------------------------------------------------------------
John 100 1 1 1 0 0 0
Steve 140 0 0 1 1 1 0
Susan 120 0 0 1 1 0 1
As a small added complication, the purchased column as spaces after the commas, and some of the values have capitals.
Can anyone help me get from A to B?
Thanks in advance
First convert all values to lowercase by lower, call str.get_dummies and last join to original.
For remove original purchased is possible use pop or drop:
df['purchased'] = df['purchased'].str.lower()
df = df.join(df.pop('purchased').str.get_dummies(', '))
df = df.drop('purchased', 1).join(df['purchased'].str.lower().str.get_dummies(', '))
print (df)
name total_value beer bread cheese eggs milk wine
0 John 100 0 1 0 1 1 0
1 Steve 140 0 0 1 0 1 1
2 Susan 120 1 0 1 0 1 0

using index match with sum if

I need to link up a sumif() with an index match (i'm guessing here) but don't really know where to start.
Basically i a table with different classes of pets, their species and quantity. there are 3 stores. I need an output where i can get the quantity of each species from each store dynamically.
data table:
"A1" Pet Stores
Species Class a b c
cat Fluffy1 1 0 0
cat Fluffy2 3 0 0
cat Fluffy3 5 7 1
cat Fluffy4 6 0 7
dog Barky1 7 6 9
dog Barky2 1 3 9
dog Barky3 0 2 8
dog Barky4 0 2 3
fish Swimmy1 0 0 0
fish Swimmy2 1 3 0
fish Swimmy3 0 2 3
fish Swimmy4 0 0 0
Output:
Pet Store a <--change this
cat 15 <--output
dog 8 <--output
fish 1 <--output
right now my formula for "cat" is =SUMIF($A$3:$A$14,A17,$C$3:$C$14). however, it only looks down the 1 column that i've set. how do i change it such that it searches for the "Pet Store" and returns sum of the respective column?
How about this:
Formula in cell H3 copied down is
=SUMIF($A$2:$A$13,G3,INDEX($C$2:$E$13,,MATCH(H$2,$C$1:$E$1,0)))
Slightly shorter that #teylyn's version:
=SUMIF(A$2:A$13,A16,OFFSET(C$2:C$13,,CODE(B$15)-97))
but less versatile as it relies on the shop names being coded (which however is as in the example and makes sense for column label purposes):
However my preference would be for a PivotTable:

Count values in a range comprehended between two values

At Column A i have this values 1
0
3
2
0
5
1
1
1
0
2
1
1
1
0
2
1
1
1
0
0
3
0
2
0
0
3
1
This list grows everyday.
I need a formula to put on every cell of column B that counts upwards how many values bigger than 1 are until the next value = 1 is found.
In another words i need to count how many values larger than 1 are between 1's.
The pretended result would be something like this:
1
0
3
2
0
5
1 3
1
0
2
1 1
1
0
2
1 1
1
0
0
3
0
2
0
0
3
1 3
Thanks in Advance
I would use a helper column, if this is acceptable.
So to create a running count of numbers greater than one which resets each time it encounters a '1', enter this starting in B2 and pull down (I'm assuming the data has a heading and the list starts with a 1) :-
=IF(A2=1,0,B1+(A2>1))
Then to display the counts at each '1' value (but not for repeated ones) enter this in C2 and pull down:-
=IF(AND(A2=1,A1<>1,ISNUMBER(A1)),B1,"")
It's also possible to do it with an array formula, but not sure if it's worth the effort:-
=IF(AND(A2=1,A1<>1),
COUNTIF(
OFFSET(
A$1,
MAX(ROW(A1:A$2)*(A1:A$2=1))-ROW(A$1)+1,,
MAX(ROW(A1))-MAX(ROW(A1:A$2)*(A1:A$2=1))),
">"&0),
"")
to be entered in B2 with Ctrl Shift Enter and pulled down.

Giving duplicates in Excel a unique identifier

I have a large excel spreadsheet containing multiple address lines. Many of these adress lines contain duplicate postcodes, with differing steet adrresses.
I need to seperate out each of these duplicates onto different spreadhseets, so that no one spreadhseet contains the same postcode. I'm currently using an =if() statement to identify the duplicates, then cutting and poasting, and repeating almost ad infinitum to speperate them all out
as an example:
address 1 postcode 1
address 2 postcode 1
address 3 postcode 1
address 4 postcode 2
address 5 postcode 2
address 6 postcode 3
address 7 postcode 3
address 8 postcode 3
address 9 postcode 3
would give 4 spreadsheets:
1)
address 1 postcode 1
address 4 postcode 2
address 6 postcode 3
2)
address 2 postcode 1
address 5 postcode 2
address 7 postcode 3
etc
Any hints on a vb script?
Many thanks
Nick
If this is a one-off thing, then I'd consider doing it in the worksheet and bulk cut-pasting. I'd try something like this:
Sort my data on the post code column
In a new column, add a function to count the number of post codes below the current row that are duplicates of the code on that row (see below)
Save (copy/paste-special-values) the values obtained
Sort by duplicate number
Cut and paste blocks by duplicate number to new sheets
Say I have 20 values between 1 and 5 in column G. I put the following formula in H1:
=SUM(IF(G1:$G$20=G1,1,0))
... and (important bit) enter it as an array formula using Control+Shift+Enter. Now I copy that formula down (for speed, double-click the "fill handle" on the bottom right-hand corner of the cell). Here's my result:
1 2
1 1
2 2
2 1
3 8
3 7
3 6
3 5
3 4
3 3
3 2
3 1
4 2
4 1
5 6
5 5
5 4
5 3
5 2
5 1
Now I can copy column H and Edit...Paste Special...Values to fix the values. Sort by H to get this:
1 1
2 1
3 1
4 1
5 1
1 2
2 2
3 2
4 2
5 2
3 3
5 3
3 4
5 4
3 5
5 5
3 6
5 6
3 7
3 8
Now I have sets of records to copy to each of the 8 sheets that I now know are needed.
Of course, if you're going to be doing this a lot, then a macro would be useful. It needn't be much more complex than automating the above.

Resources