How to reference one value to various cells, depending on a date? - excel

Please have a look at the picture I attached, it'll make understanding my problem easier because it's hard to describe.
In the first table, I have capacity data for a product. The capacity changes by the date indicated in the column, i.e. from July 2017 the capacity would be 56, from December 2018 78, and from October 2019 99. The reason why I don't write down the capacity for every month is that I want to save columns.
In the second table, I have every month. I want to reference the correct capacity for each month, e.g. it would be 56 for every month until December 2018.
I have been considering an =INDEX function, but it seems to complex for that. Is there a way to reference like this without using VBA? Would the VBA solution be simple? Or am I forced to write a column for every month's capacity in the first table? Thank you!
https://i.imgur.com/mRoBtTo.png

You can simply use several IF statements to compare the month in your column with the months given in your first table, and put the value of the correponding month.
Let's admit your first row is 1 and first column is A, it should give something like:
= IF(D7>=$F$2; $F$3; IF(D7 >= $E$2; $E$3; IF(D7 >= $D$2; $D$3; "")))

I dont see you columns and rows so i hope you will change them correctly on this formula:
=HLOOKUP(C111,$C$106:$P$107,2,TRUE)
C111 is the cell above your red row.
$C$106:$P$107 is the tableof capacities, i know it is bigger then the current one so you see you can add more columns.
2 is the row number from the capacity table.
true is becouse you dont want it to be the exact value it will take the previews in hte order of items

Both previous answers work perfectly, but I would go this way-
You don't actually need an if to find the previous capacity. you can simply use the approximate match (similar to the hlookup answer) in an index formula
=+INDEX($B$4:$E$5,MATCH($B$9,$B$4:$B$5,0),MATCH(C9,$B$4:$E$4,1))
The product $B$9 matches exact (0), but the date C9 is bigger than or equal (1).
$B$4:$E$5 is the source of capacity and
C9:AF9 the date timeline
Final advantage would be that you can have several products to index, not only a single one.

Could you please try the below formula and provide feedback please?
=IF(AND(D8>=D2,D8<E2),"56",IF(AND(D8>=E2,D8<F2),"78",IF(D8>=F2,"99")))

Related

Get sum of cells containing "Vacation" except when related date matches certain day & month

oversimplified i have two columns: Date and Text; I want to check my current amount of vacation days based on the first date in row 2, so i came up with the following formula:
="Available vacation days: "&YEARFRAC(A2;TODAY())*12*(25/12)
I calculate the fraction of the year based on the first date and todays date, multiply it by 12 to get months and multiply it again by the total amount of vacation days in my contract per month. Now i got another formula to collect me all cells in column B containing "Vacation", pretty straight forward:
=COUNTIF(B:B;"Vacation")
Now the interesting part - i got the formula who gives me a boolean if a datetime matches the 24th or 31st of december:
=AND(OR(DAY(A53)=24;DAY(A53)=31);MONTH(A53)=12)
I want to count vacation days happening on a 24th or 31st of december as a half-vacation day (0.5), and otherwise fully (as a 1). Then i want to combine my first statement with this result and subtract the used vacation days. I read about VLOOKUP and XLOOKUP but am unsure if this fits this purpose. I want to avoid having an extra column with my boolean returns and rather have this one cell giving me all the information combined.
Without introducing another column, and using DAY and MONTH
It's nearly impossible, and just unnecessarily so...
Please reconsider this, what will happen if you want to add 4th of July as a holiday?
Your formula =AND(OR(DAY(A53)=24;DAY(A53)=31);MONTH(A53)=12) only works for 1 row at a time. So, we can't ever use it with a list, because you will get the whole list as a result every single time. You can't divide them into smaller lists and join them together, there is no such functionality without VBA.
In the future, do not set arbitrary constraints like "no additional columns", you can hide them if you don't like them. And if you don't need them, remove unnecessary rows like non-vacation rows. They are irrelevant, so why not separate the two.
Just to prove my point, here's the solution you wanted:
Solution
=COUNTIFS(B2:B9;"Vacation") - (COUNT(IFERROR(FILTER(FILTER(FILTER(A2:A9;B2:B9="Vacation");MONTH(FILTER(A2:A9;B2:B9="Vacation"))=12);DAY(FILTER(FILTER(A2:A9;B2:B9="Vacation");MONTH(FILTER(A2:A9;B2:B9="Vacation"))=12))=31);0))+(COUNT(IFERROR(FILTER(FILTER(FILTER(A2:A9;B2:B9="Vacation");MONTH(FILTER(A2:A9;B2:B9="Vacation"))=12);DAY(FILTER(FILTER(A2:A9;B2:B9="Vacation");MONTH(FILTER(A2:A9;B2:B9="Vacation"))=12))=24);0))))*0,5
It works, but it's a pain to read, use and maintain.
A2:A9 refers to the dates column
B2:B9 refers to the text column
So in the future, the last thing you want to do is set arbitrary constraints. Furthermore, why use functions like MONTH and DAY when we can just read the text? That way you could even create a table of holidays to search for instead. That will be no fun task with this setup. (Oh, and if it's because of the year, just strip it away from the text when you want to know only the month and day.
Best of luck!

How to filter data of last record of each week

I have a big data set of daily selling value of a particular ITEM. I want to know what was the price of ITEM on the last day of each week. Typically the last working day is Friday but if you don't have data for Friday then we need to get the previous working day data (Thursday).
Monday is considered the First day of week.
My Data looks something like this:
Data is in cells A2:C13.
My expected output is shown below:
Please help with VB macro or even simple excel formula.
You may want to try using a formula using the LOOKUP function, to search the list from bottom to top.
Afterward, a combination of INDEX and MATCH may get you on the right path as well.
Edit: I realize now that I was leading you astray because I thought you were asking something else! The most straightforward way I can see is as follows:
use WEEKDAY() to pull out the weekday values (as you did), except leave the values as numbers (with 1 being Sunday and 7 being Saturday).
Check each of these days to see if it precedes (i.e. has a lesser value) than the day above it. If not, we know that the week started over, and that cell is the last day of the week. Therefore, display its value.
Of course, this assumes that there are no Saturdays in your data - otherwise, Saturday would be listed as the end of the week. If you're crafty you can fix this dilemma though!
Thanks, Tyler.
Your suggestion helped me a lot in putting efforts in the right direction.
The way I did is as follows:
First I sorted my data in decreasing order of date so that I can have all the latest data at the top.
Range("AW4:BE999999").Sort Key1:=Range("BC4:BC999999"), order1:=xlDescending, Header:=xlYes
From Date created a string of "YEAR"&"WEEKNUM". This way I was able to group all the days in a specific week. Formula Used is:
=(TEXT(BC5,"yyyy"))&(TEXT(WEEKNUM(BC5),"00"))
Then I gave a unique Number to each record. The best way I could think of is to give the row number where record belongs.
=ROW(AY5)
Now using VLOOKUP function I got all the records matching string I have created in step 2
=VLOOKUP(AZ5,AZ:BD,5,FALSE)
I applied the above formula to get all the columns that I need.
Now I removed all the duplicate rows using below formula:
Cells.RemoveDuplicates Columns:=Array(1)
Now the remaining rows are the expected rows.
There may be a better way to do this but this is my first time with excel macro and formulas, so feeling happy.
Please comment other better ways to do this. It's always good to keep on improving our work.

Excel function for First Row and Last Row of group

I have a gate keeping report with a number of entry/exit times for an employee over a 24hr period.
I need another formula to go into I40 which is the difference between the first entry time - last entry time for each employee eg. I40 = F50 - D40.
Dont worry about the formula regarding the subtraction of dates as I have this. I really just need the formula that will allow me to get the Last Exit time cell and the First Entry time cell for each employee.
The best way is to always store datetime values (ie, 2018-05-24 13:454) instead of just the times. You could still display it as a time by changing the cell's formatting to a time format.
Shortcut to Number Formatting options: Ctrl+1
There are many advantages, including that "regular math" will still work even if a shift starts in a different day than it ends.
If you must stick with only times, you can still calculate it correctly (up to a 23.9-hour shift) with an IF statement to add a day if the returned value is negative.
For example, if your existing formula works for same-day shift, and is:
=F50-D40
...then you could change it to:
=IF(F50-D40<0,F50+1-D40,F50-D40)
More Information:
Office.com : How to use dates and times in Excel
Office.com : Add or subtract time (Excel)
EDIT:
Looking at your question again, perhaps I misunderstood what you were trying to ndo. It's a little unclear, but you mention the fist and last times.
If you mean the "earliest and latest", you can get those using MIN and MAX. If the crossing-midnight is an issue here too, you'll need to see my first suggestion above, or else add a "helper column" to determine which times are before which.
Storing datetime is still best and this all would have been avoided.
try the below to get the difference in hours.
=(E50+F50)-(C40+D40)

Excel - What is the easiest way to calculate incidence plus prevalence over time?

Say I have the dataset below, what is the most efficient formula to fill the cells in column D, where the number of patients alive are calculated?
Example data set in excel
The way it should calculate is:
month 1: 8*100% = 8
Month 2: 8*80%+6*100% = 12.4
Month 3: 8*75%+6*80%+9*100% = 19.8
...
Month 10: etc.
The problem that I have is that which each row, the formula becomes longer. It is feasible to just manually enter the formulas for small datasets, but as datasets become larger, this task becomes unfeasible.
I have been able to use VBA to code the survival of the number of new patients column (C). But then I would have to rerun the VBA code as soon as I change a single value in that column.
I have a feeling it should be possible with some combination of the INDEX function in excel, I just haven't been able to figure it out.
Who can help me out here?
Kind regards,
Sander
If moving the data a bit is allowed at least for the calculation, you could do something like this:
=SUMPRODUCT($F$11:$F$20,B2:B11)
It uses a reversed list of your current list of new patients. That list is created with (formula obtained from this site):
=INDEX($C$11:$C$20,COUNTA($C$11:$C$20)+ROW($C$11:$C$20)-ROW())
Result:
The added space is necessary for the formula to work (so that it gets 0% for patients not present yet).
Or one where you don't have to leave spaces (everything from above is reversed however):
=SUMPRODUCT($C$2:$C$11,G11:G20)

Excel Formula IF(AND #VALUE! Error

I am working on a financial model in excel. If the number of customers is between 1-10000, the cost is .20 per customer per month, 10000-100000 is .15 per customer per month, 100k and 1MIL is .10 per customer per month and > 1MIL is .08 per month per customer.
What I would like to do is create a formula where if the cell that references the number of customers at that month is within those values above, the cost per month changes depending on the number of cstomers.
This is what I have:
=IF(AND(B6>=1,B6<=10000),$Q$6), IF(AND(B6>10000,B6<=100000),$Q$7), IF(AND(B6>100000,B6<=1000000),$Q$8),IF(AND(B6>1000000),$Q$9)
Q6, Q7, Q8, Q9 are respectively: $.20, $.15, $.10, $.08
My B6 cell is the cell that is pulling over the number of customers from another sheet.
I am getting a #VALUE! Error when I use this formula. It works if I simply have:
=IF(AND(B6>=1,B6<=10000),$Q$6) which leads me to believe that my logic is wrong with all of the IF statements, and I should be using ELSEIF but I am not sure the syntax for that.
Help is appreciated!
So this is how I fixed this issue in case anyone had the same issue:
=IF(B6<=10000,$Q$6,IF(B6<=100000,$Q$7,IF(B6<1000000,$Q$7)))
By nesting the if statements with higher values excel automatically recognizes the max value for that if statement!
It seems that you are closing the IF statements too soon. Your formula repaired would look like,
=IF(AND(B6>=1,B6<=10000),$Q$6, IF(AND(B6>10000,B6<=100000), $Q$7, IF(AND(B6>100000,B6<=1000000), $Q$8, IF(AND(B6>1000000),$Q$9))))
If you start at the upper limit, you can reduce the conditions with sequential logic.
=IF(B6>1000000,$Q$9, IF(B6>100000, $Q$8, IF(B6>10000, $Q$7, IF(B6>=1,$Q$6, 0))))
You could probably do that a little more easily like this
=IF(B6>1000000,$Q$9,IF(B6>100000,$Q$8,IF(B6>10000,$Q$7,IF(B6>=1,$Q$6,"Error))))
You don't need AND because the IF functions are implicitly checking a range because the previous IFs rule out ranges of values

Resources