Excel Array formula, use info from one IF result as criteria in another IF? - excel

Context
I'm building a financial dashboard, but I'm having troubles to get a formula that fit my client's need.
I'm consolidating amount in different currencies, but for a special indicator,
I need to build a YTD with the Exchange Rate of the last month.
Something like :
(Amount_$_Jan + Amount_$_Feb)*ExRate_$_Feb + (Amount_£_Jan + Amount_£_Feb)*ExRate_£_Feb
OR
(Amount_$_Jan + Amount_$_Feb + Amount_$_Mar)*ExRate_$_Mar + (Amount_£_Jan + Amount_£_Feb + Amount_£_Mar)*ExRate_£_Mar
My issue
In the data, I have multiple currencies and they'll be more to come, so I cannot list the currencies.
I'm trying to :
get the value of the currency of each line that matches the criteria of the first IF
to use it in my second IF to find the exchange range for that currency
for the month I'm calculating for,
with: Named_Rg[Currency]=Named_Rg[Currency]
which is obviously always true, but it is the only syntax I've tried that I could validate...
I've tried :
Named_Rg[Currency]=[#[Currency]]
Named_Rg[Currency]=[Currency]
But both are giving errors (I'm using that formula outside of the table Named_Rg)
I know I can write a function in VBA, but I'd prefer to keep an xlsx.
My formula
I've removed some tests, like testing the year, which are not pertinent for the question.
I'm using it on a another sheet that the one where the table Named_Rg is :
{=SUM(IF(Named_Rg[Month]<=MONTH(X$5);Named_Rg[Amount]*IF(AND(Named_Rg[Month]=MONTH(X$5);Named_Rg[Currency]=Named_Rg[Currency]);Named_Rg[Chg to €];0);0))}
How can I refer to the Row/Currency found with the first IF in the second one?
Sample Data
That is just a sample, I'll have multiples rows per month and currency.
Year Month Currency Chg to € Amount
2017 1 EUR 1 20
2017 1 USD 0.6 30
2017 1 LST 2 40
2017 2 EUR 1 200
2017 2 USD 0.7 300
2017 2 LST 2.2 400
2017 3 EUR 1 2000
2017 3 USD 0.8 3000
2017 3 LST 2.4 4000
CSV format :
Year;Month;Currency;Chg to €;Amount
2017;1;EUR;1;20
2017;1;USD;0.6;30
2017;1;LST;2;40
2017;2;EUR;1;200
2017;2;USD;0.7;300
2017;2;LST;2.2;400
2017;3;EUR;1;2000
2017;3;USD;0.8;3000
2017;3;LST;2.4;4000
Expected results :
YTD last chg (Jan) : 118 = 20*1+30*0.6+40*2
YTD last chg (Feb) : 1419 = (20+200)*1+(30+300)*0.7+(40+400)*2.2
YTD last chg (Mar) : 15540 = (20+200+2000)*1+(30+300+3000)*0.8+(40+400+4000)*2.4

Array formula do not like the AND() or OR() operators. They need to be substituted with * or + respectively.
So your:
AND(Named_Rg[Month]=MONTH(X$5);Named_Rg[Currency]=Named_Rg[Currency])
Should be:
(Named_Rg[Month]=MONTH(X$5))*(Named_Rg[Currency]=Named_Rg[#Currency])
So the formula would be:
=SUM(IF(Named_Rg[Month]<=MONTH(X$5);Named_Rg[Amount]*IF((Named_Rg[Month]=MONTH(X$5))*(Named_Rg[Currency]=Named_Rg[#Currency]);Named_Rg[Chg to €])))
Remember that this is an array formula and needs to be confirmed with Ctrl-Shift-Enter
But I think you want this formula instead to get the desired output:
=SUMPRODUCT(SUMIFS(Named_Rg[Amount],Named_Rg[Month],"<=" & MONTH(X5),Named_Rg[Currency],Named_Rg[Currency])*(Named_Rg[Month]=MONTH(X5))*(Named_Rg[Chg to €]))
Change the , to your ; for your local settings.

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))

Calculate 3 month Average on the base of CustomerID

I am trying to calculate three month average sales in excel w.r.t customerid in excel. I tried by doing it by AverageIfs function but nothing helped.
A B C
Orderdate sales customerid
5/15/2019 7 1
5/15/2019 48.5 1
4/15/2019 92.94 1
3/17/2019 102.85 1
3/18/2019 49 1
3/18/2019 119.95 1
2/18/2019 58.96 1
1/20/2019 14.6 1
5/16/2019 17 6
4/15/2019 148.5 6
4/12/2019 912.94 6
3/17/2019 102.85 6
9/18/2018 22.34 6
Formula I tried: =AVERAGEIFS(B:B,C:C,C2)
output expected:
customerid average(3 months)
1 49.48
6 359.48
Let's start from today's date and the date 3 months ago (Make it dynamic):
Remember to change the cell format from General to Date. Otherwise, it will show [43563]
Next use the date as part of our filter:
Now you should get the most recent 3 months data:
Copy the filtered data into a new spreadsheet
Copy the filtered data into a new spreadsheet
Copy the filtered data into a new spreadsheet
Next Step: get the distinct customer ID:
You will get this:
Last Step:
Use the function "AVERAGEIF":
Done!

Determine number of days within a date range across multiple years

I hope I can explain this properly. I need to calculate occupancy time for a population of migratory animals. Occupancy is defined as the period, in days, between the first and last sighting of an individual in a given year between 1999 and 2015. A sample of the type of sighting data I am dealing with is pasted below. I need to be able to calculate the number of days between sightings within a year and NOT between the first and last sightings or between each sighting. Further, I need a value of "1" if the animal was only seen once in that year, rather than a value of "0".
So, for example, based on individual 37 in the data below, the results table would look something like
1999 - 0 2000 - 11 2001 - 40 2002 - 2 2003 - 0
1999 - 0
2000 - 11
2001 - 40
2002 - 1
2003 - 0
2004 - 52
2005 - 1
...and so on.
I have tried this series of equations but it does not return the right values for the years in which an animal was seen only once and it ignores any years prior to the first year seen.
in cell K2:
=DATE(YEAR(MIN($B$2:$J$2)),1,1)
in cell L2:
=IF(N(K2)=0,"",MAX(MAX(IF($B$2:$J$2=K2,$B$2:$J$2)),0))
in cell M2:
=IF(N(K2)=0,"",IF(MAX($B$2:$J$2)>DATE(YEAR(K2)+1,1,1),DATE(YEAR(K2)+1,1,1),""))
so if more than 1 year between sightings then it should be 0?
and is that more than "365 days a year" or if it is spotted 1 time 1. december and 1 time 1. januar then its still only spotted 1 time pr year...
I was able to find a solution using this equation in the cells immediately following the last sighting.
=IF(COUNTIFS($B2:$V2,">="&DATE(BE$1,1,1),$B2:$V2,"<="&DATE(BE$1,12,31))>0,LOOKUP(DATE(BE$1,12,31),$A2:$V2)-INDEX($A2:$V2,MATCH(DATE(BE$1,1,0),$A2:$V2)+1)+1,0)

How to calculate workdays and compensation for given period based on Employee's monthly salary?

I have only three editable fields:
salary = 15,000
start date = 18 Jul 2014
end date = 12 Oct 2014
With these fields, I need to calculate the total salary I need to pay. Payment is on a monthly basis:
(JULY = 15,000 / 31 * 14) +
(AUG = 15,000 / 31 * 31) +
(SEPT = 15,000 / 30 * 30) +
(OCT = 15,000 / 30 * 12).
I can get total days based on both dates (ie 87 days, =DAYS($enddate,$startdate)+1) ) but I need to split the days according to the months.
What formula do I need automatically to get the amount, because each person will have different salary and different dates?
You can use Excel VBA custom Function to solve your problem:
1). First, you should populate Excel Worksheet with data structure reflecting Employees Name, Monthly Salary, StartDate and EndDate, like in the following sample:
Employee M.Salary Start Date EndDate
John $15,000.00 7/18/2014 10/12/2014
Ann $20,000.00 7/19/2014 10/13/2014
Peter $16,000.00 7/20/2014 10/14/2014
Jeff $25,000.00 7/21/2014 10/15/2014
2). The DAYS in date range can be simply found by subtraction (EndDate-StartDate), because the underlying data type in integer
3). For general solution to the problem (Calculate the compensation for any arbitrary period and monthly salary) you will need to create custom VBA formula and use it in a separate column for each Employee. Refer to this article for explanation: Create a custom worksheet function in Excel VBA
4). Pertinent to your particular case with fixed date rage, the simplified solution based on the Excel Worksheet formulas (no VBA) is described below:
Employee MoSalary Start End Days FullMo FirstMo LastMo Total
John $15,000.00 7/18/2014 10/12/2014 87 30000.00 $6,774.19 $6,000.00 $42,774.19
Ann $20,000.00 7/18/2014 10/12/2014 87 40000.00 $9,032.26 $8,000.00 $57,032.26
Peter $16,000.00 7/18/2014 10/12/2014 87 32000.00 $7,225.81 $6,400.00 $45,625.81
Jeff $25,000.00 7/18/2014 10/12/2014 87 50000.00 $11,290.32 $10,000.00 $71,290.32
4a). In column E starting with row 2 add formula for DAYS : =(D2-C2)+1 and extend it for entire range
4b). In column F starting with row 2 add formula for whole months : =2*B2 and extend it for entire range
4c). In column G starting with row 2 add formula for the first month : =14*B2/31 and extend it for entire range
4d). In column H starting with row 2 add formula for last month : =12*B2/30 and extend it for entire range
4e). In column I starting with row 2 add formula for total compensation : =SUM(F2:H2) and extend it for entire range
Hope this will help. Best regards,
I have split this out because I am not entirely sure what you require and you may be able to assemble the pieces in a way that better suits you:
Assuming your data is in A1:A3, put 1/7/14 in C1, 1/8/14 in D1, 1/9/14 in E1 and 1/10/14 in F1.
To count the number of applicable days by month, in C2 enter:
=IF(MONTH($A2)=MONTH(C1),EOMONTH(C1,0)-$A2+1,IF(MONTH($A3)=MONTH(C1),DAY($A3),EOMONTH(C1,0)-EOMONTH(C1,-1)))
To compute the salary for the month by computing the daily rate for the applicable month and multiplying that by the number of days from above, in C3 enter:
=$A1*C2/(DAYS(EOMONTH(C1,0),C1)+1)
Format C2:C3 to suit and copy across to F2:F3.

How to vlookup on 2 columns and return total value of another?

I've got a table in Excel which is structure like so:
Month Date Time ID Name Currency Value
Jan 07/01/14 5 1234567 Ted GBP 500
Jan 10/01/14 12 1234567 Ted GBP 723
Feb 23/02/14 6 9877654 John GBP 300
Feb 10/02/14 10 1234567 Ted GBP 333
What I need to do is write a formula which basically returns be the total of Value where ID and Month are equal to whatever the lookup values are. For example, using the above I would say:
Find the total of Value where Month equals Jan and ID equals 1234567.
The answer in this case would be 1223.
Ive just tried
=SUMIFS(INPUT!H:H,INPUT!D:D='TRANS BY MID'!B2,INPUT!A:A='TRANS BY MID'!C1)
INPUT!H:H is my ID column
INPUT!D:D='TRANS BY MID'!B2 is the ID I want to use
INPUT!A:A is the Month column
TRANS BY MID'!C1 is Jan
To provide a working solution I simplified your question into one sheet. The data appears like this:
You can link your other sheet to the values shown in column J.
The formula is now this:
=SUMIFS(G:G,D:D,J1,A:A,J2)
The result is shown in J7:

Resources