How to count occurrences of each year in Excel? - excel

I want to count which cells have a specific year in their date/string.
I have a problem where my formula only works if it's a valid date, some cells have month or day missing or are totally blank.
Here are some examples of values I want to be able to count:
2002-07-?
2010-11-27
2009-10-21
2009-10-21
2004-12-20
2004-11-07
2010-11-?
2004-09-17
2000-?-?
2005-04-26
This is how I want the output to be:
Unknown 2
2000 1
2001 0
2002 1
2003 0
2004 3
2005 1
2006 0
2007 0
2008 0
2009 2
2010 2
If I use =COUNTIF(A1:A12;"2000*") I only get those cells which are strings. Is there a way I could count both dates and strings?

Use a helper column and use the following formula to extract the year:
=IF(ISTEXT(A1);LEFT(A1;4);TEXT(A1;"YYYY"))
Then use your existing =COUNTIF() formula but without the wildcard * argument:
=COUNTIF(A1:A12;"2000")

Haven't got Excel to hand to test this, but I believe you can have another column that converts the value to text- I think it's =TEXT(A1,"<format>"), then just do your 'countif' on that.
EDIT: Forgot about the second argument. I'm surprised it didn't work with the 'yyyy' argument though.

Related

Excel formula to count how many times a part number is used in a top level assembly (no UNIQUE or FILTER)

I have a list of part numbers that are used in 4 different top level assemblies. The parts can be used in 1 to 4 of the top level assemblies. I'm trying to write a formula that will count how many unique top level assemblies a part number occurs in. I had previously written a formula that worked, but it uses UNIQUE and FILTER, and my coworkers don't have Excel 365, so those formulas aren't supported for them. I've been trying to come up with a workaround and would really appreciate any help :)
I have an example (I can't provide any real data) section of our spreadsheet and an image of the formula I had that was working
Top Level Assy
Part Number
Qty
Number of times used
02554
01622
4
3
89975
01622
4
3
95665
01622
4
3
98886
01723
4
1
98886
01723
10
1
98886
01723
4
1
02554
01734
4
3
89975
01734
4
3
95665
01734
4
3
02554
01740
6
3
89975
01740
6
3
95665
01740
6
3
02554
01746
5
3
89975
01746
5
3
95665
01746
5
3
02554
01835
2
3
89975
01835
2
3
95665
01835
2
3
02554
51205
4
3
=SUM(--(LEN(UNIQUE(FILTER(A:A, C:C=C2, "")))>0))
Picture of the excel sheet
Picture of working formula
Use the following formula in row 2: =SUMPRODUCT(--(FREQUENCY(IF($B$2:$B$20=$B2,$A$2:$A$20),$A$2:$A$20)>0))
*I think it doesn't require ctrl+shift-enter in older Excel versions, since SUMPRODUCT is an array formula by default.
The formula checks the frequency of values in column A where column B matches the value in the current row. It returns the count per unique value meeting the condition. Wrapping it in -- & >0 returns 1 for each unique value. SUMPRODUCT sums them.
Edit:
I realized that the top level assembly values are actual text, not numeric values. In that case (since it's all numeric values stored as text) you can use this workaround:
=SUMPRODUCT(--(FREQUENCY(IF($B$2:$B$20=$B2,--($A$2:$A$20)),--($A$2:$A$20))>0))
It converts the text to numbers.
Sidenotes to this workaround:
If any value would contain a character other than numeric it will not get counted.
If you have both values like 02554 and 2554 they'll both get converted to 2554 and counted likewise.
Edit 2:
For text use the following:
=SUMPRODUCT(IF($B$2:$B$20=$B2, 1/(COUNTIFS($B$2:$B$20, $B2, $A$2:$A$20, $A$2:$A$20)), 0))

EXCEL formula to sum demand per date when multiple entries of dates exist

How do I sum the amount of demand I have for each date, when there are multiple entries for each date., e.g.
Sheet 1:
A B
Date Demand
13/7/21 5
13/7/21 4
13/7/21 2
15/7/21 6
15/7/21 3
16/7/21 2
16/7/21 4
So I'm trying to get a summary as follows:
Sheet 2:
A B
13/7/21 11
14/7/21 0
15/7/21 9
16/7/21 6
17/7/21 0
I've tried =SUMPRODUCT(--('Sheet 1'!$A$2:$A$240='Sheet 2'!A2),'Sheet 1'!$B$1:$B$240)
I'm not wanting to do a pivot table, as the pivot table does not give me the zero values for dates where there is no data (unless there is a way to show this in a pivot)
You can use SUMIFS() like
=SUMIFS(Sheet1!B:B,Sheet1!A:A,A1)
You can also use SUMPRODUCT() in this way.
=SUMPRODUCT((Sheet1!$B$2:$B$8)*(Sheet1!$A$2:$A$8=A1))
Put together quickly to start you off:
Edit to give text version of the solution to the OP's problem:
=SUMIFS(B6:B12,A6:A12,"="&G12)
Then you can do between by setting lower and upper criteria in the sumifs().

Excel sum based on lookup of code and values in another table

Given 2 named tables in Excel 2013 (or higher):
tblInvoice
ID InvRef Total
1 I/123 45
2 I/234 8
tblDeliveries
ID InvRef Amt
1 I/123 10
2 I/123 15
3 I/123 20
4 I/234 5
5 I/234 3
How can we get the tblInvoice[Total] to compute automatically using an Excel formula? i.e. in pseudocode:
tblDeliveries[Total] = SUM(tblDeliveries[Amt] WHERE MATCH InvRef)
I have tried this Excel formula in tblInvoice[InvTotal] but it is returning an incorrect value:
=SUMPRODUCT(SUMIF(tblDeliveries[InvRef],[InvRef],tblDeliveries[Amt]))
Also tried swapping the first and second parameters. Produces a different amount, but still incorrect:
=SUMPRODUCT(SUMIF([InvRef],tblDeliveries[InvRef],tblDeliveries[Amt]))
If relevant, it is assumed that there is a 1:N relationship from tblInvoice[InvRef]:tblDeliveries[InvRef] and that tblInvoice[InvRef] is UNIQUE.
The syntax is incorrect for what you require.
=SUMPRODUCT(SUMIF(tblDeliveries[InvRef],[#InvRef],tblDeliveries[Amt]))
The # is the crucial difference.
Regards

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

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.

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)

Resources