Finding the closest date, that is most recent date - 1 year's value - excel

I have the following table, table1:
date
value
05/04/2022
400
22/04/2022
312
04/05/2022
942
06/08/2022
231
12/08/2022
243
I want to get the most recent date in the table (in this case 12/08/2022) and minus 1 year from it to get 12/08/2021. Then look at the table to find the value that is most closest to 12/08/2021. In this case, the closest value is from the table is 05/04/2022, so the formula will return 400.
I tried with this formula, but really unclear how to do it.
=VLOOKUP(IFERROR(MAX(Table1[date])-365,MAX(Table1[date])), Table1, 2,TRUE)
I have Microsoft Excel 2020

If you have Excel 365 you can use this formula:
=LET(mostRecentDate,MAX(table1[date]),
previousYearDate,mostRecentDate-365,
MIN(FILTER(table1[date],table1[date]>previousYearDate))
)
If you don't have the FILTER-function try this:
=INDEX(table1[date],MATCH(MIN(ABS(table1[date]-(MAX(table1[date])-365))),ABS(table1[date]-(MAX(table1[date])-365)),0))
It checks for the differences to the max-date - 365

Related

Excel - How to do the following?

I have a question related to EXCEL:
Since I have the following prices for different period, how can I compute in an automatic way, for example the price between 10 June - 15 July ?
EDIT--> Expected output:
50€+50€ (10-11 june) + 58€x13 (period 12 june - 25 June) + 75€x13 (period 26 june - 9 July) + 92€x5 (period 10 july-15july) = 2289€
This is the same pictures with the cells in excel, please do not consider the "Price of period", but only the daily price
You could do this with a (lengthy) calculation:
Consider:
A8 Contains the from date (10-06-2021)
B8 Contains the from date (17-07-2021)
=(MIN($B$8,D3)-MAX($A$8,C3)+1)*C5+(MIN($B$8,F3)-MAX($A$8,E3)+1)*E5+(MIN($B$8,H3)-MAX($A$8,G3)+1)*G5+(MIN($B$8,J3)-MAX($A$8,I3)+1)*I5
This will consider a period from 10-06 - 11-06 a period of 2 days. And also 12-06 - 25-06 a period of 14 days, and so on.
The result is: 2514.00
One method would be to create a lookup table and use a formula.
Given your data, your lookup table might as shown below:
I NAME'd it: priceTable
You can then use the formula:
=SUM(VLOOKUP(SEQUENCE(End-Start+1,,Start,1),priceTable,2))
If, for some reason, you cannot easily create priceTable, you can create it with a formula (depending on your version of Excel):
=INDEX($C$4:$I$6,{1,3},{1;3;5;7})
Note: All of the formulas assume your dates are real dates and not strings.
If your version of Excel does not have the SEQUENCE function, you can replace it with:
=SUM(VLOOKUP(ROW(INDEX($A:$A,Start):INDEX($A:$A,End)),priceTable,2))

Compare values previous date and second previous date in PowerPivot

I'm new to PowerPivot and DAX. I've followed some on-line tutorials. Now I have a small problem that I can't solve. I have the following data:
Date Instrument Value
2016-07-27 A 100
2016-07-27 B 98
2016-07-26 A 102
2016-07-25 B 99
For each date I would like to calculate the difference (Profit/Loss) in Value between most recent date and second most recent date. For the data above it would be the following:
Date Instrument Value Profit/Loss
2016-07-27 A 102 2 ([Val. inst. A 2016-07-27]-[Val. inst. A 2016-07-26])
2016-07-27 B 98 -1 ([Val. inst. B 2016-07-27]-[Val. inst. B 2016-07-25])
2016-07-26 A 100
2016-07-25 B 99
I have tried with DAX to find the second largest date using =EARLIER([Date])but haven't managed to get it to work. With the second largest date I would maybe be able to find the Value corresponding to that date. Any suggestions how this could be solved?
In the end I came up with a solution in three steps (the steps can be combined into one step). First I rank all the dates, with the most recent being 1 and the second most recent being 2. After that I retrieve the Value for the second most recent day for each row. Finally I calculate the difference between the Values current row and the Value for the second most recent day compared to that row's date.
To rank the dates I used the following:
=RANKX(FILTER(ALL(table);EARLIER([Instrument])=[Instrument]);[Date];;FALSE())
Explanation to what I think the DAX formula is doing. RANKX works by taking a table and then rank the values in a column in that table. Above I've used a filtered table as the table. The filtered table creates a new table for each row containing only the same instrument as the instrument for that particular row. For the first row, the filtered table would look like below.
Date Instrument Value
2016-07-27 A 100
2016-07-26 A 102
The dates in that filtered table is then ranked.
Date Instrument Value Rank
2016-07-27 A 100 1
2016-07-26 A 102 2
Using the Rank I then pull out the second most recent dates Value for each row based on the current row's Rank-1.
Value second most recent date = CALCULATE(MAX([Value]);FILTER(table;EARLIER([Instrument])=[Instrument] && [Date Rank]= EARLIER([Date Rank]))
Finally I calculate the difference:
PnL = [Value] - [Value second most recent date]
I'm not sure what EARLIER is doing but I think it is some sort of iterative process.

create formula with sumif,if and ,or

I want to make formula with sumif and with conditions i tried but its failing to provide sum.I want to find value if conditions:
1. G/L is 62810101 or 62810100 then value for 81810101 and 81810100 should be calculated or vice versa.
2.If code is A2+B2 then value for B2+A2 should be find in document currency.
i tried something like this... =IF(OR(E2=62810101,E2=62810100),SUMIFS(I:I,L:L,L2,E:E,E2=81810101))
CoCd Tr.Prt DocumentNo Reference G/L Doc. Date Amount in local cur. LCurr Amount in doc. curr. Curr. Code 1 Sum if 1
BG05 RS31 1100000007 111 62810100 42081 2542.58 BGN 1300 EUR BG05RS31 2918
BG05 RS31 1100000017 1100000017 81810101 42234 621.95 BGN 318 EUR RS31BG05 318
Try these formula:
For Accts Receivable:
=SUM(SUMIFS(I:I,L:L,L2,E:E,{62810101,62810100}))
For Accts Payable:
=SUM(SUMIFS(I:I,L:L,L2,E:E,{81810101,81810100}))
It is just a matter of putting the wanted account numbers in the {} brackets.

Conditional averages (AVERAGEIF, AVERAGEIFS, or other option?)

Perhaps it's just been a long week, but I can't think of how to get a pretty simple average.
Here's my data (two columns):
1/3/1994 1165
1/4/1994 1162
1/5/1994 1133
1/6/1994 1133
1/7/1994 1138
1/10/1994 1143
1/11/1994 1118
1/12/1994 1150
1/13/1994 1171
1/14/1994 1177
1/17/1994 1161
1/18/1994 1162
1/19/1994 1121
1/20/1994 1112
1/21/1994 1129
1/24/1994 1136
1/25/1994 1124
1/26/1994 1118
1/27/1994 1127
1/28/1994 1133
1/31/1994 1088
2/1/1994 1055
2/2/1994 1051
2/3/1994 1071
2/4/1994 1079
2/7/1994 1054
2/8/1994 1079
2/9/1994 1079
2/10/1994 1089
2/11/1994 1074
2/14/1994 1083
2/15/1994 1068
2/16/1994 1075
2/17/1994 1071
As you can see, it's a column of dates (that continue until Sept. 9 2015, so it's long), and another of price. I am just trying to get the averages for January each month, of each year (i.e. Jan 1994, 1995, 1996 ... 2015, then Feb 1994, etc).
Here's the table I plan on using the formula in:
2007 2008 2009 2010 2011
January
February
March
April
So, in the cell right of "January" and below "2007", I want the average of prices that are in Jan, 2007.
I tried using this (again, my data starts in A1 and B1):
=AverageIfs(B:B,year(A:A),1994,month(A:A),1) (regular and as array), but it doesn't work - I keep getting the error "The formula you typed contains an error." (I'd really prefer this to be a formula, rather than a VB solution)
Thanks for any ideas!
Edit: In the mean time, I have created two helper columns, that are just the Month() and Year() of each row of data. Then I can use =AverageIfs(B:B,[month helper range],1,[year helper range],2007). Is there a way to do this without a helper column though?
Try this
=AVERAGE(IF(YEAR(A:A)=1994,IF(MONTH(A:A)=1,B:B,""),""))
entered as an array formula (CTRL-SHIFT-ENTER). If you want to use the month as text you could use
=AVERAGE(IF(YEAR(A:A)=1994,IF(TEXT(A:A,"mmmm")="January",B:B,""),""))
Hope that helps
assuming your data has a header: "Date" and "Price" in cells A1, B1.
assuming your data begins in A2 = "1/3/1994" and B2 = 1165
C1 = "Month"
D1 = "Year"
C2 = =TEXT(A2,"Mmmm")
D2 = =YEAR(A2)
Copy Cells C2+D2 down ...
I place your new table in:
H2 = "January"
H3 = "February"
... etc.
I1 = 1994
J1 = 1995
... etc.
I2 = =AVERAGEIFS($B:$B,$C:$C,$H2,$D:$D,I$1)
and copy that formula throughout the table.
Cheers!
Yes, you can use AVERAGEIFS() and you should. This is about a thousand times faster than the accepted answer:
=AVERAGEIFS(B:B,A:A,">="&DATE(1994,1,1),A:A,"<"&DATE(1994,2,1))
You can even do it this way for a more concise formula, but I believe it raises problems for non-USA users because of their date format settings:
=AVERAGEIFS(B:B,A:A,">=1/1/94",A:A,"<2/1/94")
I don't know if this is the most concise solution, but it works. You can use SUMPRODUCT as follows:
=SUMPRODUCT((MONTH($A:$A)=1)*(YEAR($A:$A)=1994)*$B:$B)/SUMPRODUCT((MONTH($A:$A)=1)*(YEAR($A:$A)=1994))
What this is essentially doing is summing the values in column B based on the two criteria, and then counting the number of rows that matched the criteria and dividing by that number.
For each row, the MONTH and YEAR conditions evaluate to either 1 (true) or 0 (false) and then those two values are multiplied with the value in column B, resulting in column B's value if both conditions are true, or 0 if one or both conditions are false.
This solution requires to use in the "table with the results" the number of the month instead of the names of the month
It also assumes that the "table with the results" starts at F2 (see picture)
Then use this formula:
=IFERROR(AVERAGEIFS($B:$B,$A:$A,">="&DATE(G$2,$F3,1),$A:$A,"<="&EOMONTH(DATE(Q$19,$P20,1),0)),"N/A")
The formula shows “N/A” if there are no prices for the period (Year/Month), if you want to see blank then replace it with “”
Small changes done to your sample data to work with several periods

MS Excel: Using AGGREGATE to add up all mileage in each month

I have the following data in a logbook format:
DATE MILEAGE
02-Jul-13 15
05-Jul-13 12
09-Jul-13 156
10-Aug-13 20
11-Aug-13 20
12-Aug-13 232
12-Aug-13 20
13-Aug-13 265
15-Aug-13 20
18-Aug-13 20
I am looking to extract data from it.
I need to ignore errors and #N/A so I have been trying to use the AGGREGATE function. To no avail though.
I would like to present the following information:
Mileage this month -
=AGGREGATE(9,7, IF(MONTH(IFERROR(LogBookTable[Date], 0)) = MONTH(TODAY()), LogBookTable[Total KM], 0)) - Does not work
Mileage in July -
=AGGREGATE(9,7, IF(MONTH(IFERROR(LogBookTable[Date], 0)) = MONTH(7), LogBookTable[Total KM], 0)) - Does not work
Mileage in August -
=AGGREGATE(9,7, IF(MONTH(IFERROR(LogBookTable[Date], 0)) = MONTH(8), LogBookTable[Total KM], 0)) - Does not work
Total Mileage -
=AGGREGATE(9,7,LogBookTable[Total KM]) - This works
The monthly mileage and current month mileage all result in a "#VALUE!" being displayed.
Any assistance would be much appreciated.
Just in case anyone asks, the naming schemes are corrected, it's not the references that aren't working, it's the values.
You could use DSUM. If you had two criteria cells setup Say in D1:E2 as follows:
Date Date
>1/07/2013 <31/7/2013
and if your data was in A1:B11 (as per your example above), to return a sum for July ignoring errors, use the following formula:
=DSUM(A1:B11,2,D1:E2)
You could setup the criteria such that they were updated based on the current month, and that the month end is calculated using:
="<"& DATE(YEAR(TODAY()),MONTH(TODAY())+1,DAY(1)-1)
and the month start calculated by:
=">"& DATE(YEAR(TODAY()),MONTH(TODAY()),DAY(1))
Remember that with criteria, multiple criteria on the same row is an AND, and multiple rows on the same criteria is an OR.
You can use SUMIF is you add a column representing the month of the date:
Column B contains =MONTH of Column A. All you have to do is update the DATE entry in A16 with a new date. B16 is accordingly updated with the appropriate month, creating a new monthly aggregate/sum.

Resources