Conditional Formatting based on date and value in Excel - excel

I am trying to return the color for a score based on the date for the score and the score itself. Scoring has used different cut-offs over time:
Table 1
Date1 Score Color
Sep-16 24 [should be red]
Jul-16 6 [should be green]
Apr-14 12 [should be yellow]
... ... ...
Table 2
Date2 Red Orange Yellow Green
Aug-16 20 15 9.5 0
Jul-16 20 15.5 9.5 0
Apr-16 20 15 9.5 0
Mar-15 19 14 7 0
Feb-15 20 13 8.5 0
Jan-15 19 14 7 0
Apr-14 19 14 7 0
I want to place a formula in the "Color" cell that will evaluate Table 2 and return the column name for instances where the date in date1 is the most recent instance where it is greater than date 2, and for which the score given on table 1 is equal to or larger than the score given on table 2 for the correct row.
Thanks,

You need nested approximate lookups. This would be easier if your data was sorted the other way around. At least table 2 should have the columns in ascending order, instead of descending, so the match function can return the correct position of the number with an approximate match.
If you can arrange the columns in Table2 in the order Date2, Green, Yellow, Orange, Red, then the following formula will be possible.
=INDEX(Table3[[#Headers],[Green]:[Red]],MATCH([#Score],INDEX(Table3[Green],IFERROR(MATCH([#Date1],Table3[Date2],-1),1)):INDEX(Table3[Red],IFERROR(MATCH([#Date1],Table3[Date2],-1),1)),1))
This uses structured references, which accommodates rows being inserted into the tables without breaking the formulas.
Now you can use conditional formatting based on the cell values in column C.

Just for comparison, I have chosen to keep the lookup table (in Sheet2 rather than in an actual table) the same as in the question i.e. both tables are sorted from largest to smallest or most recent to least recent and the MATCHes both have -1 as the third argument:-

Related

How to count items in Excel within a date range or without an end date

Note: date formats are DD/MM/YYYY
I have a list of records, each with one column for a start date, and one for an end date.
Every record has a start date, but if an item is current it has no end date and the end date cell is blank.
I want to write a/some formulas to determine how many records were a given age at a given date, rounded down to the nearest whole year.
So for example, how many records were 0-1 years old at the date at (cell reference R1), and then how many were 1-2 years, 2-3 years etc.
I want this to be reusable so that I can update the date at R1 each month and it recalculates automatically. This is easy enough for R1=TODAY, as I can assume all end dates are in the past, but for R1=EDATE(TODAY,-12) it becomes trickier.
As an example, in the yellow highlighted cell I want to calculate how many records were between 1&2 years old as of 30/06/21 (S1), AND were current at the time (i.e. exclude from the count any records that have an end date before 30/06/21).
The blue highlighted area is my data, the green area is what I'm trying to calculate. I don't mind adding an extra data column or two if it assists in the calculation, but I don't want to have to add an extra column for every year that I'm trying to calculate, if it can be avoided.
Start Date
End Date
Years (as of 30/06/2022)
Age
30/06/2022
30/06/2021
30/06/2020
30/06/2019
30/06/2018
30/06/2017
30/06/2016
30/06/2015
30/06/2014
30/06/2013
20/09/2021
0.77
13
0
7/09/2020
4/12/2020
0.24
12
0
6/08/2019
2.90
11
0
17/02/2020
2.37
10
0
1/04/2019
3.25
9
0
16/03/2020
18/11/2020
0.68
8
0
17/08/2021
19/11/2021
0.26
7
0
23/08/2022
-0.15
6
0
16/11/2020
1/04/2022
1.37
5
0
20/04/2020
21/10/2021
1.50
4
0
7/05/2019
26/02/2021
1.81
3
2
29/06/2020
7/01/2021
0.53
2
5
16/08/2021
20/04/2022
0.68
1
5
0
13
I created a table for the data (insert table) --> tblData
=LET(calculatedAge,MAP(tblData[Start Date],tblData[End Date],
LAMBDA(startdate,enddate,
ROUNDDOWN((MIN(IF(ISBLANK(enddate),E$1,enddate),E$1)-startdate)/365,0))
),
filteredAges,FILTER(calculatedAge,calculatedAge=$D2),
IFERROR(ROWS(filteredAges),0))
The MAP-function returns the calculated age per target date (E$1) - rounded down.
It simulates a helper column - that then can be filtered.
Thanks to #Robert Mearns for the FILTER-hack - as COUNTIF doesn't work in this scenario (s. Using LET with COUNTIF and Array, e.g. MAP)

Excel formula help - I have 14/28 day readings averaged per day which need to be recorded as daily readings

Dates when the data was read are shown on the date column on the left, then worked out a value per day shown in ET/DAY column.
To analyze monthly data etc etc I want daily data so every day has the ET/DAY value between the dates it was read and inserted in the et/day column.
I have entered in manually what I want in blue but need a formula to do this as too long to do all manually
You can do this with MATCH. With an ascending list, if you give the magic number 1 as the third parameter, it will show the last row number with a smaller or equal number.
Here it's matched 17 with 15 in column A and the result is 2, for row 2:
A
B
C
1
10
=MATCH(17,A:A,1) = 2
2
15
3
20
So for your data, you can match the daily dates with the column on the left, and feed the row number into INDEX to look up from the ET/Day column.
A
B
C
D
E
1
Date
ET/Day
Daily
ET/Day
2
05/Jan/1995
05/Jan/1995
=Index(B:B, Match(#D:D,A:A,1)) = #N/A
3
19/Jan/1995
3.00
06/Jan/1995
=Index(B:B, Match(#D:D,A:A,1)) = 3.00
4
02/Feb/1995
5.41
07/Jan/1995
=Index(B:B, Match(#D:D,A:A,1)) = 3.00
5
...
...
6
19/Jan/1995
=Index(B:B, Match(#D:D,A:A,1)) = 3.00
7
20/Jan/1995
=Index(B:B, Match(#D:D,A:A,1)) = 5.41
There's an inconsistency here with the 05 Jan rate but it should be good after that.

Subtotals grouped by value using COUNTIF to create ranges for SUMIF, but in a single formula

End-goal: A column with the subtotals of groups (defined in the below table as all foods listed above Zucchini, incl Zucchini).
Current attempt: create a column to define groups using COUNTIF('count all 'zucchini' thus far'). Then use SUMIF to get the total cost for the current group.
Problem: I don't know how to do this without the COUNTIF column (since SUMIF needs range C:C to be resolved first). I'd like to have it in a single formula. I looked into array formulas but not sure if/how to apply that here.
FOOD COST COUNTIF(A2:A$2;"Zucchini") SUMIF(C:C;C2;B:B)
Apple 3 0 12
Pecan 7 0 12
Zucchini 2 0 12
Apple 4 1 23
Olive 8 1 23
Pecan 6 1 23
Zucchini 5 1 23
Apple 4 2 16
Olive 9 2 16
Zucchini 3 2 16
Any ideas on how to solve either the current problem or the end-goal problem? Thanks!
Put this in C2 and copy down:
=IF(A2="Zucchini",SUM($B$1:B2)-SUM($C$1:C1),"")
It basically sums everything to the row and subtracts what is already accounted for.

Excel using SUMIF to calculate totals of multiple columns

I'm trying to use Excle's SUMIF to calculate totals of Col1 to Col5 for dates that are similar.
My formula is as follows =SUMIF($A2:$A7,A10,$B2:$F7), but this only gives me the total of a single column.
How can I get the Totals of all the columns based on the date like I've shown in my results.
Date Col 1 Col 2 Col 3 Col 4 Col 5
1/5/2017 1 2 2
1/5/2017 5 3 1
1/5/2017 9 5 5
2/5/2017 10 5 3
2/5/2017 20 10 3
2/5/2017 6 8 1 5
Desired Results
1/5/2017 15 7 7 3 1
2/5/2017 30 11 11 11 8
use below formula in cell B11
=SUMIF($A$2:$A$7,$A11,B$2:B$7)
Per the example you provided, One solution is to use SUMPRODUCT
Multiplies corresponding components in the given arrays, and returns the sum of those products
Microsoft Docs give a thorough example, but per SO etiquette, here is an example in case of link-rot: [FYI, I used absolute reference for easier filling across, arbitrary how you get it done though]
Forumlas shown:
Formula is kind of hard to see without clicking on image:
=SUMPRODUCT(($B$3:$B$8=$B$11)*C3:C8)
This basically breaks down like this, it searches the B:B column for a match, and it will naturally return a true or false for the match, or 0/1 counterparts, and multiplys that by the number found in the column to the right (C3:C8), so it will either be 1 * # = # or 0 * # = 0

Extracting the upper quartile data from an array and placing it in new column

Hi and thanks for any help with this in advance
Below is a hypothetical data set; abundance = count data; mud% = the mud content in which the animals were found; mud bin = bins i've made up depending on the mud%; and UQ = upper quartile of the abundance data from its corresponding mud bin (i.e. the upper quartile for the abundance data in mud bin 1 is 17.25 etc).
Problem:
In excel, for abundance data in each of the four mud bins, I'm wanting to extract any values in the abundance column that are >= the upper quartile value for that particular mud bin and place these in a new column on the same sheet (with no gaps between rows from values that didn't meet the criteria) along with their corresponding mud% value in the neighboring cell. I've added the new columns to the below sheet to give you an idea of what I'm after.
abundance | mud% | mud bin | UQ | | New column | Mud% |
18 10.9 1 18 10.9(mud bid 1)
15 6.5 1 44 38.9(mud bin 1)
6 13.4 1 45 38 (mud bin 2)
13 42.1 1 37 37.8(mud bin 2)
15 36.4 1 etc
44 38.9 1 17.25 etc
22 46 2
30 36.4 2
45 38 2
29 35.3 2
37 37.8 2
29 41.8 2 35.25
11 44.4 3
17 47.8 3
21 40.7 3
15 13.9 3
35 13.9 3
14 13.9 3
15 13.9 3 19
19 12 4
14 12 4
10 12 4
12 12 4
14 12 4
13 12 4
45 9.525 4
66 9.525 4
78 9.525 4 45
The reality is I have a rather large dataset containing abundance data for a number of species, all on the same excel sheet and would greatly appreciate any insight into how I might achieve this in the most efficient manor.
For starters, to make this explanation simpler, I will assume that the last row of data is in row 100.
Populate Upper Quartile values for all line items
First you'll need to use the Quartile formula; however, since you want to find the upper quartile within a bin, you'll have to use an array formula. Put this formula in your UQ column (place in cell D2 and drag down). When entering the formula Be sure to press Ctrl+Shift before pressing Enter
=QUARTILE(IF($C$2:$C$100=C2,$A$2:$A$100,""),3)
The first part of this formula, $C$2:$C$100=C2 is your condition. Everywhere this condition is met, you will get the corresponding value in $A$2:$A$100; otherwise, you'll get a blank value. This will give you an array of abundance values that matches the indicated mudbin, C2. now that you have your subset of data, the quartile function will give you the value in the 3rd quartile (17.25 for mudbin 1, which will be placed next to every row that has a mudbin of 1).
Now that we have all the quartiles, we can get all the abundance values that are greater than the UQ for that mudbin. This is done in two parts
Get abundance values greater than mudbin UQ
First, you need to select one column of cells that has the same number of rows as your data (for example, select cells F2:F100)
Enter the following formula into the formula bar (while F2:F100 are highlighted) and press Ctrl+Shift, then enter
=IF($A$2:$A$100>$D$2:$D$100,$A$2:$A$100,"")
Similar to the IF statement used before, this formula finds all the abundance values that are greater than their corresponding UQ value. Now column F will have an abundance number where it is greater than it's UQ value, and a blank where it is not. Now onto the final step.
Populate abundance values that are greater than the UQ value, without the blanks
Select G2:G100 (your "New Column" in your sample data)
Enter the following formula into the formula bar (while G2:G100 are highlighted) and press Ctrl+Shift, then enter
=INDEX(F2:F100,SMALL(IF(F2:F100<>"",ROW(F2:F100)-1),ROW()-ROW($F$1)))
Looking at the IF statement again, this will find every value in F2:F100 that is not blank, but instead of grabbing the values, we'll keep track of the row number of that non blank value (done by ROW(F2:F100)-1
). Now that we have the row numbers of all the non blank values, we can grab the non-blank values in order and populate them in G2:G100. ROW()-ROW($F$1) is a counter, and SMALL will use the counter to determine the nth smallest number to return. Once we have our row number of the non blank value, INDEX returns that value
Finally, to populate the Mud%, you'll need to use the row number of the non blank values to get the mud% and the mud bin (You have the formula already to get the row number of the non blank value).
It's not a simple answer, but at least you won't have to use VBA.

Resources