How can I average the last n number of rows when using a Average(Index, match formula)? - excel

I have two sheets one has a table with sets across the top of the table and daily km for each set the second sheet has set numbers in column b, I wish to find the daily average for the last 6 months (so 182 days) for each of the sets and return the value in column F.
Sheet 2
sheet 1 - Table
I have used the following formula to average the entire column
=AVERAGE(INDEX(Table1[[H9003]:[H9043]],0,MATCH('FA Forecasts'!B5,Table1[[#Headers],[H9003]:[H9043]],0)))
However I only want to return the average of the last 182 rows of the table to the corresponding set number - and each time a new line is added I only want the last 182 rows.
I'm not sure what I can do to achieve this - any advice would be most appreciated.

If you have Excel 365 you can use this formula:
=LET(indexSetNumber,MATCH([#[Set Number]],tblSheet1[#Headers],0),
filterByDate,FILTER(tblSheet1,tblSheet1[Date]>=LARGE(tblSheet1[Date],cntDays)),
filterBySet,INDEX(filterByDate,,indexSetNumber),
AVERAGE(filterBySet))
I like "readable" formulas - this one reads like this:
indexSetNumber: returns the column index of e.g. H9003 --> 5
filterByDate: returns the rows with Top "cntDays" dates. I am using a parameter cntDays in C2 - for this example I chose 2, but you would put 182.
filterBySet: returns column indexSetNumber from filterByDate
from these values AVERAGE is returned.

This might also work (assuming C2 is =TODAY()):
=AVERAGEIF(Table1[Date],">" & C2 - F2,INDIRECT("Table1["& 'FA Forecasts'!B5 &"]"))
Using AVERAGEIF formula :
Table1[Date] : returns the "Date" table column (sheet 1)
">" & C2 - F2 : "date more recent than Today-182 days"
INDIRECT("Table1["& 'FA Forecasts'!B5 &"]") : returns the table column (sheet 1) which header match with Set Number in the current row (sheet 2)
Basically return the average for kms in the past 182 days for the Set Number of the row.

Related

Find an intersecting value for each row and column in excel

I have monthly data in rows in column A. I also have monthly data in headers from column B to Column G. In Column H i have current month populated for all rows. I would like to display the value where, for example if the month in row is Jan and current month is Mar, then find the value where Jan and Mar intersect and do this for all rows. I tried using the following formula in Column J but it only gives the first value when it works. The result in Column I is desired. Attached is the picture for clarity.
=INDEX(B1:H2,MATCH(A2,$A$1:A2,0),MATCH(A2,$B$1:H2,0))
You can do it with SUMPRODUCT.
I made a fake dataset with different values than the ones you show, so i could test properly if changing Current month would work.
It works even if column CurrentMonth holds different months
My formula is:
=SUMPRODUCT(--($B$1:$G$1=H2);B2:G2)
This is how it works:
--($B$1:$G$1=H2) will return an array of 1 and 0 if range B1:G1 is equal to current month in that row. So in case CurrentMonth=Mar then it will return {0;0;1;0;0;0}
With SUMPRODUCT we multiply previous array by range B:G in current row, so for row 2, it would be B2:G2. So as example, it would be {0;0;1;0;0;0} * {1;2;3;4;5;6} = {0;0;3;0;0;0} and we sum up this array, getting 3
just use INDEX with one MATCH:
=INDEX(B2:G2,,MATCH(H2,$B$1:$G$1,0))

Use dynamic sum range and criteria range in SUMIFS

I'm using SUMIFS to calculate sales revenue of product X over a period of time. The problem is that all the sum range and the criteria ranges are dynamic since sales reps will add more rows to record new sales everyday. My formula looks like this:
=SUMIFS(Revenue!I5:I24,Revenue!F5:F24,"Product X",Revenue!B5:B24,">="&B3,Revenue!B5:B24,"<="&C3)
The starting row is row 5, and the last row that currently has data is row 24.
Revenue is the worksheet that contains the data. In this worksheet, column I is the revenue, column F is the product name, column B is the date that the revenue is recorded. B3 and C3 are the start and end date of the period.
When a sales rep records a new revenue, the last row that has data will become row 25, so my formula won't count it in. I tried to replaced 24 with 1000 hoping that it would count when new data was added beneath, but it returned #VALUE.
What should I do?
Thank you
Declare 3 dynamic ranges and use their names in your formula.
=IFERROR(SUMIFS(revenueI,revenueF,"Product X",revenueB,">="&B3,revenueB,"<="&C3),"")
Adding the names: Press Ctrl+F3 to open the name manager and then enter the 3 names
Name: revenueB
RefersTo=
OFFSET(Revenue!$B$5,0,0,COUNTA(Revenue!$B$5:$B$1048576),1)
Name: revenueF
RefersTo=
=OFFSET(Revenue!$F$5,0,0,COUNTA(Revenue!$F$5:$F$1048576),1)
Name: revenueI
RefersTo=
=OFFSET(Revenue!$I$5,0,0,COUNTA(Revenue!$I$5:$I$1048576),1)
1048576 is for the max number of rows in more recent versions of Excel. Adjust to the total number of rows for your Excel version.
Wrap in an IFERROR in case of errors e.g. if named ranges are off different length.

Summarize hours using 2 criteria

I wish to add a summary sheet to a workbook that captures timesheet data.
The timesheet sheets are laid-out like the screenshot below, where each row represents a day, and then a person's total hours worked are distributed across columns C to E, which are identified by a unique combination of seg2 and seg3 codes:
So, for the first column (C) has a unique combination of 201 & 303, which totals 19.9 hours, then (D) has a unique combination of 201 & 301 which totals 5.9 hours and then (E) has a unique combination of 202 & 303 which totals 11.2 hours.
My summary sheet will look like the screenshot below; where every possible combination of seg2 and seg3 codes are listed:
What I'm looking for is a formula on the summary sheet against the "January Hours" column that will take the combination of Seg2 and Seg3 codes for its row...
Then compare that with the range of cells in the timesheet sheet that has the Seg2 and Seg3 codes used:
Where it finds a match...
I want it to return the SUM of the range of hours below it...
...returning the total number of hours for that unique Seg2 and Seg3 combination:
I'll then drag the fill handle to finish all the formulas for the Summary sheet's "January Hours" column.
I've managed to do this using a single seg code using SUMPRODUCT, but have not yet figured out how to do it for 2 criteria (i.e. 2 seg codes).
By leaving the row parameter of the INDEX function empty, you can lookup an entire column. See the below formula and enter with CTRL + SHIFT + ENTER
=SUM(INDEX($C$4:$E$8,,MATCH($G2,IF($H2=$C$2:$E$2,$C$1:$E$1),0)))
Please note that this formula sums up all the values in the table as suggested in your example, and not just the values with a date of 1/1/2015.
INDEX/MATCH can handle a 2-way match -- is it necessary to "re-sum" the hours? If the Seg2/Seg3 Totals from the time sheets are always in the same place, this will look up the total hours:
=INDEX(Timesheet!$C$2:$E$10,9,MATCH($A2&$B2,Timesheet!$C$2:$E$2&$C$3:$C$3,0))
Edit: forgot to note this is an array formula

Excel: Match\Index specific column based on multiple criteria

I have two Tables, Table 1 Column A is a rolling date column. Table 2 consists of four columns of differing "Trigger Dates". Table 2, Column 1 contains an Annual trigger, ie occurs once a year. Table 2 Column 2 contains Bi-Annual Triggers, and occurs twice yearly. Table 2 Column 3 contains Monthly Triggers, "12 dates" and Table 2 Column 4 contains Weekly Triggers, "52 dates".
Ok, so what I'm looking for is to a formula that will return the date trigger, where Table 1 Column 1 matches a condition (Annually, Bi-Annually, Monthly, Weekly) selected in a cell "we'll say F1, outside of any ranges that may be created.
Example: If Table 1 Col 2 = "01/02/2013" and I select "Weekly" in F1, the formula will look in the weekly Column in Table 2 to determine if "01/02/2013" exists.
I've allocated a named range to each column in Table 2
I've allocated a named range to the condition in F1 which is a drop down list
Any suggestions?
You might try:
=IFERROR(VLOOKUP(B2,INDIRECT(CHAR(72+MATCH(F$1,$I$1:$L$1,0))&":"&CHAR(72+MATCH(F$1,$I$1:$L$1,0))),1,0),"")
Ok, so supposing your Range Name is in F1, your formula to find the match would look as follows:
=If(IsError(Match(LookupVal,Indirect(F1),0)),"No Match", "Match")
...Something along those lines
Hope this helps point you in the right direction.
INDIRECT and VLOOKUP will be your friends here.
I constructed Table2 just as you had described, with the Annual,Bi-annual,Monthly,Weekly labels for the columns in the table - this will avoid having to keep named ranges up to date, as it's looking directly at the table in the formula
For the function, I then used VLOOKUP to find the date, in the column referenced by F1.
Column Reference: INDIRECT("Table2["&$F$1&"]")
Find Value: VLOOKUP([#Col2],INDIRECT("Table2["&$F$1&"]"),1,FALSE) (looking at column 1 in the return values, as I don't really care what value is returned.
This will return the date if found, and an error if not. I can then wrap the result in the IF(ISERROR( so I can return Yes or No if the date is found.
This produces the entire formula of
=IF(ISERROR(VLOOKUP([#Col2],INDIRECT("Table2["&$F$1&"]"),1,FALSE)),"No","Yes")
for the next column in Table1, so that when I change the value in F1, it will look for that column name in Table2, and let me know if it is found.

How do I check for duplicate rows and do calculate on that rows in excel file?

I have one excel file which is having data like following
CUSIP Quantity Date Price
AF0 500000 5/6/2013 1
AE4 400000 5/6/2013 1.0825
AE4 500 5/6/2013 1
I need to check for column CUSIP and Date
If I'm having duplicate CUSIP for same date then I need to do following calculation.
1.Need to add Quantity for both of them instead of showing duplicate records need to show only one record( sum of Quantity).
2.Need to do calculation on Price as well like following
NewPrice = ((400000 * 1.0825) + (500 * 1.00))/(400000 + 500) = 1.08148
For example in using above data
Need to show output like
CUSIP Quantity Date Price
AF0 500000 5/6/2013 1
AE4 400500 5/6/2013 1.082397004
How do I achieve this in excel file using LOOKUP or else ?
Okay, after quite some research (interesting question by the way!), I came up with this:
=IF(COUNTIF($A$2:A2,A2)>1,"",SUMIF(A:A,A2,B:B))
=IF(COUNTIF($A$2:A2,A2)>1,"",SUMPRODUCT(--(A:A=A2),B:B,D:D)/SUMIF(A:A,A2,B:B))
Put these in cell D2 and E2 respectively (which is the next column after Price and in the row of CUSIP AF0).
And fill to the bottom of the worksheet to get the weighted average price or each CUSIP.
The first formula gives you the total quantity for the CUSIP and the second gives you the average price.
Copy and paste values for those two columns after calculation.
Put a filter and remove all the rows where the total quantity and average price is blank, and sort to make the worksheet neat.
Let me know if this works for you! I tried it on your sample data and it seems to be working. It's my first time using SUMPRODUCT ^^;
SUMPRODUCT(--(A:A=A2)
This bit returns the rows from column A where it equals to the row's CUSIP.
SUMPRODUCT(--(A:A=A2),B:B,D:D
This additional bit tells excel to multiply the values in column B and D of each returned row found above and SUMPRODUCT adds each result together.
EDIT:
I actually forgot about the date. You could maybe add a helper column where you'll generate an identifier to separate the different dates. To make it, you'll have to make a concatenate.
Hence in cell F2, you put:
=CONCATENATE(A2,C2)
In the formulae for cells D2 and E2, you will have to change them so they become:
=IF(COUNTIF($F$2:F2,F2)>1,"",SUMIF(A:A,A2,B:B))
=IF(COUNTIF($F$2:F2,F2)>1,"",SUMPRODUCT(--(A:A=A2),B:B,D:D)/SUMIF(A:A,A2,B:B))
reEDIT: Oops, put the wrong reference. Fixed now.

Resources