So I'm not a fan of VBA and I recently learned that OFFSET can be used with COUNTA to flashfill a range as far at it is as long as you aim for a longer range than you have data. Now I want to be able to achieve this both for columns and rows at the same time, where the rows are averaged. Could this be done? I am banging my head against the wall to find some logic to do it, but can only manage to combine it in a way that multiplies the rows with the number of the column.. which is not desired, of course.
I have posted a Minimal Reproducible Example in Excel Online:
https://onedrive.live.com/view.aspx?resid=63EC0594BD919535!1491&ithint=file%2cxlsx&authkey=!ALmV0VtFb7QZCvI
If you see Cell J9 and J11 you will see what I want to combine. The three rows in J11 and down, I want to average in J10, and spill/flashfill (like J9 and 11 does automatically because of the formula already) them from to the right, for as many columns as there data in the range A1-G4..
So I have raw data of numbers with titles in A1-G4, and by writing =OFFSET($A$1:$A$1,0,0,1,COUNTA($A$1:$EV$1)-1) in J9 I get all the titles of the columns filled from left to right, and by writing =OFFSET($A$1,1,0,COUNTA($A:$A)-1) in J11 I get the rows of the first column filled from top to bottom. They can also be combined, by writing OFFSET(Days,1,0,COUNTA($A:$A)-1,COUNTA(Days)), where "Days" is =OFFSET($A$1:$A$1,0,0,1,COUNTA($A$1:$EV$1)-1) (in a named range for readability) or OFFSET($A$1:$A$1,0,0,1,COUNTA($A$1:$EV$1)-1) without using a named range
As a thought, though I'm not sure how to implement it, maybe this could somehow be used in some form to get the column reference for the horizontal part in combination with =AVERAGE(OFFSET($A$1,1,0,COUNTA($A:$A)-1))
=MID(ADDRESS(ROW(),COLUMN()),2,SEARCH("$",ADDRESS(ROW(),COLUMN()),2)-2)
..found at https://superuser.com/questions/1259506/formula-to-return-just-the-column-letter-in-excel/1259507
Now, based on your explanation, here is the screenshot of my test:
Section A1:Exxx
I have converted that section into a Table, called «TblData», having numerous avantages:
It expands automatically without any additional efforts/formula
We can identify Data by its Columns attributed automatically by the Table [#1], [#2],[#3], [#4], [#5]
Section J9:N9
As a replica of the table name, I have used the following formula to retrieve it:
=INDEX(TblData[#Headers],1,COLUMN(A1)) '<--- This is for J9
=INDEX(TblData[#Headers],1,COLUMN(E1)) '<--- This is for N9
Section J11:Nxx
As a replica of the Table Content, I have used the following formula to populate the content:
=INDEX(TblData,ROW($A1),MATCH(J$9,TblData[#Headers],0)) '<--- This is on J11
=INDEX(TblData,ROW($A3),MATCH(N$9,TblData[#Headers],0)) '<--- This is on N13
Section J10:N10
Now this is the interesting part of the Average, so here is the formula I used for it:
=AVERAGE(TblData[1]) '<--- This is on J10
=AVERAGE(TblData[5]) '<--- This is on N10
NB: (1) Instead of using the Content below J10:N10, I prefer to reuse the Table as it expands automatically as more rows are added.
(2) Unless it is really necessary, I feel it is a double work as well to replicate again A1:Exxx from J9:Nxxx, because you can use the Table for whatever you need, with less maintenance.
Kindly find attached the file as well after I updated those items:
File Link: https://drive.google.com/open?id=1wRbpUxg0XLpfGqdvMF4fNKXDrL7xPPWs
We can correspond more below for further info. Hoping you to strech more your compentence :)
Sorry, mate, I can't figure out what you want to calculate. If it makes sense to add J9+J11 then you could just concatenate the two formulas in J9 and J11 with a plus sign. After much deliberation I decided to assume that your question is not one of formula but of formula-writing - "referencing" for short. Therefore I prepared this answer for you, hoping that it will prove helpful.
Building on your named range Days I suggest you create a dynamic named range Data with this formula.
[Data] =OFFSET(Sheet1!$A$1,0,0,COUNTA(Sheet1!$A:$A),COUNTA(Sheet1!$1:$1))
The range thus defined is dynamic in both directions. However, bearing in mind that OFFSET is volatile (slows down your worksheet) you may like to keep its use limited to this one formula and perhaps start the range at A2, but I shall tempt you to break the rule. Now you can use the INDEX function to refer to the Data range.
= INDEX(Data, [Row number], [Column number])
defines a single cell. But by setting either column or row to zero you can define an entire column or row. =INDEX(Data,0,1) defines column 1 of the Data range, =INDEX(Data,1,0) defines its first row.
=INDEX(OFFSET(Data,1,0),0,1) defines the first column of a range moved down by one row from its original position. I recommend the alternative and start the Data range from A2 and perhaps declare another range for the first row if needed.
=AVERAGE(INDEX(Data,0,1)) would draw the same average you already have in your sheet, provided that Data was defined starting at A2. For fun's sake, =AVERAGE(INDEX(OFFSET(Data,1,0),0,1)) would do the same without the change in the range's definition.
=COLUMN() returns the number of the column this formula resides in. So, you could enter =COLUMN()-6 in column G, copy to the right and get a count starting from 1. (You can do the same vertically with the ROW() function.) Applied to your formula, =AVERAGE(INDEX(Data,0,COLUMN()-6)) would return the average from column 1 if entered in column G, and from columns 2, 3 4, etc as copied to the right.
As I said, I don't understand enough of your request to bring this idea to a conclusion but I think that using the method described above will provide you with a tool to copy formulas into the table your sample has at its right. If you would elaborate on your requirement I might be able to assist more.
Related
For those of us used to Microsoft Excel who switch to using Google Sheets, there are many differences which need to be taken into account.
One of the nice features in Excel that I miss is tables. If you insert a table into your Excel Spreadsheet, it does a lot of automatic things for you. You can have a single formula for one column of your table, and not have to update it whenever you add new data - whether adding a table row, or adding a row in the middle of the table.
Sometimes (though I haven't figured out why it sometimes does and why it sometimes doesn't) even without tables, Excel will suggest a formula fill as you're entering new data into a row, making copying the formula as easy as pressing "Tab".
There is no functionality in Google Sheets that matches this exactly. When you have a lot of data to enter, having to copy the formulas every single time you add a row is very tedious and time consuming and further leaves open the possibility in making a mistake when transcribing the information and copying / pasting the formulas. Any single cell could have a mistake and you won't know until it causes a problem later, then troubleshooting it will also be time consuming and difficult.
There are various questions in StackOverflow, StackExchange, Google Support and other sites that tackle this issue, but none seem to have a good solution that works for everyone. A lot of people have written an Apps Script do do just this, or use Apps Script + HTML forms as well... but it seems like that shouldn't be necessary, it adds more time & setup, and ends up with a specific solution for that sheet and that sheet only.
So, how can you replicate this behavior in Google Sheets so you don't have to keep copying & pasting your formulas over and over again and save yourself time (and your company money) and make Google Sheets act more like Excel?
BACKGROUND
There is a Google Support Thread on Inserting new Rows which suggests the use of ARRAYFORMULA to do this job. It is not an exact replacement for Excel's functionality, but it can work in most applications. There are other functions that output arrays, such as SEQUENCE which can also be applied similar to these examples depending on the situation, but I'll focus on ARRAYFORMULA here as it's the most generic and MOST functions can be wrapped in it and otherwise behave as you'd expect.
Here is also a link to an ARRAYFORMULA & MMULT Example Provided by Google (Note that this link will make a copy of the sheet, not let you directly access the example). The first tab is all about matrix multiplication, the second and later tabs have examples using ARRAYFORMULA.
The examples above are pretty limited in scope, so let's expand on those. To illustrate, I will use a basic formula involving 4 columns as an example. Let's say we have data in columns A, B, and C, and we want to do a relatively simple formula between them. Let's assume row 1 is being used as a header row, and your data is from row 2 down, as most people would do. Let's make the formula simple, but a little interesting, by having column D equal to the PREVIOUS value of A plus the product of B and C. Let's also assume we currently have 12 rows of data, but we know we will have data we need to enter in the future. Most of that data will get entered at the end, but sometimes we may need to add data in the middle of the range.
You can follow along with my Publicly Posted Example Sheet Here if you want (this will also create a copy on your drive so you can make changes and follow along). Each example below corresponds to a tab in the Example Sheet.
EXAMPLE: FORMULA ON EVERY ROW
In it's simplest form, the formula in D2 would be = A1 + B2 * C2. Except, of course, we know A1 is a text header and if we include that we'll get an error. It's also commonly understood that absolute references (with $) execute faster in Google sheets, and we don't need relative references on the columns (but rows are necessary to fill), so let's modify cell D2 as follows:
=IF( ISNUMBER($A1), $A1, 0 ) + $B2 * $C2
Then fill down to cell D13 (this is already done in the example).
So now you have your current data... but what if you need to add data?
If you add data to row 14, in columns A, B, and C, you then also have to copy the formula to D14. Easy peazy for this example, but what if you have 30 columns, 5 of them with formulas and you add another 10 entries to the list every day? This becomes very tedious. You can avoid entering it for every row, but filling down the number of rows you need today and save a little time, but it breaks your flow of data entry.
Even worse, what if the entries are in some sort of order (e.g. order of date data was captured) and you get old data that needs to be entered in the middle of the range? You can add at the end and copy, then sort.
Some sheets won't let you sort, or won't sort correctly if you have certain data, so you may need to insert in the middle... let's say between rows 8 and 9. If you did this in an Excel table, and used "insert row" it would automatically populate cell D9 with your formula.
But here, when you add this new row 9 not only is D9 blank and need you to enter the formula, but now the A column reference in cell D10 is pointing to A8 instead of A9 where it should! So you have to recopy / refill your equation to cell D10 as well - and this is easy to miss - you may not know to do it, or forget to do it, and now your formulas are broken.
... Now, to be honest, Excel didn't get this part right, either.
Somehow, it properly fills D9 in with the correct formula but botches
D10 with a reference to A8, but then continues with a correct reference to A10 in D11. Which is almost worse because since D9 was filled and all the other rows are correct, you may not realize you have a problem in D10...
This is basic spreadsheet use and is roughly the same behavior as using Excel WITHOUT Tables (except those instances where it decides to make suggestions for you) - so par for the course here if Excel didn't have the Table or suggestion ability.
Pros:
Simplest formula to implement
Works fine in fixed size sheets or sheets that don't change often
Tried and true, Will always work
Cons:
Have to copy formula to every new row you make
Very tedious for "living documents" that change often
If any formulas cross between rows, the pattern breaks when you insert a row
in between and you have to copy your formula to the row below as
well as your new one
With all the additional required repeated actions, it's very easy to make a mistake
Since the mistake could be in a single cell, finding the mistake after the fact can be difficult
EXAMPLE: CLOSED RANGE ARRAYFORMULA
Google support touts this as the best method. Indeed, if you want your formulas to update automatically when you add data in between and you want the least amount of computation time, then an ARRAYFORMULA with a limited (or "closed") range is the best solution.
To use ARRAYFORMULA, you put the formulas only in your top row of data (in this example, row 2). What makes this example closed range is that we will set it to cover exactly the data we have. So, the formula in D2 would be:
=ARRAYFORMULA( IF( ISNUMBER( $A$1:$A$12 ), $A$1:$A$12, 0 ) + $B$2:$B$13 * $C$2:$C$13 )
Here, we can (and I recommend) use all absolute references as the range we're using doesn't change as the cell row it's calculating changes. When you enter this formula, you will see it automatically populate D3 through D13 with the correct data as well.
If we want to add another row in the middle, it's easy. Taking the previous example, if we add a row between rows 8 and 9, you will see the formula in D2 has changed all the last rows - 12 is now 13, and 13 is now 14. When you enter data into columns A, B, and C in the new row 9, it automatically calculates correctly in D9.
When you look at the data in rows in column D (except D2), however, it shows the number itself in the formula bar - so someone looking at this sheet unaware there is an ARRAYFORMULA in use has no indication that it's an ARRAYFORMULA and overwriting ANY cell that was populated by ARRAYFORMULA will break the formula, give you an error in D2 and leave the rest of the values in the column blank. This is true for all methods using ARRAYFORMULA So, for that reason, I recommend you make your column a protected range!
Alternate: You could name all of your ranges. For example, $A$1:$A$12 could be col_A_prev, $B$2:$B$12 could be col_B, and $C$2:$C$12 could be col_C. Which gives the formula:
=ARRAYFORMULA( IF( ISNUMBER( col_A_prev ), col_A_prev, 0 ) + col_B * col_C )
The behavior would be identical. When you add a row in between, the named ranges will automatically expand to include it. You could also use the same ranges for your column protection to ensure no data is written over.
Note: I do want to give kudos where it is due. Google Sheets handles named ranges WAY better than Excel. When you add or remove rows / columns inside your named range in Google it automatically expands the range - and Google actually allows you to use the named ranges as references in any of the settings (conditional formatting, protection, etc.). While you can enter a named range in Excel for some of these, it will convert it to R/C references which won't change even if your range changes later. If you want to add to the ends or you move rows / columns in your named range - well, they're both still terrible at that
However, if we want to add new data to the end, in row 14 or after, this arrayformula will not automatically update.
Even worse, if you add a row between rows 12 and 13, it breaks the formula - as the references to columns B and C will update, but the references to column A will not - because A only went to row 12. In row 14 you now get the error:
Array arguments to ADD are of different size.
Because you're trying to add an array with 12 elements to an array with 13 elements. Admittedly, this is only a problem if you're referencing other rows which isn't that common across all useful spreadsheets. However, there are many practical reasons to do so, like cumulative sums.
So, either you have to deal with updating your ARRAYFORMULA columns each time you add data to the end (which doesn't make it much better than just copying your formulas to each row) or, you could basically make the last two rows "dummy rows" that you don't care about and add protection to those rows so they can't be edited or a row added between them, with perhaps a note saying "To add new data, insert a row above this line" so other people using it know what they have to do.
Pros:
Relatively simple formula to implement
Fastest Execution time
Will automatically adjust formula to any rows added in the middle
Can manage your ranges as named ranges
Cons:
Have to change the formula if you add any new data to the bottom (which is where you usually add new data) -OR- you have to implement one or more blank rows included in range with protection & reminders to ensure no one adds data to the bottom
Data below ARRAYFORMULA looks like just number entries and could easily confuse people into thinking it's not a formula entry and overwrite it without thinking.
EXAMPLE: OPEN RANGE ARRAYFORMULA
If you're following along in the example sheet, the first thing you'll note is this sheet doesn't do the same thing. It is simply using the CURRENT value in column A, rather than the previous row. This is because you CAN'T reference a previous row with this method (see a couple paragraphs down for why). To compensate, I forced A, B, and C to 0 in the first row and added another row to the bottom.
This is similar to the closed range example in its application of ARRAYFORMULA the difference here, is instead of having a fixed end to the ranges (rows 12 & 13 above), you leave the range open by using just the column letter at the end of the range, which references the last row of the column. So the equation in D2 now looks like this:
=ARRAYFORMULA( IF( ISNUMBER( $A$2:$A ), $A$2:$A, 0 ) + $B$2:$B * $C$2:$C )
The reason you can't reference a previous row's cell is if we used $A$1:$A here, that array would always have one more element than either $B$2:B or $C$2:$C and thus won't be able to add and will result in the error:
Result was not automatically expanded, please insert more rows (1).
Except inserting more rows won't work because the ranges will all expand by 1 also. Again, this is only a problem if you need to reference other rows which isn't common but is useful for things like cumulative sums.
When it comes to adding rows, though, this method is the best. Whether you are adding to the middle or the end of your data, it will automatically update the values in your ARRAYFORMULA columns.
Alternate: Same as with closed ranges, you could name all of your ranges. For example, $A$1:$A could be col_A_prev, $B$2:$B could be col_B, and $C2:$C could be col_C. Which gives the same formula as with closed range:
=ARRAYFORMULA( IF( ISNUMBER( col_A_prev ), col_A_prev, 0 ) + col_B * col_C )
So if you're not referencing previous rows, or if you just add a top "dummy" row like I did in the example, it's all good... easy peazy lemon squeezy, right?
Yes, at least at first. The other problem here is that open ranges are computationally intense for Google Sheets algorithms. As you add more and more rows, especially if you have multiple open range ARRAYFORMULA columns, the sheet calculations get slower and slower and slower. The sheet I was working on that prompted this had 21 columns, 8 of which had ARRAYFORMULA formulas in row 2. At around 200 rows of data (not that much in the world of spreadsheets) it was taking MINUTES to calculate with each and every change I was making. That's simply not useable - I almost went back to copying the formula to each row. (It's possible using named ranges may improve the speed some - I didn't try it on that sheet)
So this solution doesn't really work for big (but not even that big) spreadsheets where you have lots of formulas.
Also, a more minor gripe - you'll notice in the example that every row on the spreadsheet was now populated in column D, even where no data was entered. That's annoying, but not a sheet killer by any means - and you could add an IF statement to the ARRAYFORMULA to just output "" whenever you have no data in one or more data columns.
Pros:
Relatively simple and straight forward formula to implement
"Works" with any number of rows
Automatically includes any rows that are added - on the end or in between
Can manage with named ranges
Cons:
Cannot reference data from previous rows
Extremely slow - computation time goes up with every added row (& every added column with an open reference)
Data below ARRAYFORMULA looks like just number entries and could easily confuse people into thinking it's not a formula entry and overwrite it without thinking.
EXAMPLE: HYBRID ARRAYFORMULA
Are you ready to give up on Google Sheets yet?
Well, there is one more option. It gets complicated and involved, but IMO works better in most situations than any of the above examples.
What I do here is add a cell with a formula for the number of rows in the sheet that have data in a certain column. Let's just say column A for this example. That formula looks like this:
= ARRAYFORMULA( MAX( IF( LEN($A:$A), ROW($A:$A), ) ) )
This, in and of itself, is an open ranged formula. It scans everything in column A and returns the last row that has SOMETHING in it. But it's one single formula in one cell reporting 1 value - no other cells get populated from it. It's relatively computationally intense for this one cell, but it's just one cell in the entire sheet.
Then, to make sure that any changes you make (adding / removing rows or columns) do not affect any references to that cell, name it. In the example provided, this is named last_example_row.
I also strongly recommend that you add protection to last_example_row so it's not accidentally changed. Extra tip: you can actually set both sets of permissions: "Only You can edit" and "show a warning when editing" so even if you try to edit it accidentally it will give you the chance to cancel the edit.
Since it's not a piece of data you need visually, hiding it is also a good idea (I left it unhid in the example so you can easily see the formula)
Now, in order to use the value in last_example_row as part of our ranges, we have to use the INDIRECT function. We replace every open-ended instance in the previous example with a specific INDIRECT call.
For calls to the same row, for example, we replace with a pattern like this:
$B$2:$B is replaced with $B$2:INDIRECT( "$B$" & last_example_row )
so it ends on the last used row.
For calls to the previous row, we replace with a pattern like this:
$A$1:$A is replaced with $A$1:INDIRECT( "$B$" & ( last_example_row - 1 ) )
so it ends 1 row before the last used row.
So the final equation becomes this monstrosity:
=ARRAYFORMULA( IF( ISNUMBER( $A$1:INDIRECT( "$A$" & ( last_example_row - 1 ) ) ), $A$1:INDIRECT( "$A$" & ( last_example_row - 1 ) ), 0 ) + $B2:INDIRECT( "$B$" & last_example_row ) * $C2:INDIRECT( "$C$" & last_example_row ) )
So it's a closed range reference that points to a single open range calculation, and it works. Whether you add data in the middle or to the end, it automatically calculates your column for you - and it only populates rows where your data is also populated.
Since it only does the open range calculation ONCE, then uses that value in all remaining closed range calculations, this is much, much faster than the open range example above. It IS slower calculating than the first two examples, however - but I haven't yet hit the point in my real sheets where the delay has made it unusable (stay tuned as I add more data to my sheets over time). If anyone reading this has hit that point with this method, please let me know how many columns & rows you got to, including how many of the columns used an ARRAYFORMULA like this.
Unfortunately, however, since this method requires an INDIRECT call, you cannot use named ranges to accomplish this.
Pros:
Most flexible option
"Works" with any number of rows
Automatically includes any rows that are added - on the end or in between
Much Faster than completely open references
Cons:
Formulas are complex, hard-to-follow, and easy to make a mistake while entering
Slower than closed references - computation still time goes up with every added row and every added column with these "hybrid" references
Data below ARRAYFORMULA looks like just number entries and could easily confuse people into thinking it's not a formula entry and overwrite it without thinking.
Cannot manage with named ranges
Epilogue
Maybe (hopefully) someday Google will add a feature that will keep track of your formulas and execute them in a speedy way and this post will be obsolete. Until then, I hope this post helps someone out there.
Additional Note
Using any of the ARRAYFORMULA methods above can break sorting. If you add filters, and the sort by A->Z or Z->A on a particular column and row 2 is no longer row 2 - then your ARRAYFORMULA gets moved to whatever row it gets sorted to - and then only applies from that row down. Rows above it will be blank in all your ARRAYFORMULA columns. This is very disappointing to me. One way around it (that I don't like) is you can make row 2 a "dummy" row where whatever columns you may sort by have values that will always make it the top row. That's a pretty ugly solution, though.
You can make it less "ugly" by hiding row 2. Then columns will sort fine and you won't see any of the dummy data ("dummy" data may not even be necessary as the hidden row shouldn't sort with the rest). The caveat here is if you share it with multiple users - they won't even see there is a formula being used, it looks like all manual entries - and if one gets overwritten, it will break the ARRAYFORMULA. So, I would recommended protecting the ARRAYFORMULA columns, as well.
Here I am stucked with one excel issue where i want to concatenate from column F till column I where the logic is when the benchmark column A3 (for example) is blank it need to concatenate column F till column I till there is a value at column A4.and this logic need to automatically concatenate the mentioned column till there is a value under the benchmark column. currently i need to keep change the concatenate range in order to concatenate it fully with the logic. Appreciate if anyone can help me out.
Below image shows how i am doing manually which very time consuming
You can use the MATCH function (with a wildcard) to find the next non-blank row; and use that in an INDEX function to detect the range to concatenate.
Assuming your data starts in A3 and the lowest possible row is row 1000 (change the 1000's in the formula below if it might be much different:
J2: =IF(A2="","",CONCAT(INDEX(F2:$I$1000,1,0):INDEX(F2:$I$1000,IFERROR(MATCH("*",A3:$A$1000,0),1000-ROW()),0)))
Note: It is possible to also develop solutions using INDIRECT and/or OFFSET. Unfortunately, these functions are volatile, which means they recalculate anytime anything changes on your worksheet. If there are a number of formulas using these functions, worksheet performance will be impaired. INDEX and MATCH are non-volatile (except in ancient versions of Excel - pre-2003 or so)
The OFFSET-function would come on handy here. One solution is to do it like
This works in my worksheet.
Cell Q6 just defines the number of rows downwards that the MATCH-function is checking for the next "HEADER1" value. If "HEADER1" is found, the MATCH-function returns how many rows down-1. If no "HEADER1"-value is found within that range, that value is then the number of rows used.
If the first column also has "HEADER2" and so on, you can add the MID-function to both references inside MATCH to limit which part of the string are to be searched for.
I tried to adjust the references properly to fit your sheet, but I may have missed something:
=IF(ISBLANK($B2),"",CONCAT(OFFSET($B2,0,0,IFNA(MATCH(MID($B2,1,6),MID(OFFSET($B2,1,0,$B$1),1,6),0),$B$1),4)))
I have a data in Excel as shown in attached image where in I've used named ranges.
Spends. Apr18. May18. Jun18
Category A. 120. 120. 120
Category B. 135. 125. 129
Category C. 110. 111. 112
Name Range: Spends
Range: =Sheet1!$A$1:$D$4
The average quarterly spends are calculated using named references as:
=AVERAGE(INDEX(Spends,2,2):INDEX(Spends,2,4))
This returns 119 in Cell E2
How can I drag this formula to subsequent cells so that it is applied automatically.
If you use the INDEX function with constants, of course these constants will not change when you drag the formula down and/or across.
You will need to learn about absolute and relative references and use something like Row(A1) and/or Column(A1) instead of the constants 2 and 2.
But maybe it does not have to be that complicated.
You could just add a column in the data table that calculates the Average and refer to that cell.
Or, in cell E2, use the formula =average(B2:D2).
I don't see the need for the named range at all in this scenario. It only complicates things.
It seems that you are struggling with appropriate data architecture concepts rather than with using formulas.
If in doubt, keep it simple. I don't see an application for copying the formula across, since you only have three months of data and you want to average these three months.
If your real scenario has more columns, then, please!!, update your question and post more relevant context. Then post a comment, so I can see you made a change.
Again, since you are new here: do not post updates into comments. Edit your question and then post a comment.
Use the ROW() function to return the relative position
E.g. in E2 and drag down
=AVERAGE(INDEX(Spends,ROW(),2):INDEX(Spends,ROW(),4))
Maybe you want to dynamically find the quarter start and end columns then you can use Match function to find the dates and return the position (column) where found. Then feed these into your formula:
Getting the columns by searching for qtr start and end:
Referencing those found positions as column arguments in your formula:
EDIT: I have revived the source data source to remove the ambiguity of my last screen shots
I am trying to transpose spreadsheet data where there are many rows where the customer name may be duplicated but each row contains a different product.
For instance
revised original data source
to
revised proposed data format
I would like to do it with formulae if possible as I struggle with VB
Thank you for any help
I realise this is a huge answer, apologies but I wanted to be clear. If you need anything from me, drop me a comment and I'll help out.
Here's the output from my formula:
EDITED ANSWER - Named ranges used for ease of understanding:
These are just an example of a few of the named ranges I have used, you can reference the ranges directly or name them yourself (simplest way is to highlight the data then put the name in the drop down next to the formula bar [top left])
Be wary that as we will be using Array formulas for AccNum and AccType, you will not want to select the entire column and instead opt for either the exact data length or overshoot it by 100 or so. Large array formulas tend to slow down calculation and will calculate every cell individually regardless of it being empty.
First formula
=IF(COUNTIF(D2:D11,">""")>0,CONCATENATE("Account Number ",LEFT((COLUMN(A:A)+1)/2,1)),"")
This formula is identical to the one in the original answer apart form the adjusted heading title.
=IF(Condition,True,False) - There are so many uses for the IF logic, it is the best formula in Excel in my opinion. I have used to IF with COUNTIF to check whether there is more than 0 cells that are more than BLANK (or ""). This is just a trick around using ISBLANK() or other blank identifiers that get confused when formula is present.
If the result is TRUE, I use CONCATENATE(Text1,Text2,etc.) to build a text string for the column header. ROW(1:1) or COLUMN(A:A) is commonly used to initiate an automatically increasing integer for formulas to use based on whether the count increase is required horizontally or vertically. I add 1 to this increasing integer and divide it by 2 so that the increase for each column is 0.5 (1 > 1.5 > 2 > 2.5) I then use LEFT formula to just take the first digit to the left of this decimal answer so the number increases only once every 2 columns.
If the result is FALSE then leave the cell blank ,""). Standard stuff here, no explanation needed.
Second Formula
=CONCATENATE(INDEX(Forename,MATCH(Sheet4!$A2,Reference,0)))
=CONCATENATE(INDEX(Surname,MATCH(Sheet4!$A2,Reference,0)))
CONCATENATE has only been used here to force blank cells to remain blank when pulled by INDEX. INDEX will read blank cells as values and therefore 0's whereas CONCATENATE will read them as text and therefore "".
INDEX(Range,Row,Column): This is a lookup formula that is much more advanced than VLOOKUP or HLOOKUP and not limited in the way that they are.
The range i have used is the expected output range - Forename or Surname
The row is then calculated using MATCH(Criteria,Range,Match Type). Match will look through a range and return the position as an integer where a match occurs. For this I have set the criteria to the unique reference number in column A for that row, the range to the named range Reference and the match type as 0 (1 Less than, 0 Exact Match, -1 Greater than).
I did not define a column number for INDEX as it defaults to the first column and I am only giving it one column of data to output from anyway.
Third Formula
Remember these need to be entered as an array (when in the formula bar hit Ctrl+Shift+Enter)
=IFERROR(INDEX(AccNum,SMALL(IF(Reference=Sheet4!$A2,ROW(Reference)-ROW(INDEX(Reference,1,1))+1),ROUNDDOWN((COLUMN(A:A)+1)/2,0))),"")
=IFERROR(INDEX(AccType,SMALL(IF(Reference=Sheet4!$A2,ROW(Reference)-ROW(INDEX(Reference,1,1))+1),ROUNDDOWN((COLUMN(B:B)+1)/2,0))),"")
As you can see, one of these is used for AccNum and the other for AccType.
IFERROR(Value): The reason that this has been used is that we are not expecting the formula to always return something. When the formula cannot return something or SMALL has run out of matches to go through then an error will occur (usually #VALUE or #NUM!) so i use ,"") to force a blank result instead (again standard stuff).
I have already explained the INDEX formula above so let's just dive in to how I have worked out the rows that match what we are looking for:
SMALL(IF(Reference=Sheet4!$A2,ROW(Reference)-ROW(INDEX(Reference,1,1))+1),ROUNDDOWN((COLUMN(B:B)+1)/2,0))
The IF statement here is fairly self explanatory but as we have used it as an array formula, it will perform =Sheet4!$A2 which is the unique reference on every cell in the named range Reference individually. In your mock data this returns a result of: {FALSE;TRUE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE} for the first entry (I included titles in the range, hence the initial FALSE). IF will do my row calculation* for every true but leave the FALSEs as they are.
This leaves a result of {FALSE;2;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE} that SMALL(array,k) will use. SMALL will only work on numeric values and will display the 'k'th result. Again the column trick has been used but to cover more ground, I used another method: ROUNDDOWN(Number,digits) as opposed to using LEFT() Digits here means decimal places so I used 0 to round down to a whole integer for the same result. As this copies across the columns like so: 1, 1, 2, 2, 3, 3, SMALL will alternatively (as the formulas alternate) grab the 1st smallest AccNum then the 1st Smallest AccType before grabbing the 2nd AccNum and Acctype and so forth.
*(Row number of the match minus the first row number of the range then plus 1, again fairly common as a foolproof way to always get the correct row regardless of where the data starts; actually as your data starts on row 1 we could just do ROW(Reference) but I left it as is incase you had data in a different format)
ORIGINAL ANSWER - Same logic as above
Here's your solution in 3 parts
Part 1 being a trick for the auto completion of the titles so that they will hide when not used (in case you will just copay and paste values the whole lot to speed up use again).
=IF(COUNTIF(C2:C11,">""")>0,CONCATENATE("Product ",LEFT((COLUMN(A:A)+1)/2,1)),"") in C
=IF(COUNTIF(D2:D11,">""")>0,CONCATENATE("Prod code ",LEFT((COLUMN(B:B)+1)/2,1)),"") in D
Highlight both of the cells and drag across to stagger the outputs "Product " and "Prod code "
Part 2 would be inputting the unique IDs to the new sheet, I would suggest copying your entire column A across to a new sheet and using DATA > REMOVE DUPLICATES > Continue with current selection to trim out the multiple occurrences of unique IDs.
In column B use =INDEX(Sheet2!$B$1:$B$7,MATCH(Sheet4!$A2,Sheet2!$A$1:$A$7,0)) to get the names pulled across.
Part 3, the INDEX
Once again, we are doing a staggered input here before copying the formula across the page to cover the entirety of the data.
=IFERROR(INDEX(Sheet2!$C$1:$D$11,SMALL(IF(Sheet2!$A$1:$A$11=Sheet4!$A2,ROW(Sheet2!$A$1:$A$11)-ROW(INDEX(Sheet2!$A$1:$A$11,1,1))+1),ROUNDDOWN((COLUMN(A:A)+1)/2,0)),1),"") in C
=IFERROR(INDEX(Sheet2!$C$1:$D$11,SMALL(IF(Sheet2!$A$1:$A$11=Sheet4!$A2,ROW(Sheet2!$A$1:$A$11)-ROW(INDEX(Sheet2!$A$1:$A$11,1,1))+1),ROUNDDOWN((COLUMN(B:B)+1)/2,0)),2),"") in D
The formulas of Part 3 will need to be entered as an array (when in the formula bar hit Ctrl+Shift+Enter) . This will need to be done before copying the formulas across.
These formulas can now be dragged / copied in all directions and will feed off of the unique ID in column A.
My Answer is already rather long so I haven't gone on to break the formula down. If you have any trouble understanding how this works, let me know and I will be happy to write up a quick guide, breaking it down chunk by chunk for you.
Need solutions for the queries posted in the below link,
http://www.mrexcel.com/forum/excel-questions/334740-dynamically-sum-row-values-based-column-headers.html
Having PersonWeek as separate column with sum of weeks should be displayed.
Quest: Dynamically sum row values based on column headers?
It is good form to re-post the question in its entirety on this site; nevertheless:
What you are looking for is relatively straight forward. There are a lot of ways to accomplish this but I suggest that you use combination of the 'Match' function and the the 'Offset' function. The Offset function creates a range based on a given start point, moving up/down / left/right as indicated, with a given height / width. For example:
=Offset(B2,1,2,3,4)
Indicates the range D3:G5. This is the range given by starting at cell B2, moving 1 row down and 2 columns to the right, and going for a total of 3 rows and 4 columns.
So the remaining point is to determine where to start and stop your offset.
First, the first field in your Offset function will be cell A1, as that is the top-left corner of your data table. To find how many rows to move down, you need to find what project you are referring to (I will assume that cell A6 is where you enter the project name that you care about, A7 is where you enter the first week you care about, and A8 is where you enter the last week you care about). To find how many rows to move down from A1, then, use Match:
=Match(A6, A2:A5,0)
To find how many columns to move to the right to find your first week, use Match again:
=Match(A7, B1:G1,0)
Assuming you only want to look at a single project, we know how high we want the range to be (1).
To find how wide we want the range to be, we need to know your ending week, less your starting week:
=(Match(A8,B1:G1,0)-Match(A7, B1:G1,0))
So the whole thing together will be:
=Offset(A1,Match(A6, A2:A5,0),Match(A7, B1:G1,0),1,Match(A8,B1:G1,0)-Match(A7, B1:G1,0))
Now the only thing left is to wrap the newly defined range in a 'sum' function, like so:
=Sum(Offset(A1,Match(A6, A2:A5,0),Match(A7, B1:G1,0),1,Match(A8,B1:G1,0)-Match(A7, B1:G1,0)))
This formula will create an error if someone enters a project name / weekname in a way that isn't found in your table - so in cells A6-A8 you may want to use data validation to only allow those names to be entered - let me know if you would like elaboration on that.