Related
I am in the process of building a formula to split a total cost (in column J) based on start and end expenditure periods that can vary from 2021 to 2031. Based on the days between the expenditure period dates (column M), I managed to work out to split the cost using the formulas below up to 2023 but it is not consistent and at times incorrect.
In cell P5 I have the following formula. For year 2021, I seem to get the correct split result.
=IF($K5>AS5,0,$J5/$M5*(AS5-$K5))
In cell Q5, I have the following formula. For year 2022, I seem to get the correct spit as well
=MIN(IF(SUM($N5:P5)>=$J5,0,IF($L5>=AS5,$J5/$M5*(AS5-AR5),$J5/$M5*($L5-MAX(AR5,$K5)))),K5)
However, I don't get the right result in cell Q6 which has the same formula but different dates
=MIN(IF(SUM($N6:P6)>=$J6,0,IF($L6>=AS6,$J6/$M6*(AS6-AR6),$J6/$M6*($L6-MAX(AR6,$K6)))),K6)
Cell R6 shouldn't return any result because it is out of date range. This is where things get mixed up.
Note that from column AR to BC, it is all year end dates from 2020 to 2031 as shown below.
Is there a better way to tackle this sort of formula as I seem to get dragged into a long and unreliable way of doing this.
Here single function(♣) that will create a series of pro-rata multipliers (of appropriate length) for any given start/end date:
EDIT: see end of soln for extended version per OP comment to original soln...
SINGLE FUNCTION
=J11*(LET(dates,EDATE(DATE(YEAR($K11),1,1),12*(SEQUENCE(1,YEAR($L11)-YEAR($K11)+2,1))),IF(dates<K11,K11,IF(dates<L11,dates,L11)))-LET(dates,EDATE(DATE(YEAR($K11)-1,1,1),12*(SEQUENCE(1,YEAR($L11)-YEAR($K11)+2,1))),IF(dates<K11,K11,IF(dates<L11,dates,L11))))/(L11-K11)
It may appear somewhat unwieldy in length, but it is far more robust (and concise) compared to the combination of steps/series you have created. What's more, it returns the precise answer RE: pro-rata payments and is guarenteed to never over/under-run RE: total payment (by design).
BREAK-DOWN
Comprises 3 distinct parts (some of which are similar in pattern/formation):
1] First part - create a series (array) of years spanning start-end dates:
=LET(dates,EDATE(DATE(YEAR($K5)-1,1,1),12*(SEQUENCE(1,YEAR($L5)-YEAR($K5)+2,1))),IF(dates<K5,K5,IF(dates<L5,dates,L5)))
Thanks to the lovely Spill functionality the new Office 365 variant Excel boasts, you never have to worry about how many years are required -- so long as you have the space to the right of this workbook (would be unusual otherwise - assuming you start in column O and clear any content to the right of this, you'd need an end date beyond the year 2557 (26th century) to run out of columns! ☺
2] Second part is merely a replica of the firs series, albeit shifted to the right 'once' (so starts with the 2nd element in the 1st series):
=LET(dates,EDATE(DATE(YEAR($K5),1,1),12*(SEQUENCE(1,YEAR($L5)-YEAR($K5)+2,1))),IF(dates<K5,K5,IF(dates<L5,dates,L5)))
3] Third part - you have the basic ingredients from parts 1 and 2 to complete the required task easily: simply deduct series 2 from 1 (giving days between successive dates in series 1 - i.e. days for each year between start and end dates), divide by total days (to yield pro-rata multipliers), and then multiply these by the total £amount and voila - you have your series!
=J5*(O6#-O5#)/(M5)
♣ Caveat(s) - assuming you have Office 365 compatible version of Excel (which is quite common nowadays)
*EDIT - EXTENDED VERSION
Given above, the following extends this to align monetary results (1st table, o11:w21) within respective calendar period columns (spanning the entire period in question).
This soln:
Determines header row based upon the number of columns & corresponding calender periods (financial yrs commencing 1/1) as an array function (i.e. dynamic range)
Utilises a modified version of the eq. provided for dates arrage (refer: "First Part", original soln)
Comment - same caveats as before - i.e. Office 365 etc.
Screenshot(s)/here refers:
DATES (HEADER) - Y10 (array)
=LET(y_,MIN(K11:K21),x_,MAX(L11:L21),EDATE(DATE(YEAR(y_)-1,1,1),12*(SEQUENCE(1,YEAR(x_)-YEAR(y_)+2,1))))
Comment - enter once within single cell Y10 - i.e. as an array function with Spill to right
ALIGNED/SHIFTED FINANCIALS - Y11:Y21 (each cell in col is an array)
=IFERROR(IF(Y$10#<EDATE(K11,-12),"",IF(Y$10#>EDATE(L11,12),"",INDEX(O11#,1,MATCH(Y$10#,EDATE(DATE(YEAR($K11)-1,1,1),12*(SEQUENCE(1,YEAR($L11)-YEAR($K11)+2,1))),0)))),"")
Comment - enter this as an array fn. (#SPILL! to the right) in each cell within column Y (can drag this function down Y11:Y21 as required)
I have 2 Nested IF Statements..
=IF($B3=$K$3,DATE(YEAR(D3)+$L$3,MONTH(D3)+$M$3,DAY(D3)),IF($B3=$K$4,DATE(YEAR(D3)+$L$4,MONTH(D3)+$M$4,DAY(D3)),IF($B3=$K$5,DATE(YEAR(D3)+$L$5,MONTH(D3)+$M$5,DAY(D3)),IF($B3=$K$6,DATE(YEAR(D3)+$L$6,MONTH(D3)+$M$6,DAY(D3)),""))))
and
=IF($C3=$O$3,DATE(YEAR(D3)+$P$3,MONTH(D3)+$Q$3,DAY(D3)),IF($C3=$O$4,DATE(YEAR(D3)+$P$4,MONTH(D3)+$Q$4,DAY(D3)),IF($C3=$O$5,DATE(YEAR(D3)+$P$5,MONTH(D3)+$Q$5,DAY(D3)),"")))
which work as required in two separate columns. What I would like is to Nest them again so that depending on the choice made from a Data Validation dropbox (Choice 1 or Choice 2, in the following) it either displays the date calculated from the first Choice, or from the second. This is what I have, but I can't get it to work. I think I need an OR in there, but I don't know where, as there are so many IFs already.
=IF($I$2=”Choice1”,IF($B3=$K$3,DATE(YEAR(D3)+$L$3,MONTH(D3)+$M$3,DAY(D3)),IF($B3=$K$4,DATE(YEAR(D3)+$L$4,MONTH(D3)+$M$4,DAY(D3)),IF($B3=$K$5,DATE(YEAR(D3)+$L$5,MONTH(D3)+$M$5,DAY(D3)),IF($B3=$K$6,DATE(YEAR(D3)+$L$6,MONTH(D3)+$M$6,DAY(D3)))),IF($I$2=”Choice2”,IF($C3=$O$3,DATE(YEAR(D3)+$P$3,MONTH(D3)+$Q$3,DAY(D3)),IF($C3=$O$4,DATE(YEAR(D3)+$P$4,MONTH(D3)+$Q$4,DAY(D3)),IF($C3=$O$5,DATE(YEAR(D3)+$P$5,MONTH(D3)+$Q$5,DAY(D3))),""))))))
If it was just one column of dates I can use this..
=IF($I$2="Choice 1",$E4, IF($I$2="Choice 2",$F4,""))
with a couple of helper columns, but I could have as many as 10, which would require 20 helper columns on a spreadsheet that's already 70 odd columns wide, which I'd rather avoid if possible. I've done this on a test worksheet, rather than mess about with the original for now.
EDIT Additional information as requested. (Edited to generic terms as it's a work item)
I have these columns
and using the following dropdown..
would like the date in "Choice 1 OR 2 Increase" to update depending on the selection of either "Choice 1" or Choice 2" information chosen from either of the two sets of Category dropdowns..
This formula takes the information from the first date dropdown and correctly calculates the new date..
=IF($B3=$K$3,DATE(YEAR(D3)+$L$3,MONTH(D3)+$M$3,DAY(D3)),IF($B3=$K$4,DATE(YEAR(D3)+$L$4,MONTH(D3)+$M$4,DAY(D3)),IF($B3=$K$5,DATE(YEAR(D3)+$L$5,MONTH(D3)+$M$5,DAY(D3)),IF($B3=$K$6,DATE(YEAR(D3)+$L$6,MONTH(D3)+$M$6,DAY(D3)),""))))
with Category 1 - 4, in that order, being the four date sections of this formula.
This formula takes the information from the second date dropdown and correctly calculates the new date..
=IF($C3=$O$3,DATE(YEAR(D3)+$P$3,MONTH(D3)+$Q$3,DAY(D3)),IF($C3=$O$4,DATE(YEAR(D3)+$P$4,MONTH(D3)+$Q$4,DAY(D3)),IF($C3=$O$5,DATE(YEAR(D3)+$P$5,MONTH(D3)+$Q$5,DAY(D3)),"")))
with Category 5-7, in that order being the three date sections of this formula.
Both formulas work correctly when used independently of each other to populate individual columns, and a third column can be used to get the date required (using the dates already provided separately in two "helper" columns) using the following formula..
=IF($I$2="Choice1",$E3, IF($I$2="Choice2",$F3,""))
What I would like is to be able to combine the actions of both of the above formulas somehow, so that by using the Choice 1/Choice 2 dropdown (Pic 2), it calculates the date depending on the choice in that dropdown box, and also the other two dropdown boxes (Pics 3 + 4) and populates the "Choice 1 OR 2 Increase" column (Pic 1)
If I was only using this one example, I'd just use the helper columns but there will be several, and the sheet is around 70 columns wide already. Combining (at least the actions of) these two formulas into one, will save a lot of extra columns.
I hope that's the information you require..
Thanks
Liam
I would set things up differently, and use a different formula.
Since your Categories and Choices do not overlap, I would combine all the categories into a single table.
eg:
Since both Years and Months can have a varying number of days, I'd suggest using EDATE for the additions.
I also used a Named Range for the Choice dropdown,
and a Table for the Category table.
Doing that, you can use this formula (assuming your data table is in A1:Cn)
=EDATE(C2,VLOOKUP(INDEX($A2:$B$5,1,MATCH(Choice,$A$1:$B$1,0)),Category,2,FALSE)*12 + VLOOKUP(INDEX($A2:$B$5,1,MATCH(Choice,$A$1:$B$1,0)),Category,3,FALSE))
The formula
MATCH decides which column of category choice to use
VLOOKUP to return the appropriate years and months
EDATE(Date, years*12 + months)
If you prefer to use your method of adding datevalues, you can apply the same methodology:
=DATE(YEAR(C2)+VLOOKUP(INDEX($A2:$B$5,1,MATCH(Choice,$A$1:$B$1,0)),Category,2,FALSE),MONTH(C2)+VLOOKUP(INDEX($A2:$B$5,1,MATCH(Choice,$A$1:$B$1,0)),Category,3,FALSE),DAY(C2))
Note that the two methods can give different results depending on the day of the month, and whether the end month has the same number of days as the start month.
eg:
31 January 2020 + 1 month --> 29 Feb 2020 vs 02 Mar 2020
31 January 2020 + 3 month --> 30 Apr 2020 vs 01 May 2020
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")))
I'm looking a way to add an extra column in a pivot table that that averages the sum of the count for the months ("Count of records" column) within a time period that is selected (currently 2016 - one month, 2017 - full year, 2018 - 5 month). Every month would have the same number based on the year average, needs to be dynamically changing when selecting different period: full year or for example 4 months. I need the column within the pivot table, so it could be used for a future pivot chart.
I can't simply use average as all my records appear only once and I use Count to aggregate those numbers ("Count of records" column).
My current data looks like this:
The final result should look like this:
I assume that it somehow can be done with the help of "calculated filed" option but I couldn't make it work now.
Greatly appreciate any help!
Using the DataModel (built in to Excel 2013 and later) you can write really cool formulas inside PivotTables called Measures that can do this kind of thing. Take the example below:
As you can see, the Cust Count & Average field gives a count of transactions by month but also gives the average of those monthly readings for the subtotal lines (i.e. the 2017 Total and 2018 Total lines) using the below DAX formula:
=AVERAGEX(SUMMARIZE(Table1,[Customer (Month)],"x",COUNTA(Table1[Customer])),[x])
That just says "Summarize this table by count of the customer field by month, call the resulting summarization field 'x', and then give me the average of that field x".
Because DAX measures are executed within the context of the PivotTable, you get the count that you want for months, and you get the average that you want for the yearly subtotals.
Hard to explain, but demonstrates that DAX can certainly do this for you.
See my answer at the following link for an example of how to add data to the DataModel and how to subsequently write measures:
Using the Excel SMALL function with filtering criteria AND ignoring zeros
I also recommend grabbing yourself a book called Supercharge Excel when you learn to write DAX by Matt Allington, and perhaps even taking his awesome online course, because it covers this kind of thing very well, and will save you significant head-scratching compared to going it alone.
Trying to show a rolling average of pieces produced from the last 5 work days.
Currently I am tracking all days Mon-Sun and taking the last 5 days.
Here is my current formula:
=SUMIFS(AL:AL,AK:AK,">="&TODAY()-5,AL:AL,"<="&TODAY())/5
With AL:AL being the date column and AK:AK being the pieces produced.
I Can't figure how to arrange the data and dates to get my last 5 days average.
This is not difficult with a helper column. As you indicated in the question, the arrangement is important. First, organize your data with the most recent values at the top. So AL1 will always hold today's date.
In AM1 enter:
=IF(OR(WEEKDAY(AL1)=1,WEEKDAY(AL1)=7),"",1)
In AM2 enter:
=IF(OR(WEEKDAY(AL2)=1,WEEKDAY(AL2)=7),"",IF(SUM(AM$1:AM1)=5,"",1))
(column AM defines the values to be averages)
Then in another cell enter:
=SUMPRODUCT(--(AM1:AM10=1)*(AK1:AK10))/5
Some beginners place the most recent data at the bottom of a column. This usually make it more difficult to analyze.
I think your SUMIFS() function is incorrectly reproduced. The first argument is the "sum_range," the data to be summed, so that should be the number of pieces.
Also, you are getting 6 days including today. If you want 5 days (and if you have data for today), then you have to subtract 4 instead of 5.
You can accomplish what you want with this formula, I think:
=SUMIFS(AK:AK,AL:AL,">="&WORKDAY(TODAY(),-4),AK:AK,"<="&TODAY())/5
If you don't actually have data for today, then subtract 5 instead of 4.
This also depends entirely on your not having any data for weekend days. If you have production data for weekend days, then the averages will be off (they will include extra days).
If you have data for both weekdays and weekend days, you will have to use something like #garysstudent suggested.