Excel: Challenging formula sought for counting records - excel

I am looking for some help with a formula. On my 'data' sheet, I have data in the format below:
A B C D E F G H I J
1 UID RecordType HCode AdmittedDate Forename Surname DOB Sex STDate RDate
2 87962 STAsses STIV1 01/01/2012 Mark Jones 13/07/1978 Male 09/12/2012
3 89658 Transfer GLSI2 01/01/2012 Alison Aitken 20/12/1956 Female 08/07/2013
4 84563 Discharge JHOP1 01/01/2012 David Beckham 09/08/1987 Male 08/07/2013
5 89654 STAsses STGE1 01/01/2012 Andrew Macbeth 27/09/1976 Male 08/07/2012
6 89867 Transfer KIND1 01/01/2012 George Deas 08/05/1989 Male 08/07/2013
7 87962 Transfer STIV1 01/01/2012 Mark Jones 13/07/1978 Male 04/03/2013
8 89654 Transfer STGE1 01/01/2012 Andrew Macbeth 27/09/1976 Male 12/08/2012
On my 'report' sheet, I have the following table set up:
B C D E F
4 HospCode RecordType Jul-12 Aug-12 Sep-12
5 STGE1 Assessments
6 Transfers
7 Discharges
8
I would like to do 2 things.
In D5, E5, F5, a formula is needed to return the number of 'STAssess' records for HCode 'STGE1' from the data sheet, which were completed in the month shown in D4 of the 'report' sheet (Jul-12). The dates for these records are in column I of the data sheet - 'STDate'.
I've tried using SUMPRODUCT which works to an extent, but I'm having difficulty bringing back records only completed in July. I need the formula to be dynamic in that it only brings back results for the month entered in D4 (so a date range built into the formula isn't doing the job).
In D5, E5, F5, I need to bring back the number of records from the 'data' sheet, which have 'Transfer' in column B (RecordType), 'STGE1' in column C (HCode) and a date in column J (RDate) which falls in the month specified on D4 of the 'report' sheet (Jul-12).
Anyone got any ideas? The data sheet is actually going to be an external .csv file which can't be manipulated. I don't mind using other hidden sheets to do any sorting that needs to be done, provided that this can be setup so that when the data is brought in, no extra work has to be done.
Thanks!

For both of your questions you can use COUNTIFS. It is easier to understand (for me) and compatible with Excel 2007 and newer.
First question:
=COUNTIFS(DATA!B:B,"STAsses",DATA!C:C,$B5,DATA!I:I,">="&D$4,DATA!I:I,"<"&DATE(YEAR(D4),MONTH(D4)+1,1))
Second question:
=COUNTIFS(DATA!B:B,"Transfer",DATA!C:C,$B5,DATA!J:J,">="&D$4,DATA!J:J,"<"&DATE(YEAR(D4),MONTH(D4)+1,1))
And these formulas can be dragged across your needed range.

Here is an example formula that you could use for the D5 cell, does that point you in the right direction?:
{=SUM(IF($B$2:$B$8=$B14,IF($C$2:$C$8=$A14,IF($D$2:$D$8=C$13,1,0),0),0))}
This is an array function so you must enter it without the {} at each end and use Ctrl + Shift + Enter to enter the formula.

If your summary table looks like
HospCode RecordType 12-Jul 12-Aug 12-Sep
STGE1 STAsses 1 0 0
STGE1 Transfer 0 1 0
STGE1 Discharges 0 0 0
And your data is in a table called Table1, then in D5
=SUMPRODUCT((Table1[HCode]=$B5)*(Table1[RecordType]=$C5)*(MONTH(Table1[STDate])=MONTH(D$4)))
I'm not sure what you mean by 'bring back', but assume you mean that you want to show it when RecordType = column C and when the RecordType = "Transfer" and the RDate is in the right month. Then
=SUMPRODUCT((Table1[HCode]=$B5)*(Table1[RecordType]=$C5)*(MONTH(Table1[STDate])=MONTH(D$4)))+SUMPRODUCT((Table1[HCode]=$B5)*(Table1[RecordType]="Transfer")*(MONTH(Table1[RDate])=MONTH(D$4)))

Related

formula to find value from two separate tables and based on values in three tables

I'm looking for a formula for the Party column in Table 3 that will produce its values based on the data contained in Table 1 and Table 2.
NumSelect value in Table 3 determines Party value in Table 3.
Where NumSelect has "p", it refers to data in Table 1. If no "p" in NumSelect, then it refers to Table 2.
Number in NumSelect refers to row number.
If the corresponding ShortName has a value, that value should be returned.
If the corresponding ShortName is blank, then the corresponding Name should be returned.
Uppercase "P" and lowercase "p" in the NumSelect should both point to Table 1.
Each table is an Excel Table and its rows may expand or contract.
Certain rows in Table 1 and Table 2 may be empty.
Formula should not be volatile, not require control+shift+enter to enter the formula, and not require VBA.
Thanks!
Sorry for the bad formatting. I had this question formatted perfectly, but Stack Overflow kept preventing me from posting it because it claimed, "Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon."
Table 1
Name
Gender
ShortName
Occupation
Grace Turner
F
Singer
Cadie Crawford
F
Tiger
Fine Artist
Paige Johnston
F
Archeologist
Dexter Payne
M
Klondike
Veterinarian
Valeria Barnes
F
Chef
Florrie Reed
F
Lawer
Emily Ferguson
F
Scientist
Sam Hawkins
M
Alpha
Biochemist
Savana Ellis
F
Cook
Table 2
Name
Gender
ShortName
Occupation
Vanessa Cooper
F
Producer
Jasmine Morris
F
Beta
Baker
Evelyn Taylor
F
Economist
Adelaide Roberts
F
Historian
Blake Cunningham
M
Lion
Chef
Adelaide Harrison
F
Chemist
Frederick Watson
M
Journalist
Table 3
NumSelect
Party
p2
Tiger
3
Evelyn Taylor
P8
Alpha
2
Beta
7
Frederick Watson
p7
Emily Ferguson
Long Formula
Your formula has 717 characters, this one has 347.
=IF(ISNUMBER(SEARCH("P",[#NumSelect])),
IF(INDEX(Table1[ShortName],VALUE(RIGHT([#NumSelect],1)))="",
INDEX(Table1[Name],VALUE(RIGHT([#NumSelect],1))),
INDEX(Table1[ShortName],VALUE(RIGHT([#NumSelect],1)))),
IF(INDEX(Table2[ShortName],[#NumSelect])="",
INDEX(Table2[Name],[#NumSelect]),
INDEX(Table2[ShortName],[#NumSelect])))
A pseudo-code could look like this:
=IF(ISNUMBER(A),IF(B="",C,B),IF(D="",E,D))
The issue is that B (lines 2 & 4) and D (lines 5 & 7) are repeated expressions.
Hopefully, this will help someone to make a major improvement.
Microsoft 365
Using the LET function, you could use the following:
=LET(iIndex,[#NumSelect],sIndex,VALUE(SUBSTITUTE(LOWER(iIndex),"p","")),
IF(LEN(iIndex)>LEN(sIndex),
LET(nShort,INDEX(Table1[ShortName],sIndex),nLong,INDEX(Table1[Name],sIndex),
IF(nShort="",nLong,nShort)),
LET(nShort,INDEX(Table2[ShortName],sIndex),nLong,INDEX(Table2[Name],sIndex),
IF(nShort="",nLong,nShort))))
Welp, I figured out the formula. But it's very inefficient. I'm sure someone here could make it a lot shorter and more efficient.
Here it is:
=IF(
INDEX(FILTER(CHOOSE(IF(LOWER(LEFT([#NumSelect],1))="p",1,2),Table1[[Name]:[ShortName]],Table2[[Name]:[ShortName]]),CHOOSE(IF(LOWER(LEFT([#NumSelect],1))="p",1,2),Table1[Name],Table2[Name])<>""),SUBSTITUTE(LOWER([#NumSelect]),"p",""),3)
=0,
INDEX(FILTER(CHOOSE(IF(LOWER(LEFT([#NumSelect],1))="p",1,2),Table1[[Name]:[ShortName]],Table2[[Name]:[ShortName]]),CHOOSE(IF(LOWER(LEFT([#NumSelect],1))="p",1,2),Table1[Name],Table2[Name])<>""),SUBSTITUTE(LOWER([#NumSelect]),"p",""),1),
INDEX(FILTER(CHOOSE(IF(LOWER(LEFT([#NumSelect],1))="p",1,2),Table1[[Name]:[ShortName]],Table2[[Name]:[ShortName]]),CHOOSE(IF(LOWER(LEFT([#NumSelect],1))="p",1,2),Table1[Name],Table2[Name])<>""),SUBSTITUTE(LOWER([#NumSelect]),"p",""),3)
)

Updating spreadsheet after altering the original one

I have the following data in spreadsheet A.
name trait1 trait2 nice
0 Adam 29 81 0
1 Barry 17 75 1
2 Chris 62 0 1
I wish to create a spreadsheet B that will be a filtered copy of this data. Namely, let's assume for a moment that I want to filter nice = 1 and am interested only in column name. The copy in spreadsheet B would be as shown below. In spreadsheet B I wish to be adding some extra columns, e.g. education.
name nice education
1 Barry 1 primary
2 Chris 1 university
What I want to achieve is a spreadsheet B that will get updated if anything changes in spreadsheet A. So for example, if I were to change the name Barry to Ben. The spreadsheet B would become the following.
name nice education
1 Ben 1 primary
2 Chris 1 university
Similarly (and what I find to be the hardest), if a row is added in spreadsheet A, e.g.
name trait1 trait2 nice
0 Adam 29 81 0
1 Barry 17 75 1
2 Matt 69 11 1
3 Chris 62 0 1
The updated spreadsheet B would be as follows:
name nice education
1 Barry 1 primary
2 Matt 1
3 Chris 1 university
So I want the education column to remain the same.
My approach of using a combination of =IF() and =VLOOKUP() functions ultimately did not work. Guess I am really curious about how to connect rows of education to names. So that when a row is added in spreadsheet A, then spreadsheet B gets updated but the education field connected to the new row is empty and will be filled by hand later on.
Since you are looking for a finished product to be in Google Sheets, I'd advise to use QUERY():
Formula in I1:
=QUERY(INDEX({A:D,VLOOKUP(A:A,F:G,2,0)}),"Select Col1,Col4,Col5 where Col4=1")
Note: I made the assumption you pull the education in through a VLOOKUP() (since you mentioned that in the body of the question).

excel : determine if a combination of several cells exists and output result

Considering the following data :
sheet1 (customers)
A
0 customer_name
1 john
2 kevin
3 mickael
sheet2 (products)
A
0 product_name
1 book
2 ball
3 game
sheet3 (orders)
A B
0 customer_name product_name
1 john book
2 john game
3 mickael ball
I would like to know for each combination of customer and product if an order has been ordered and display it in the sheet1 to get something like that :
sheet1 (customers updated )
A B C D
0 customer_name book ball game
1 john 1 0 1
2 kevin 0 0 0
3 mickael 0 1 0
I know how to do that with "code" (by doing a macro in vba or a small exe in c# that will update the file), but I want to do it (if its possible) by just setting a formula inside my sheet (fyi, I can put the 3 inputs in the same sheet if needed, that's not a constraint
Updated :
with the previous configuration described, I have put the following formula in sheet1 B2 : =COUNTIFS(Sheet3!$A:$A;$A2;Sheet3!$B:$B;B$2), and when running the formula and extending it to every cell in my sheet I am getting everywhere the value #NAME? (I've translated if from french so I am not sure if its the right error in english). I think where I am making a mistake is that I am not using sheet2, how can I say first to "do all the combinations possible of customers insheet1 and products in sheet2 and look for those combinations in sheet3, knowing that I am in sheet1 and that I want to display the result like aking before?
Use COUNTIFS:
=COUNTIFS(Sheet3!$A:$A,$A2,Sheet3!$B:$B,B$1)

Index-Match function with NOT EQUAL criteria

I am trying to use the index with match function to give me the order of people based on their rankings. My data is something like this:
A B C
1 Rank Name Score
2
3 1 Joe 100%
4 3 Bob 80%
5 1 John 100%
6 2 Dan 90%
7
8 RankOrder Name
9 1 =index(b3:b6,match(1,a3:a6,-1)) Result = Joe
10 1 =index(b3:b6,match(1&<>"b9",a3:a6,-1)) Result = John **HELP
11 2 =index(b3:b6,match(1&<>"b9&b10",a3:a6,-1)) Result = Dan **HELP
The first formula is to find who has a ranking of 1 or greater, then the second and third formula is where I'm struggling. I need to find who the has the next ranking closest to 1 but has not already been outputted in cell B9. Then the next formula would be the same but not someone that's been outputted in cell B9 & B10. Hopefully this make sense.Thanks!
Use index and aggregate with countif for aggregate's k factor.
=INDEX(B$1:B$6,AGGREGATE(15,6,ROW($3:$6)/(A$3:A$6=A9),COUNTIF(A$9:A9,A9)))

Count X records matching certain conditions and only where there is a corresponding Y record

I am looking for some help with a formula. On my 'data' sheet, I have data in the format below:
A B C D E F G H I J
1 UID RecordType HCode AdmittedDate Forename Surname DOB Sex STDate RDate
2 87962 STAsses STIV1 01/01/2012 Mark Jones 13/07/1978 Male 09/12/2012
3 89658 Transfer GLSI2 01/01/2012 Alison Aitken 20/12/1956 Female 08/07/2013
4 84563 Discharge JHOP1 01/01/2012 David Beckham 09/08/1987 Male 08/07/2013
5 89654 STAsses STGE1 01/01/2012 Andrew Macbeth 27/09/1976 Male 08/07/2012
6 89867 Transfer KIND1 01/01/2012 George Deas 08/05/1989 Male 08/07/2013
7 87962 Transfer STIV1 01/01/2012 Mark Jones 13/07/1978 Male 04/03/2013
8 89654 Transfer STGE1 01/01/2012 Andrew Macbeth 27/09/1976 Male 12/08/2012
On my 'report' sheet, I have the following table set up:
B C D E F
4 HospCode RecordType Jul-12 Aug-12 Sep-12
5 STGE1 Assessments
6 Transfers
7 Discharges
8
What I need is a formula in cell D6 of the 'report' sheet, which counts all 'Transfer' records from the 'data' sheet, occurring in the month specified in D4 and with an 'HCode' matching that in B5 of the 'report' sheet. The tricky part is that I need this formula only to count records which have a corresponding 'STAssess' record.
Might be pushing the limits of Excel here, but anything is possible!
Thanks in advance.
You can simply use
=COUNTIFS(Sheet1!B1:B8, "Transfer", Sheet1!C1:C8, Sheet2!B5)
This is an example with 2 criteria, you can add a lot many more for multiple IFS and COUNTIFS will count those.
In case of finding multiple criteria across rows, you are better off making a Pivot Table out of this data range and then doing multiple selections thereof.
If you would like to accomplish this via formula, there is a two-step process.
THe formula described above needs to be placed next to each Person's name in Sheet1 as an additional column. And then run another COUNTIFS on this column and Column B counting that there is >1 Count for the person existing... and counting if the STAssess criterion is fulfilled or not.

Resources