How to keep excel formula consistent with inserting new columns - excel

In my sheet, my top row is a bunch of dates and I have columns separating the weeks.
| 1.1 | 1.5 | 1.12 | 1.29 |JANUARY TOTAL| 2.3 | 2.15 | FEBRUARY TOTAL|
----------------------------------------------------------------------
Below each date is the number of hours worked on a project, and in each "MONTH TOTAL" column is a sum of the time worked on each project per month.
When I created this spreadsheet, I put in a filler of 2 blank spaces, but since I can't predict when I'll be working on each project, I have to rely on inserting new columns if I decide to work on something on a day that isn't preset into the sheet.
The problem is that my formulas don't extend all the way to the MONTH TOTAL columns when I insert new columns. For instance, when I create my spreadsheet, my formula for one of the totals in the MONTH TOTAL column would be "=SUM(B2:C2)" but when I insert two more dates I need the formula to be "=SUM(B2:E2)".
Is there a way to encapsulate inserted cells into a formula so that it sums between a bound of two columns and any new inserted cells?

You can use the INDIRECT function coupled with ADDRESS, ROW, and COLUMN.
In your stated example, let's assume your "MONTH TOTAL" formula is in D2 and currently contains =SUM(B2:C2). You will want to update the formula to:
=SUM(B2:INDIRECT(ADDRESS(ROW(),COLUMN()-1)))
Whenever you insert a new column, it will still SUM by referencing the values in the preceding columns.

Related

How to create a dynamic formula to find the average of a set of values for a given vector

I am trying to create a formula that gives me the average of the last 12 entries in a given dataset depending on the associated vector.
Let's make an example:
I have in column F2,G2,H2 and I2 dates, Company1, Company2 and Company3 respectively. Then from row3 to row 33 I have months dates starting from May 2016.
Date Company1 Company2 Company3
May-16 2,453,845
Jun-16 13,099,823
Jul-16 14,159,037
Aug-16 38,589,050 8,866,101
Sep-16 63,290,285 13,242,522
Oct-16 94,005,364 14,841,793
Nov-16 123,774,792 7,903,600 41,489,883
Dec-16 93,355,037 12,449,604 69,117,105
Jan-17 47,869,982 13,830,712 83,913,764
Feb-17 77,109,905 10,361,555 68,176,643
The goal is to create a formula that, when I drag it down, correctly calculates the average of the last 12 values for a given company.
So for example i would have, say in table "B2:C5":
Company1 76,856,345
Company2 11,120,859
Company3 65,674,349
And, if a new Company4 is added to the list, then I just have to drag it down the formula, to calculate the average of the last 12 months for Company4.
Until now, I have came up with this formula:
=AVERAGE(LOOKUP(LARGE(IF(ISNUMBER(G:G),ROW(G:G)),ROW(INDIRECT("1:"&MIN(12,COUNT(G:G))))),ROW(G:G),G:G ))
This formula correctly calculates the average of a given column, considering only the last 12 values. The last step would be to come up with a formula that includes all the columns and then calculates the average for the given company.
Thanks!
I recommend that you use a named range to define your data in columns G:I. When a company is added, just modify the named range's specs. I used the name Target. Of course, you can replace it with $G:$I if you feel so inclined but I would rather recommend reducing the number of rows in the range, which is easier to manage when it is named.
Use the formula below to extract the company names from the first row of Target into the first column of your averages table. This is to ensure that the names are spelled identically in both locations.
=INDEX(Target,1,ROW()-2)
The number 2 indicates the number of rows above the row containing the formula. it is copied here from cell M3. There, ROW()-2 creates the number 1, counting sequentially as the formula is copied down.
Now I have the formula below in my cell N3 and copied down.
=SUM(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0)))
The formula simply sums up the columns G, H, and I in 3 consecutive rows.
In the final step I inserted the range definition established above, meaning excluding the SUM() function, into your existing formula.
=AVERAGE(LOOKUP(LARGE(IF(ISNUMBER(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))),ROW(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0)))),ROW(INDIRECT("1:"&MIN(12,COUNT(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))))))),ROW(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))),INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))))

In Excel how to count between date1 and date2 that have cells in row that contain text?

I need to choose cells in one column that are between two dates, and then based on the rows that contain those dates, choose cells in another row that also contains content.
I didn't use ISBLANK because it counts a formula yet an empty cell as a not-blank. Instead check if there is content by "*".
Here is what I came up with, but instead of returning the number of cells, instead this returns TRUE (which obviously isn't what I want).
In the formula below I am assuming:
C:C is the whole column containing DATES.
E:E is the whole column containing CONTENT.
The date range in this case is January 1, 2018 to January 31, 2018.
"*" means is there is content in the cell
=IF(AND(COUNTIFS(C:C,">="&"2018-1-1",C:C,"<="&"2018-1-31"),COUNTIF(E:E,"*"))=0,"",AND(COUNTIFS(C:C,">="&"2018-1-1",C:C,"<="&"2018-1-31"),COUNTIF(E:E,"*")))
My goal is to:
count the numbers of the cells in column E that are between the dates in column C
if the whole formula is 0, then return a blank.
See this picture of a sample excel sheet to make my intent clear:
How can I get my formula working so it does as needed?
SOLUTION
Hi all, so thanks to #girlvsdata, we have a working solution. I had to do a couple edits to her code to work for my uses, but her formula overall works perfect. Here is the solution:
To choose all cells in column E that are not blank, in between the date range of all of January (unknown end date) based on the adjacent C column if that is your date column, then the solution is:
=IF(COUNTIFS(C:C,">="&"2018-1-1",C:C,"<="&EOMONTH("2018-1-1",0),E:E,"*")=0,"",COUNTIFS(C:C,">="&"2018-1-1",C:C,"<="&EOMONTH("2018-1-1",0),E:E,"*"))
Note that "2018-1-1" is January 1 2018, and EOMONTH("2018-1-1",0) is the last valid day of January in the year 2018 (in this case, 31, but if it is different another year (e.g. for February this works for leap years too) then it will be that last day). Also it eliminates the need to calculate which is the last day or every month, as well as months that have changing end dates dependent on the year (e.g. Feb). This is important to eliminate a margin of error.
The only thing you have to do to change the month is only change e.g. -1- (Jan) to -2- for Feb, or change the year for other years. With this formula you can ignore the day part.
If the answer is 0 (no cells have any content in between the range), then the cell is blank instead of 0. (GOod for when you want to create a sheet checking future dates for future reference when more rows are added to the sheet.
It also works across different sheets, just use, say your other sheet is called "Tracker" then use Tracker!C:C and Tracker!E:E. Hope it helps!
Thank you all! :D
(Please note: My local date format is day, then month)
With the data laid out as in your example above:
A B
1 Dates |Content
------------+-------
2 1/01/2018 |
3 2/01/2018 |123456
4 3/01/2018 |
5 4/01/2018 |12398
6 5/01/2018 |484
7 6/01/2018 |1538
8 7/01/2018 |
9 8/01/2018 |
10 9/01/2018 |
11 10/01/2018 |14648
12 11/01/2018 |
13 12/01/2018 |145615
14 13/01/2018 |
And with the date range in cells D2 and E2:
Date Start Date End
2/01/2018 7/01/2018
This formula returns the count:
=COUNTIFS(A:A,">="&D2,A:A,"<="&E2,B:B,">0")
This will depend on whether your numbers in Column B are formatted as text or number. If they are formatted as numbers, the above formula will work. If they are formatted as text, replace the last section ">0" with "*".
This formula adds the conditional part of your question:
=IF(COUNTIFS(A:A,">="&D2,A:A,"<="&E2,B:B,">0")=0,"",COUNTIFS(A:A,">="&D2,A:A,"<="&E2,B:B,">0"))
(If the formula returns 0, show blank)

Display in Sheet1 newest entries from Sheet2 to Sheetn in excel

I have an excel file which has an arbitrary number of sheets. From Sheet2 to Sheetn the structure is the same:
| A | B | C | D |
------------------------------
| Date | Name | Value | Type |
| Date | Name | Value | Type |
However, these rows are not sorted by the date, so the 1st row of each sheet is not guaranteed to be the newest date.
The first sheet has one row for each sheet and it should be updated to include the most recent entry in each sheet.
For example, if we have 4 Sheets, then Sheet 1 will have 4-1=3 rows which will be: the row with the newest date of Sheet 2, the row with the newest date of Sheet 3, and the row with the newest date on Sheet 4.
A file equals a thousand words, so you can download the excel file here and see what I need.
I've actually figured out a solution. I'm guessing it's significantly far from the best one, but since I'm by no means an excel person, the fact that it works is enough for me.
For other non-excel people who just need to achieve this without learning excel, here's how I did it:
=INDEX(MySheetName!$A$2:$G$10000,(MATCH(MAX(MySheetName!$C:$C),MySheetName!$C:$C,0)-1),COLUMNS(MySheetName!$A$1:A1))
Replace "MySheetName" on its 4 occurrences to the actual Sheet name of each of your excel's sheets.
Replace Columns 'A to G' on the INDEX to the columns you want to retrieve.
Replace Column C:C on the MATCH to the column you want used to retrieve the row number using its MAX()
Remember to span the formula to multiple columns, as many as the maximum columns that the sheet you reference has.
What I'd like to improve now is to make it able to span multiple rows as well, which for me it would mean that it would automatically reference all the columns of the next excel sheet. For now, one needs to manually replace "MySheetName".
The "10000" also looks really bad, but I don't know how to many sure that it will get the Max() over all rows. I'll never fill more than 100 rows on each sheet, so 10000 seems safe.

Automatically working out the average of filtered results

I have a spreadsheet where column P has a score between 1-6
The cell O4 has the following formula: =AVERAGEIFS(P8:P5000,P8:P5000,"<>6",P8:P5000,"<>0")
This formula searches for the average of the score in column P excluding 6, blanks and 0
Column O has staff names e.g John, Mark, Tim.......
What i want to do is for Cell O4 to automatically calculate the average of the figures shown in column P after i have used the filter function to show only results of a selected staff member.
I was hoping excel might be able to do this automatically however cell O4 appears to still be showing the average of the whole column P regardless of whether i have filtered or not.
I was given the formula below on another forum but it seems to be giving slightly wrong results albeit only by a small amount but i need to have the results exact if possible. Any help appreciated.
=SUMPRODUCT(1-ISNUMBER(MATCH(P8:P100,{0,6},0)),SUBTOTAL(9,OFFSET(P8,ROW(P8:P100)-ROW(P8),0,1)))/SUMPRODUCT(1-ISNUMBER(MATCH(P8:P100,{0,6},0)),SUBTOTAL(2,OFFSET(P8,ROW(P8:P100)-ROW(P8),0,1)))
Maybe
{=AVERAGE(IF((P8:P5000<>6)*(P8:P5000<>0)*SUBTOTAL(103,INDIRECT("O"&ROW(8:5000))),P8:P5000))}
will do what you want. Assuming the Filter is on column O.
The 103 in SUBTOTAL will also exclude if rows are manually hidden. If this ist unwanted and it should only exclude hidden rows, if filtered, then use 3 instead.
This is an array formula. Input it into the cell without the curly brackets and then press [Ctrl]+[Shift]+[Enter] to create the array formula.
I would create a separate table in a new sheet with all unique staff members and then perform the calculation. This way, you can quickly compare values for all staff just by scanning the table instead of having to constantly update the filter to see the values for potentially dozens or hundreds of staff. You would add the staff name range and criteria to your AVERAGEIFS formula.
For your example:
Sheet 2
A B
--- ---
1 | Staff Average
2 | Bob =AVERAGEIFS(Sheet1!$P$8:$P$5000,Sheet1!$O$8:$O$5000,A2,Sheet1!$P$8:$P$5000,"<>6",Sheet1!$P$8:$P$5000,"<>0")
3 | Mary =AVERAGEIFS(Sheet1!$P$8:$P$5000,Sheet1!$O$8:$O$5000,A3,Sheet1!$P$8:$P$5000,"<>6",Sheet1!$P$8:$P$5000,"<>0")
4 | Joe =AVERAGEIFS(Sheet1!$P$8:$P$5000,Sheet1!$O$8:$O$5000,A4,Sheet1!$P$8:$P$5000,"<>6",Sheet1!$P$8:$P$5000,"<>0")

Sum values in a column based on date

I have written this function that will give me a monthly sum for two columns: one has the date of each order, one has the cost of each order.
=SUMIF($C$1:$C$1000,">="&DATE(2010,6,1),$D$1:$D$1000)-SUMIF($C$1:$C$1000,">="&DATE(2010,7,1),$D$1:$D$1000)
Using data like this:
8/16/10 17:00 7.99
8/16/10 14:25 7.99
8/15/10 22:42 7.99
I end up with a table like this:
May 998
June 968.28
July 1239.76
August 514.96
However, now I would like to do daily sums and using my way I have to hand edit each row.
How can I do this better in Excel?
Use a column to let each date be shown as month number; another column for day number:
A B C D
----- ----- ----------- --------
1 8 6 8/6/2010 12.70
2 8 7 8/7/2010 10.50
3 8 7 8/7/2010 7.10
4 8 9 8/9/2010 10.50
5 8 10 8/10/2010 15.00
The formula for A1 is =Month(C1)
The formula for B1 is =Day(C1)
For Month sums, put the month number next to each month:
E F G
----- ----- -------------
1 7 July $1,000,010
2 8 Aug $1,200,300
The formula for G1 is =SumIf($A$1:$A$100, E1, $D$1:$D$100). This is a portable formula; just copy it down.
Total for the day will be be a bit more complicated, but you can probably see how to do it.
Use pivot tables, it will definitely save you time. If you are using excel 2007+ use tables (structured references) to keep your table dynamic. However if you insist on using functions, go with Smandoli's suggestion. Again, if you are on 2007+ use SUMIFS, it's faster compared to SUMIF.
Following up on Niketya's answer, there's a good explanation of Pivot Tables here:
http://peltiertech.com/WordPress/grouping-by-date-in-a-pivot-table/
For Excel 2007 you'd create the Pivot Table, make your Date column a Row Label, your Amount column a value. You'd then right click on one of the row labels (ie a date), right click and select Group. You'd then get the option to group by day, month, etc.
Personally that's the way I'd go.
If you prefer formulae, Smandoli's answer would get you most of the way there. To be able to use Sumif by day, you'd add a column with a formula like:
=DATE(YEAR(C1), MONTH(C1), DAY(C1))
where column C contains your datetimes.
You can then use this in your sumif.
Add a column to your existing data to get rid of the hour:minute:second time stamp on each row:
=DATE(YEAR(A1), MONTH(A1), DAY(A1))
Extend this down the length of your data. Even easier: quit collecting the hh:mm:ss data if you don't need it. Assuming your date/time was in column A, and your value was in column B, you'd put the above formula in column C, and auto-extend it for all your data.
Now, in another column (let's say E), create a series of dates corresponding to each day of the specific month you're interested in. Just type the first date, (for example, 10/7/2016 in E1), and auto-extend. Then, in the cell next to the first date, F1, enter:
=SUMIF(C:C, E1, B:B )
autoextend the formula to cover every date in the month, and you're done. Begin at 1/1/2016, and auto-extend for the whole year if you like.
If the second row has the same pattern as the first row, you just need edit first row manually, then you position your mouse pointer to the bottom-right corner, in the mean time, press ctrl key to drag the cell down. the pattern should be copied automatically.

Resources