Lookup rate with Index/Match with two criteria including one date range - excel

In our Consulting business, we charge our clients according to a personal billing rate. These rates can change over time. To be able to invoice customers for any service back in time, I want to build an Excel sheet that can facilitate this process.
+--------------+------------+------------+-----------+
| Name | Begin | End | Rate |
+--------------+------------+------------+-----------+
| Paul Brown | 2016-01-01 | 2016-01-31 | $10.00 |
| Paul Brown | 2016-02-01 | 2016-03-02 | $20.00 |
| Paul Brown | 2016-03-03 | 2016-04-02 | $30.00 |
| Paul Brown | 2016-04-03 | 2016-05-03 | $40.00 |
| Anna Red | 2016-02-15 | 2016-03-16 | $100.00 |
| Anna Red | 2016-03-17 | 2016-04-16 | $127.00 |
| Anna Red | 2016-04-17 | 2016-05-17 | $145.00 |
| Martin Blue | 2016-01-01 | 2016-04-30 | $300.00 |
| Martin Blue | 2016-05-01 | 2017-02-25 | $400.00 |
| Susan Yellow | 2014-01-03 | 2014-12-29 | $10.00 |
| Susan Yellow | 2014-12-30 | 2016-08-21 | $30.00 |
| Susan Yellow | 2016-08-22 | 2016-09-21 | $50.00 |
| Susan Yellow | 2016-09-22 | 2016-10-22 | $190.00 |
| Susan Yellow | 2016-10-23 | 2016-11-22 | $200.00 |
| Susan Yellow | 2016-11-23 | 2016-12-23 | $210.00 |
+--------------+------------+------------+-----------+
In my Excel Sheet, I want to be able to enter the name of the person and any date and it should give me the correct billing rate.
So e.g. typing Susan Yellow and 08/26/16 should return $50 because it fell in this date range.
+-------------+------------+----------+
| Susan Yellow | 2016-08-26 | $50.00 |
+-------------+------------+----------+
Dates before the first billing rate should default to the first known one, and ones with no current billing rate should default to the last known one (e.g. 01/02/2018).
Normally, I would just use an Index/Match formula but my issue is that I cannot combine criteria / build a helper column because that breaks the date range function. I could define custom table arrays instead, but I do not know how long each search area is because some consultants can have quite the history and others might be short.
Anyone have a clue with what formula I can solve this in Excel/Google Sheets?
Thank you!

Edit:
SottCraner's formula is much neat and simple.
=SUMIFS(D:D,A:A,F3,B:B,"<=" & G3,C:C,">=" & G3)
This should work : {=SUM(IF(F3=$A$2:$A$16,IF((G3>=$B$2:$B$16)*(G3<=$C$2:$C$16),$D$2:$D$16,0),0))}

This will deal with the two situations where the date is not in ranges as required:
It is an array formula, so with required name in G1, and required date in H1, enter it in I1 with
Ctrl-Shift-Enter
=INDEX($D:$D,MAX(MAX(IF($A$2:$A$16=$G$1,IF($H$1>=$B$2:$B$16,ROW($C$2:$C$16)))),MATCH($G$1,$A:$A,0)))

Related

Counting Time within ranges in table

So I did look through a couple of existing answers, but they were all in some programing language (ie, RUBY, PHP, etc). I also managed to figure out a way to do what I want but some of my formulas felt either hard coded or verbose. So my question is, is there a cleaner way to write my formulas to achieve my goal.
What I am starting with is a simple line to calculate cost of electricity usage. It look like:
+─────────────+────────+───────+───────+─────────+──────────+───────────+───────────+────────+──────────+───────────+───────────+──────────+──────────────+─────────────+
| | | | | | KW used | | | | Rate | | | Usage | Delivery | Total Cost |
| Date | Start | Stop | KW/H | Season | On-Peak | Mid-Peak | Off-Peak | Total | On-Peak | Mid-Peak | Off-Peak | Cost | Cost | |
+─────────────+────────+───────+───────+─────────+──────────+───────────+───────────+────────+──────────+───────────+───────────+──────────+──────────────+─────────────+
| 2022/12/17 | 05:00 | 22:00 | 1.44 | Winter | 0 | 0 | 24.48 | 24.48 | 0.17 | 0.113 | 0.082 | 2.00736 | 1.141296768 | 3.15 |
+─────────────+────────+───────+───────+─────────+──────────+───────────+───────────+────────+──────────+───────────+───────────+──────────+──────────────+─────────────+
The first 4 columns are user entry fields and correspond to columns B through E. The remainder of the columns F through P are all formulas.
My seasonal time bands are contained in the following table
+─────────+──────────────+─────────────+───────────+──────────+──────────+──────────+
| Season | Start | Stop | Time | Start | Stop | Rate |
| | | | Period | (HH:mm) | (HH:mm) | ($/Kwh) |
+─────────+──────────────+─────────────+───────────+──────────+──────────+──────────+
| Summer | May 01 | October 31 | Off-Peak | 00:00 | 07:00 | 0.082 |
| | | | Mid-Peak | 07:00 | 11:00 | 0.113 |
| | | | On-Peak | 11:00 | 17:00 | 0.17 |
| | | | Mid-Peak | 17:00 | 19:00 | 0.113 |
| | | | Off-Peak | 19:00 | 24:00 | 0.082 |
+─────────+──────────────+─────────────+───────────+──────────+──────────+──────────+
| Winter | November 01 | April 30 | Off-Peak | 00:00 | 07:00 | 0.082 |
| | | | On-Peak | 07:00 | 11:00 | 0.17 |
| | | | Mid-Peak | 11:00 | 17:00 | 0.113 |
| | | | On-Peak | 17:00 | 19:00 | 0.17 |
| | | | Off-Peak | 19:00 | 24:00 | 0.082 |
+─────────+──────────────+─────────────+───────────+──────────+──────────+──────────+
Note: weekends and holidays are Off-Peak
To aid with formula reading (or so I thought) I used the following named ranges:
Season_1 =$B$75 ("Summer")
Season_1_Start =DATE(YEAR(TODAY()),5,1) ("May 01")
Season_1_End =DATE(YEAR(TODAY()),10,31) ("Oct 31")
Season_1_TimeRates =$E$75:$H$79 ("Off-Peak" to 0.82)
Season_2 =$B$80 ("Winter")
Season_2_Start =DATE(YEAR(TODAY()),11,1) ("Nov 01")
Season_2_End =DATE(YEAR(TODAY()),4,30) ("Apr 30")
Season_2_TimeRates =$E$80:$H$84 ("Off-Peak" to 0.82)
TimeRates =IF($F3="Summer",Season_1_TimeRates,Season_2_TimeRates)
Step 1
Determining the season in F4
I used the following formula in F4.
=IF(AND($B3>=DATE(YEAR($B3),MONTH(Season_1_Start),DAY(Season_1_Start)),$B3<=DATE(YEAR($B3),MONTH(Season_1_End),DAY(Season_1_End))),Season_1,Season_2)
It seamed reasonable and substituted the year of the actual entry for making date comparison. I took the approach of if its not within the range for summer, then by default it has to be winter. Part of me is thinking I should check the winter date check. If both date checks fail then toss up a check date statement or the like.
Step 2
Determine how many hours are in each category according to the table and place results in G through I. This is where things go a little ugly for me. After chasing my tail in circle for a little bit I said screw it and plunged in with some hard coding and got something that worked but looked rather ugly and lengthy to me
I started out by figuring out that each time range check had 6 possible outcomes. I boiled those outcomes down to 2 possible results: 0 or Formula.
SB = Start Before
SI = Start Inside
SA = Start After
FB = Finish Before
FI = Finish Inside
FA = Finish After
So the results were either 0 because the time range in C and D are outside of the Time range of the table. Or its some form of Finish - Start.
I couldn't figure out how to make it count the hours in the range using the title (ie On-Peak) because there were sometimes more than one entry depending on the season in F. So what I did as a working step was to create a formula that would count the hours for each of the 5 time ranges instead of the names of the time range:
To get the hours I used the following formulas for each row:
=IF(OR($D3<=INDEX(TimeRates,1,2),$C3>=INDEX(TimeRates,1,3)),0,IF($D3<=INDEX(TimeRates,1,3),$D3,INDEX(TimeRates,1,3))-IF($C3<=INDEX(TimeRates,1,2),INDEX(TimeRates,1,2),$C3))*24
=IF(OR($D3<=INDEX(TimeRates,2,2),$C3>=INDEX(TimeRates,2,3)),0,IF($D3<=INDEX(TimeRates,2,3),$D3,INDEX(TimeRates,2,3))-IF($C3<=INDEX(TimeRates,2,2),INDEX(TimeRates,2,2),$C3))*24
=IF(OR($D3<=INDEX(TimeRates,3,2),$C3>=INDEX(TimeRates,3,3)),0,IF($D3<=INDEX(TimeRates,3,3),$D3,INDEX(TimeRates,3,3))-IF($C3<=INDEX(TimeRates,3,2),INDEX(TimeRates,3,2),$C3))*24
=IF(OR($D3<=INDEX(TimeRates,4,2),$C3>=INDEX(TimeRates,4,3)),0,IF($D3<=INDEX(TimeRates,4,3),$D3,INDEX(TimeRates,4,3))-IF($C3<=INDEX(TimeRates,4,2),INDEX(TimeRates,4,2),$C3))*24
=IF(OR($D3<=INDEX(TimeRates,5,2),$C3>=INDEX(TimeRates,5,3)),0,IF($D3<=INDEX(TimeRates,5,3),$D3,INDEX(TimeRates,5,3))-IF($C3<=INDEX(TimeRates,5,2),INDEX(TimeRates,5,2),$C3))*24
I wound up hard coding the entries because ultimately these are going to wind up in the formula for G3 through I3. Otherwise I could have used the Range # column as index points.
So the next problem I had was adding ranges with the same name together. Could have used SUMIF if I was keeping the table, but in my head I could not as A) I was not keeping the table, B) This would eventually need to be in a row that could be copied down.
So I looked at what needed to be done for each season and it was not bad...IF the formula was short.
So basically need an IF statement looking for summer and follow the addition of appropriate seasonal ranges for each range type. Seems simple enough but it gave me the following formulas for G3 through I3:
=$E3*IF(OR(WEEKDAY($B3)={1,7}),0,IF($F3="Summer",IF(OR($D3<=INDEX(TimeRates,3,2),$C3>=INDEX(TimeRates,3,3)),0,IF($D3<=INDEX(TimeRates,3,3),$D3,INDEX(TimeRates,3,3))-IF($C3<=INDEX(TimeRates,3,2),INDEX(TimeRates,3,2),$C3)),IF(OR($D3<=INDEX(TimeRates,2,2),$C3>=INDEX(TimeRates,2,3)),0,IF($D3<=INDEX(TimeRates,2,3),$D3,INDEX(TimeRates,2,3))-IF($C3<=INDEX(TimeRates,2,2),INDEX(TimeRates,2,2),$C3))+IF(OR($D3<=INDEX(TimeRates,4,2),$C3>=INDEX(TimeRates,4,3)),0,IF($D3<=INDEX(TimeRates,4,3),$D3,INDEX(TimeRates,4,3))-IF($C3<=INDEX(TimeRates,4,2),INDEX(TimeRates,4,2),$C3))))*24
=$E3*IF(OR(WEEKDAY($B3)={1,7}),0,IF($F3="Summer",IF(OR($D3<=INDEX(TimeRates,2,2),$C3>=INDEX(TimeRates,2,3)),0,IF($D3<=INDEX(TimeRates,2,3),$D3,INDEX(TimeRates,2,3))-IF($C3<=INDEX(TimeRates,2,2),INDEX(TimeRates,2,2),$C3))+IF(OR($D3<=INDEX(TimeRates,4,2),$C3>=INDEX(TimeRates,4,3)),0,IF($D3<=INDEX(TimeRates,4,3),$D3,INDEX(TimeRates,4,3))-IF($C3<=INDEX(TimeRates,4,2),INDEX(TimeRates,4,2),$C3)),IF(OR($D3<=INDEX(TimeRates,3,2),$C3>=INDEX(TimeRates,3,3)),0,IF($D3<=INDEX(TimeRates,3,3),$D3,INDEX(TimeRates,3,3))-IF($C3<=INDEX(TimeRates,3,2),INDEX(TimeRates,3,2),$C3))))*24
=$E3*IF(OR(WEEKDAY($B3)={1,7}),$D3-$C3,IF(OR($D3<=INDEX(TimeRates,1,2),$C3>=INDEX(TimeRates,1,3)),0,IF($D3<=INDEX(TimeRates,1,3),$D3,INDEX(TimeRates,1,3))-IF($C3<=INDEX(TimeRates,1,2),INDEX(TimeRates,1,2),$C3))+IF(OR($D3<=INDEX(TimeRates,5,2),$C3>=INDEX(TimeRates,5,3)),0,IF($D3<=INDEX(TimeRates,5,3),$D3,INDEX(TimeRates,5,3))-IF($C3<=INDEX(TimeRates,5,2),INDEX(TimeRates,5,2),$C3)))*24
So the question is, is there way to use formulas that would allow for the tidying up of what is currently in place for cells G3 to I3 (last 3 formulas in this question) that can be copied downward?
UPDATE as requested
I am working on the table on the top, below it is my building block area for thoughts. You may recognize some of the shots above from it.
Using Excel 2013

Formula to get the month of the last value

I have sales data by customer as follows:
| - | A | B | C | D | E | F | G |
|---|---------------|--------|--------|--------|--------|--------|--------|
| 1 | Customer Name | Jan-18 | Feb-18 | Mar-18 | Apr-18 | May-18 | Jun-18 |
| 2 | Mr.A | 1000 | 500 | 0 | 200 | 0 | 0 |
| 3 | Mr.B | 0 | 300 | 200 | 0 | 0 | 100 |
I need the formula to know the last sales of the respective customer booked (the name of the month)
in this case, Mr. A last order is in Apr-18 while Mr.B is in Jun-18.
I have 2,000 plus customer and sales data since Apr 2016 up to last month, it will be a huge time saving to have a formula to help.
Assuming your 'months' are dates, not Text. Courtesy #barry houdini:
=LOOKUP(2,1/(B2:G2<>0),B$1:G$1)
in Row2 and copied down to suit, formatted mmm-yy.
Ref
An alternative to using LOOKUP() as in this answer, not sure what impact it has performance-wise as both need to create an array but I would take a stab in the dark that this is less performant:
=INDEX($B$1:$G$1,,MAX((B2:G2<>0)*COLUMN(B2:G2)-1)) - Ctrl+Shift+Enter
Ofcourse this could be edited to a dual lookup on the customer too:
=INDEX($B$1:$G$1,,MAX(INDEX(($B$2:$G$3<>0)*COLUMN($B$2:$G$3)-1,MATCH("Mr.B",$A$2:$A$3,0),0)))
This doesn't require the CSE as INDEX() handles the array manipulation

Excel Sum product values and stock then multiplie when multiple criteria

So I have this information:
+---------------+---------+-------+------------+
| Chocolate | Brand | Stock | Sale value |
+---------------+---------+-------+------------+
| Chokito | Nestlé | 1520 | $3,50 |
| Snickers | Mars | 3300 | $5,20 |
| Snickers 2 | Mars | 500 | $2,50 |
| Kit Kat | Nestlé | 2000 | $9,10 |
| Double Decker | Cadbury | 1000 | $2,50 |
| Idaho | Mars | 0 | $6,10 |
| Caramello | Cadbury | 350 | $7,50 |
| Cadbury Daily | Cadbury | 1000 | $3,10 |
| Almond Joy | Hershey | 500 | $1,50 |
| Twix | Nestlé | 999 | $4,50 |
| Zero Bar | Hershey | 488 | $5,50 |
+---------------+---------+-------+------------+
Wha I want to get the total stock value for each brand. I get these values by inserting a column of of stock * value then doing a Pivot Table
Cadbury $8.225,00
Hershey $3.434,00
Mars $18.410,00
Nestlé $28.015,50
But what I want to do is a formula in Excel that will get this same values.
I first tried using SUMIF but obvioulsy it didnt worked xD
I cant think of any other formula
Thanks for your help
Try,
=SUMPRODUCT((C$2:C$12), (D$2:D$12), --(B$2:B$12=G4))
For a dynamic length of data,
=SUMPRODUCT((C$2:INDEX(C:C, MATCH(1E+99, C:C))), (D$2:INDEX(D:D, MATCH(1E+99, C:C))), --(B$2:INDEX(B:B, MATCH(1E+99, C:C))=G4))
Alternative approach using sumif
Place the following in E2 and copy down
=D2*E2
this will give the value you of each individual chocolate level in stock
in column G generate a list of brands
in H2 use the following formula and copy down as needed
=SUMIF(B:B,G2,E:E)

Create Distinct ID by Group in Excel without sorting

I have an Excel column that cannot be sorted and for which I need to create a unique id by group, similar to what is below:
+--------+------+
| Name | ID |
+--------+------+
| Jim | 1 |
| Sarah | 1 |
| Tim | 1 |
| Jim | 2 |
| Rachel | 1 |
| Sarah | 2 |
| Jim | 3 |
| Sarah | 3 |
| Rachel | 2 |
| Tim | 2 |
+--------+------+
You can do this with a simple COUNTIF() and getting a little creative with your cell references:
=COUNTIF($A$1:$A1, A2) + 1
Put that in B2 (assuming your list with headers starts in A1) and then copy down.
COUNTIF() here is counting the number of times the name in the adjacent cell has appears in all of the cells above it. As you copy it down, that range will grow to include all cells between A1 and the next row up.

Count distinct occurrences and averages in a column based on identifiers in another column

In MS Excel, I want to count the number of distinct categories (ignoring a specific item) based on a different column. Also, I want to find the average and the max for the same selection. This is the data:
+--------+-----------+-------+
| Person | idea | score |
+--------+-----------+-------+
| George | vacuum | 9 |
| George | box | 6 |
| George | x | 1 |
| Joe | scoop | 4 |
| Joe | x | 1 |
| Joe | x | 1 |
| Joe | scoop | 4 |
| Joe | gear | 7 |
| Mike | harvester | 10 |
| Mike | gear | 7 |
| Mike | box | 6 |
+--------+-----------+-------+
The result should be the following:
+--------+----------------+------------+-----------+
| Person | distinct ideas | Avg. score | Max score |
+--------+----------------+------------+-----------+
| George | 2 | 5.3 | 9 |
| Joe | 2 | 3.4 | 7 |
| Mike | 3 | 7.7 | 10 |
+--------+----------------+------------+-----------+
Because Joe has two "scoop" and one "gear" idea, and I want to ignore the "x" items.
I reluctantly gave up and did it manually for each person, e.g., this is for the first person:
SUM(IF(FREQUENCY(MATCH(B2:B4,B2:B4,0),MATCH(B2:B4,B2:B4,0))>0,1))-IF(COUNTIF(B2:B4,"x")>0,1,0)
Doesn't Excel have functions to return a range instead of a value? If I could select the range based on the name of the person in the first columns, I could count distinct occurrences or find the average in another column.
Add a 4th column and label it Distinct Ideas
If your table starts in A1, then:
EDIT: Formula changed to exclude "x". Screen shot also changed
D2: =IF(TRIM($B2)="x",0,IF(SUMPRODUCT(($A$2:$A2=A2)*($B$2:$B2=B2))>1,0,1))
and fill down.
Then construct a Pivot table
Person to Row Labels
Distinct Ideas to Values area
score to Values and select to Average
Score to Values area and Select Max
Format as desired. Here is one result:

Resources