Excel formulae query - excel

I have a data sheet and have to create a report which is based on multiple conditions based on data. Attaching an image of the Data sheet and another sheer which is a report sheet.
Data Sheet:
Report Sheet:
Question: In the report sheet, in column A I have unique types and in column B I want the total count of the objects based on type and color. So presently I want total count of Orange fruit and Orange vegetable. i.e. B3 will have 2 as the value and B4 will be 1.
I tried the following formulae, but I am getting 0 in B3:
=COUNT(IF((Data!$A$2:Data!$A$7=A3 * Data!$B$2:Data!$B$7=“Orange”),Data!$C$2:Data!$C$7))
Is the condition I am using inside IF incorrect?

For getting the total, you are looking for SUM not COUNT.
Especially SUMIFS which as the name suggests, will let you define multiple criterias, like this:
=SUMIFS(C2:C7,A2:A7,"Fruits",B2:B7,"Orange")
This will return 30.
=SUMIFS(C2:C7,A2:A7,"Vegetables",B2:B7,"Orange")
And this will say 14.
So the first argument is the area where the numbers to be summed are;
Next you will add the range where you would like to evaluate the following, the criteria.
Edit
I see that you might want to use COUNTIFS to return the number of occurences, not the total, in this case, this might be very similar:
=COUNTIFS(A2:A7,"Fruits",B2:B7,"Orange")

You can get it to work with your original formula if
(1) You put extra brackets as shown
(2) Use SUM instead of COUNT
(3) Enter it as an array formula using Ctrl-Shift-Enter
=SUM(IF((Data!$A$2:Data!$A$7=A3) * (Data!$B$2:Data!$B$7="Orange"),Data!$C$2:Data!$C$7))
so it appears with curly brackets round it.
I also had to re-type the quotation marks to get it to work.
=SUM((Data!$A$2:Data!$A$7=A3) * (Data!$B$2:Data!$B$7="Orange") * Data!$C$2:Data!$C$7)
is another way of doing it, also as an array formula.

Related

Getting number and off setting numbers from array in Excel

I have array of numbers in a single column like this:
I want only that numbers for which corresponding negative numbers exist. If number exist 2 times, but negative number exist only one time, then I wanted to retain one positive and one negative number. Similarly, if number exists 3 times, and negative number appears only two times, then I want 2 set of numbers including positive and negative. In this case, I wanted to get output:
5 2 -2 -5
Orders of numbers are not relevant for me. Please do not use VBA. You can create multiple column and apply filter at the end.
Thank you for the response, but I wanted to get the data in column next to the values. Like:
5
2
-2
-5
Please help.
Here's another Office 365 solution:
Name the data range DATA
Put this formula anywhere: =CONCAT(REPT(-ROW(A1:A100)&" "&ROW(A1:A100)&" ",COUNTIF(DATA,"="&ROW(A1:A100)*IF(COUNTIF(DATA,"="&-ROW(A1:A100))<COUNTIF(DATA,"="&ROW(A1:A100)),-1,1))))
That will output the pairs into one cell.
Here's a slightly modified Step 2, which excludes duplicates: =CONCAT(IF((COUNTIF(DATA,"="&-ROW(A1:A100))>0)*(COUNTIF(DATA,"="&ROW(A1:A100))>0),-ROW(A1:A100)&" "&ROW(A1:A100)&" ",""))
Looks like this:
The data doesn't need to be sorted. Both methods work up to 100, but you can easily expand that by changing A100 to A1000 or whatever you need.
Use the vlookup formula to identify the rows, and you can use the Filter & Unique formula to get the list, or a pivot table.
First, immediately next to your data use the formula:
=vlookup(A1*-1,$A$1:$A$1,1,0)
For non-365:
This will produce an error for each instance that doesn't have a match. You can filter at this point to get your list from the existing table. You can also create a pivot table under the Data tab of your ribbon and inserting a pivot table. Filter the #N/A from there to get an exclusive list without hidden rows.
For 365:
You can use the following combination of formulas to get the exclusive list as well.
=UNIQUE(FILTER(B1:B8,ISNUMBER(B1:B8)),0,0) or =UNIQUE(FILTER($B$1:$B$8,ISNUMBER($B$1:$B$8)),0,0) should yield the same results
As ScottCraner mentioned, you can circumvent the helper column in 365 by modifying the formula a bit more:
=UNIQUE(FILTER(A1:A8,ISNUMBER(MATCH(-A1:A8,A1:A8,0)),"")
The Match here is doing something similar to the Vlookup, but housing that logic within the formula, so it's a cleaner solution in my opinion.
Using your data the result was { -5,-2,2,5 }
These are spill formulas so you only need to put it in one spot and it will expand the formula over the adjacent cells below where it's entered for however many cells needed to list all the unique numbers that occur. It takes into account the negatives and so on. This may be a 365 formula, so if you're on another version of excel it may not work.
Edit: Adjusted the instructions to fully address the question.

Issue converting SUMPRODUCT formula to INDEX+MATCH

I have a spreadsheet that shows the actual dollars produced and the expected dollars produced for each employee, where each row is one day and each employee has two columns. I would like to count the number of times an employee came within 10% of their production goal without referencing specific columns. Please look at the example spreadsheet.
I want for the formulas in cells Sheet2!E4:E7 to do what the formulas in Sheet2!E10:E13 do.
I am trying to avoid using direct column references because it causes me to have to go in and update the columns in the formulas every time a new employee is added to the sheet.
I'm no stranger to using INDEX+MATCH, or to converting SUMPRODUCT formulas to INDEX+MATCH. What I can't get past are the formulas in cells Sheet2!E4:E7. If you evaluate the formula you can watch the INDEX+MATCH section on the left side of the formula work correctly, and then a nearly identical INDEX+MATCH section on the right side evaluate to 0 for no apparent reason.
The formula I'm having trouble with is in cell Sheet2!E4:
COUNTIF(INDEX(Sheet1!$A$2:$AZ$314,,MATCH($B4,Sheet1!$A$1:$AZ$1,0)),">"&(INDEX(Sheet1!$A$2:$AZ$314,,MATCH($B4,Sheet1!$A$1:$AZ$1,0)+1)))
The section to the left of the ">" will match with the correct range (Sheet1!B2:B314), however the section to the right of the ">" will evaluate to 0 instead of Sheet1!C2:C314.
This is strange to me because the only real difference between the two sections is the '+1' on the end of the MATCH function, and adding the '+1' to the section to the left of the ">" produces the expected result for the MATCH function (Sheet1!C2:C314) as seen by evaluating the formula in cell Sheet2!E5.
The formula that produces the correct result is in cell Sheet2!E10:
SUMPRODUCT(--(ISNUMBER(Sheet1!$B$2:$B$313)),--(Sheet1!$B$2:$B$313<Sheet1!$C$2:$C$313),--(Sheet1!$B$2:$B$313>=Sheet1!$C$2:$C$313*0.9))
I understand that if I exclude the '$' before the column references, any future additions/subtractions to the columns on Sheet1 will adjust the references accordingly. This solution is not ideal, because there are multiple data sheets (one for each year) where the columns are all different (Dan is column F for 2019, G for 2018, M for 2017, etc.), and the tables using these sheets are laid out in a way that would prevent me from easily being able to auto-update the formulas, so a solution that locates the correct column using the employee's name is preferred.
The correct result should be 2 for Allen and 3 for Torres, but I can only seem to get the INDEX+MATCH formulas to return 0, 12, 15, 16, or 17 (depending on what tweaks I make to the formula).
Any help with this would be greatly appreciated.
You can try this: ARRAY FORMULA CTRL + SHIFT + ENTER
=SUM(IF(ISNUMBER(INDEX($A$1:$E$30,2,MATCH(G9,$A$1:$E$1,0)):INDEX($A$1:$E$30,30,MATCH(G9,$A$1:$E$1,0))),INDEX($A$1:$E$30,2,MATCH(G9,$A$1:$E$1,0)):INDEX($A$1:$E$30,30,MATCH(G9,$A$1:$E$1,0))<INDEX($A$1:$E$30,2,MATCH(G9,$A$1:$E$1,0)+1):INDEX($A$1:$E$30,30,MATCH(G9,$A$1:$E$1,0)+1)*(INDEX($A$1:$E$30,2,MATCH(G9,$A$1:$E$1,0)):INDEX($A$1:$E$30,30,MATCH(G9,$A$1:$E$1,0))>=INDEX($A$1:$E$30,2,MATCH(G9,$A$1:$E$1,0)+1):INDEX($A$1:$E$30,30,MATCH(G9,$A$1:$E$1,0)+1)*0.9))*1)
and adapt the ranges to your needs. It is entered on the same sheet as the data.
Basically this approach uses ARRAY FORMULA CTRL + SHIFT + ENTER
INDEX($A$1:$E$30,2,MATCH(G9,$A$1:$E$1,0)):INDEX($A$1:$E$30,30,MATCH(G9,$A$1:$E$1,0)))
to get the needed ranges. MATCH looks for the right column. the row number can be set as needed, here its is from 2 to30 adapted to your provided data. My search term in this case is in G9 with Allen as content. The result of the formula is $B$2:$B$30. A +1 after Match gives the other range $C$2:$C$30. Both ranges can then be evaluated with the needed conditions
Pull it down and provide Torres in G10. Then the ranges will be adapted to $D$2:$D$30 and $E$2:$E$30.

Transpose multiple occurrences

EDIT: I have revived the source data source to remove the ambiguity of my last screen shots
I am trying to transpose spreadsheet data where there are many rows where the customer name may be duplicated but each row contains a different product.
For instance
revised original data source
to
revised proposed data format
I would like to do it with formulae if possible as I struggle with VB
Thank you for any help
I realise this is a huge answer, apologies but I wanted to be clear. If you need anything from me, drop me a comment and I'll help out.
Here's the output from my formula:
EDITED ANSWER - Named ranges used for ease of understanding:
These are just an example of a few of the named ranges I have used, you can reference the ranges directly or name them yourself (simplest way is to highlight the data then put the name in the drop down next to the formula bar [top left])
Be wary that as we will be using Array formulas for AccNum and AccType, you will not want to select the entire column and instead opt for either the exact data length or overshoot it by 100 or so. Large array formulas tend to slow down calculation and will calculate every cell individually regardless of it being empty.
First formula
=IF(COUNTIF(D2:D11,">""")>0,CONCATENATE("Account Number ",LEFT((COLUMN(A:A)+1)/2,1)),"")
This formula is identical to the one in the original answer apart form the adjusted heading title.
=IF(Condition,True,False) - There are so many uses for the IF logic, it is the best formula in Excel in my opinion. I have used to IF with COUNTIF to check whether there is more than 0 cells that are more than BLANK (or ""). This is just a trick around using ISBLANK() or other blank identifiers that get confused when formula is present.
If the result is TRUE, I use CONCATENATE(Text1,Text2,etc.) to build a text string for the column header. ROW(1:1) or COLUMN(A:A) is commonly used to initiate an automatically increasing integer for formulas to use based on whether the count increase is required horizontally or vertically. I add 1 to this increasing integer and divide it by 2 so that the increase for each column is 0.5 (1 > 1.5 > 2 > 2.5) I then use LEFT formula to just take the first digit to the left of this decimal answer so the number increases only once every 2 columns.
If the result is FALSE then leave the cell blank ,""). Standard stuff here, no explanation needed.
Second Formula
=CONCATENATE(INDEX(Forename,MATCH(Sheet4!$A2,Reference,0)))
=CONCATENATE(INDEX(Surname,MATCH(Sheet4!$A2,Reference,0)))
CONCATENATE has only been used here to force blank cells to remain blank when pulled by INDEX. INDEX will read blank cells as values and therefore 0's whereas CONCATENATE will read them as text and therefore "".
INDEX(Range,Row,Column): This is a lookup formula that is much more advanced than VLOOKUP or HLOOKUP and not limited in the way that they are.
The range i have used is the expected output range - Forename or Surname
The row is then calculated using MATCH(Criteria,Range,Match Type). Match will look through a range and return the position as an integer where a match occurs. For this I have set the criteria to the unique reference number in column A for that row, the range to the named range Reference and the match type as 0 (1 Less than, 0 Exact Match, -1 Greater than).
I did not define a column number for INDEX as it defaults to the first column and I am only giving it one column of data to output from anyway.
Third Formula
Remember these need to be entered as an array (when in the formula bar hit Ctrl+Shift+Enter)
=IFERROR(INDEX(AccNum,SMALL(IF(Reference=Sheet4!$A2,ROW(Reference)-ROW(INDEX(Reference,1,1))+1),ROUNDDOWN((COLUMN(A:A)+1)/2,0))),"")
=IFERROR(INDEX(AccType,SMALL(IF(Reference=Sheet4!$A2,ROW(Reference)-ROW(INDEX(Reference,1,1))+1),ROUNDDOWN((COLUMN(B:B)+1)/2,0))),"")
As you can see, one of these is used for AccNum and the other for AccType.
IFERROR(Value): The reason that this has been used is that we are not expecting the formula to always return something. When the formula cannot return something or SMALL has run out of matches to go through then an error will occur (usually #VALUE or #NUM!) so i use ,"") to force a blank result instead (again standard stuff).
I have already explained the INDEX formula above so let's just dive in to how I have worked out the rows that match what we are looking for:
SMALL(IF(Reference=Sheet4!$A2,ROW(Reference)-ROW(INDEX(Reference,1,1))+1),ROUNDDOWN((COLUMN(B:B)+1)/2,0))
The IF statement here is fairly self explanatory but as we have used it as an array formula, it will perform =Sheet4!$A2 which is the unique reference on every cell in the named range Reference individually. In your mock data this returns a result of: {FALSE;TRUE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE} for the first entry (I included titles in the range, hence the initial FALSE). IF will do my row calculation* for every true but leave the FALSEs as they are.
This leaves a result of {FALSE;2;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE} that SMALL(array,k) will use. SMALL will only work on numeric values and will display the 'k'th result. Again the column trick has been used but to cover more ground, I used another method: ROUNDDOWN(Number,digits) as opposed to using LEFT() Digits here means decimal places so I used 0 to round down to a whole integer for the same result. As this copies across the columns like so: 1, 1, 2, 2, 3, 3, SMALL will alternatively (as the formulas alternate) grab the 1st smallest AccNum then the 1st Smallest AccType before grabbing the 2nd AccNum and Acctype and so forth.
*(Row number of the match minus the first row number of the range then plus 1, again fairly common as a foolproof way to always get the correct row regardless of where the data starts; actually as your data starts on row 1 we could just do ROW(Reference) but I left it as is incase you had data in a different format)
ORIGINAL ANSWER - Same logic as above
Here's your solution in 3 parts
Part 1 being a trick for the auto completion of the titles so that they will hide when not used (in case you will just copay and paste values the whole lot to speed up use again).
=IF(COUNTIF(C2:C11,">""")>0,CONCATENATE("Product ",LEFT((COLUMN(A:A)+1)/2,1)),"") in C
=IF(COUNTIF(D2:D11,">""")>0,CONCATENATE("Prod code ",LEFT((COLUMN(B:B)+1)/2,1)),"") in D
Highlight both of the cells and drag across to stagger the outputs "Product " and "Prod code "
Part 2 would be inputting the unique IDs to the new sheet, I would suggest copying your entire column A across to a new sheet and using DATA > REMOVE DUPLICATES > Continue with current selection to trim out the multiple occurrences of unique IDs.
In column B use =INDEX(Sheet2!$B$1:$B$7,MATCH(Sheet4!$A2,Sheet2!$A$1:$A$7,0)) to get the names pulled across.
Part 3, the INDEX
Once again, we are doing a staggered input here before copying the formula across the page to cover the entirety of the data.
=IFERROR(INDEX(Sheet2!$C$1:$D$11,SMALL(IF(Sheet2!$A$1:$A$11=Sheet4!$A2,ROW(Sheet2!$A$1:$A$11)-ROW(INDEX(Sheet2!$A$1:$A$11,1,1))+1),ROUNDDOWN((COLUMN(A:A)+1)/2,0)),1),"") in C
=IFERROR(INDEX(Sheet2!$C$1:$D$11,SMALL(IF(Sheet2!$A$1:$A$11=Sheet4!$A2,ROW(Sheet2!$A$1:$A$11)-ROW(INDEX(Sheet2!$A$1:$A$11,1,1))+1),ROUNDDOWN((COLUMN(B:B)+1)/2,0)),2),"") in D
The formulas of Part 3 will need to be entered as an array (when in the formula bar hit Ctrl+Shift+Enter) . This will need to be done before copying the formulas across.
These formulas can now be dragged / copied in all directions and will feed off of the unique ID in column A.
My Answer is already rather long so I haven't gone on to break the formula down. If you have any trouble understanding how this works, let me know and I will be happy to write up a quick guide, breaking it down chunk by chunk for you.

lookup formula for comma separated Values- Excel

I have a table excel as below.
Name Emp ID task
A 12 x
b 21 y
A 12 z
and I need to receive output as below:
Query: Give me tasks of A, 12.
Output: X,Z
Any help/thoughts? TIA.
If you use this array formula (Ctrl+Shift+Enter when in formula bar) and copy this formula down it will put each result that matches both criteria in a seperate cell, you are then free to use a concatenate to build the string from those if you wish. Only other solution is through VBA.
=IFERROR(INDEX($B$1:$D$5,SMALL(IF($B$1:$B$5=$F$2,IF($C$1:$C$5=$G$2,ROW($B$1:$B$5)-ROW(INDEX($B$1:$B$5,1,1))+1)),ROW(1:1)),3),"")
Please note that I only built this based on a small range (B1:D5) and assumed that the searching criteria are in F2 and G2.
Using small with an IF statement allows you to access the range as an array. where both criteria match, the if statement produces an array of the rows which SMALL will retrieve smallest to largest as Row(1:1) will atomatically update when the formula is copied to increase which row number SMALL is retrieving.
(this was a bit of a rush job so I know that my description is bad, feel free to edit or probe for more detail and I can update it later with a better explanation)

Obtain group average for numerous groups that vary in size

I get the feeling this is close to impossible, but here goes. I have data structured like this:
Test Group 1
pass
fail
pass
fall
Test Group 2
pass
fail
Test Group 3
pass
fail
fail
pass
I want to be able to paste it into Excel and have Excel summarise the percentage of each test group. So it would end up looking like this:
Test Group 1 Percentage Pass: 50%
pass
fail
pass
fall
Test Group 2 Percentage Pass: 50%
pass
fail
Test Group 3 Percentage Pass: 50%
pass
fail
fail
pass
BUT as you can see the test group length is not set, and it may vary. I was hoping is that I might be able to create a formula that follows this logic:
if( A<n> contains "Test")
count A<n> +1 until A<n> contains "Test"
It seems like I'm asking a lot from Excel formulas. I have spent some of today writing a small C# app that will split the configs into separate files so that I could copy and paste separate files into Excel. But it would be great to have fewer steps!
--- UPDATE ----
There were three very interesting approaches to this problem proving nothing is impossible :p
I wanted a solution that allowed me to copy and paste results in and see a load of percentages pop up so I was always sort of looking for a formula solution HOWEVER, please take the time to look at pnuts and Jerrys answers as they reveal some useful features of Excel!
chuffs answer was the one I was looking for and it worked out of the box. For anyone who wants to delve deep into how it works and why, I broke the formula down into steps and filled in some help information. The key to these formulas is combining MATCH, OFFSET and the slightly more obvious SEARCH / FIND / LEFT (I used to use IFERROR(FIND type approach, LEFT seems cleaner :) )
Do look at the documentation for these formulas but to see it all in action with some examples see the Google Spreadsheets I created detailing chuffs answer:
https://docs.google.com/spreadsheet/ccc?key=0AqODI11eAjtldDhDd2dBcFhpZW9SXzEybGtMUWMwM3c#gid=0
---P.S---
For the record I did actually create a C++ program to prettify my data and ouput it as .csv files. If I had had this info I wouldn't have bothered but am glad I tried both routes, it was a fun learning adventure.
A single, array-formula solution to this problem is possible. It requires only that a stop value "Test" be added at the bottom of your column of data.
Assuming your data are in the range A2:A18, here is the formula that would compute the average in cell B2:
=IF(LEFT(A1,4)="Test",
COUNTIF(OFFSET(A1,1,0,MATCH("Test*",$A2:$A$18,0)-1,1),"pass")
/ COUNTA(OFFSET(A1,1,0,MATCH("Test*",$A2:$A$18,0)-1,1)),
"")
The key parts of the formula are expressions that calculate the ranges for the two count functions, COUNTIF and COUNTA.
The COUNTIF function - which counts the number of passes in a group - takes two arguments, the range for the count and the condition to be met by cells counted.
I use the OFFSET function to provide the count range. OFFSETtakes five arguments:
An anchor cell (if a range is provided, the function only uses the top left cell in the range).
The number of rows below (+) or above (-) the anchor cell that the range to be returned will begin.
The number of columns to the right (+) or left (-) of the anchor cell for the start of the return range.
The number of rows to return.
The number of columns to return.
For example, OFFSET(A1,5,2,4,1) will return the range C6:D9.
In the formula, the anchor cell is A1, the row offset is 1 and the column offset is 0, in other words, the start of the range to return is A2.
The number of rows to return is computed by the MATCH function. For the calculation of the average for the first test group, MATCH looks in the range A2:A18 for the row of the first cell that starts with the word 'Test'. A2 is the cell in the pass/fail column just below the cell that starts the test group.
The last cell in the column is A18, which has been added to the data and contains the word 'Test'. This prevents the formula from returning an error for the last test group. Otherwise, the match would return an error because it could not find another occurrence of Test beyond the start of the final group.
Also note that the anchor points for the match range, i.e., $A2:$A$18, leaves unanchored the row reference to the top of the range (the 2 in A2). That's so the range will adjust downward as the formula is copied down so as to find the next, not the previous, occurrence of 'Test'.
Finally, I decrease the number of return rows by 1 to exclude the Text that MATCH found, which belongs to the next group.
The COUNTA functions - which counts the total number of passes and fails in a group - uses the same OFFSET/MATCH expression to get the range for the group.
This is an array formula and so must be entered with the Control-Shift-Enter key combination. Copy the formula down to the bottom of the data (excluding the cell with the 'Test' stop value) to calculate the averages for each group.
For convenience, here is the formula without the breaks shown above. It can be directly pasted into the worksheet (remembering to confirm it with Control-Shift-Enter).
=IF(LEFT(A1,4)="Test",COUNTIF(OFFSET(A1,1,0,MATCH("Test*",$A2:$A$18,0)-1,1),"pass")/COUNTA(OFFSET(A1,1,0,MATCH("Test*",$A2:$A$18,0)-1,1)),"")
Basically Subtotal will meet the requirement, provided some fairly tedious layout adjustments are acceptable.
Preparation:
Assuming Test Group 1 is in A1, insert Row1 and ColumnA, with in A2 =IF(LEFT(B2,1)="T",B2,"") and in C2 =IFERROR(MATCH(B2,{"fail","pass"},0)-1,"") and both formulae copied down as required. (And change fall to fail in source!)
Subtotal:
Select A:C, Data > Outline – Subtotal, At each change in: (1) Test Group 1, Use function: Average, Add subtotal to: check (Column C), uncheck Summary below data, OK.
Tidy up:
1. Move A3:C3 to A1 and A2:C2 to A3.
2. Filter A:C and for ColumnB, Text Filters, Contains add Test, OK.
3. In C3 put ="Percentage Pass: "&C4*100&"%" and copy down to last row numbered in blue (should show #DIV/0!).
4. Highlight A:C for all the rows numbered in blue and embolden.
5. Hide ColumnA and, optionally, hide or delete Rows1:2.
6. Take off filter selection.
Hopefully with preparation as on the left the result would be similar to as shown on the right:
Example re comment to #Jerry's answer:
There are many ways to tackle this -- one possible easy solution is to add a column next to your pass/fail column that says (assuming column A):
=IF(A1="pass",100,0)
if you do that, then the average of those values would be equal to the percentage pass.

Resources