I'd like to know if it's possible to use the SUMIF function with implicit or "nested" arrays. With "implicit" array I mean a matrix which data isn't in it's final form in any rank of the spreadsheet, but it's function of some other array. For example, lets say that we have data of an independent variable (which values, all integers, range from 0 to 5) in the rank A1:A100, and data of a dependent variable in B1:B100. With the SUMIF function we may calculate easily, for example, the sum of the dependent variable when the independent is 4. But if we want to know the sum of the SQUARES of the dependent variable it's not that easy, indeed, the SUMIF function gives an error if we write SUMIF(A1:A100;4;B1:B100^2) no matter how we enter it (as array or as a simple formula).
Is there any way to do this without having to waste an entire column for the squares of the values of column B?
I know that for this very example the function SUMPRODUCT((A1:A100=4)*B1:B100^2) would do the job, what I don't know is how to "nest" arrays (which is very useful) in general.
The answer is no, I'm afraid. The ranges used in COUNTIF(S)/SUMIF(S)/AVERAGEIF(S) must be either:
1) References to worksheet ranges
2) Constructions which resolve to references to worksheet ranges
One example of the former:
=SUMIF(A1:A10,"A",B1:B10)
And two of the latter (which just happen to be identical to the above):
=SUMIF(A1:INDEX(A:A,10),"A",B1:INDEX(B:B,10))
=SUM(SUMIF(OFFSET(A1,{0,1,2,3,4,5,6,7,8,9},),"A",OFFSET(B1,{0,1,2,3,4,5,6,7,8,9},)))
Here SUMPRODUCT has the advantage over this group of functions, in that constructions may be passed which do not necessarily resolve to worksheet ranges.
However, it might well be the case that a more efficient set-up is achieved by, as you suggested, first using an additional column within the worksheet to compute the squares and then referencing that column within a SUMIF, not least since one of the major advantages that COUNTIF(S), SUMIF(s), etc. can claim over SUMPRODUCT is that arbitrarly large references can be passed with no detriment to calculation performance. For example, the difference in performance between:
=SUMIF(A:A,"A",B:B)
and:
=SUMPRODUCT(0+(A:A="A"),B:B)
is enormous, the latter, having to process all 1,048,576 cells within that range (whether they are technically beyond the last-used cells or not), being not at all recommendable.
Regards
Related
I've been retooling some older spreadsheet tools for filtering and formatting dynamic data outputs using Excel's newer Dynamic Array Formulas functionality. This has been helping remove some of the need for pre-allocating cells and lower amounts of helper columns (which has allowed for reduced file sizes and snappier performance).
One function type I am struggling to replace is pulling out dynamic, running duplicate counts.
For instance, say I have a column B of 20 names that can vary in length from a handful to say 200 names. There is also related data in columns C, D, etc that similarly varies in size. For use of filtering the Data in the later columns, we currently use a helper column in A consisting of the running count of the duplicates in A with a formula using semi-anchored ranges(ie. Beginning the range with an anchored cell that expands as the formula is copied down the helper column akin to the solution here with CountIf() and a semi-anchored range). The drawback here vs the new dynamic array formulas is that the helper column needs to be pre-allocated for the data.
Despite attempts with Index(), Aggregate(), Filter(), and a few more involved notations like Sumproduct(--(...)), the most straightforward method I can find to make helper column A seems to be by creating the running count via semi-anchored ranges, which unfortunately does not seem to translate well to the new dynamic array Formulas.
Has anyone had any luck adapting the use of semi-anchored ranges and formulas for use in dynamic array formulas?
To use the dynamic array formula we need to use OFFSET which is volatile.
=COUNTIFS(OFFSET(A1,0,0,SEQUENCE(COUNTA(A1#))),A1#)
Appreciate this is an old post, but for future reference (I personally at least couldn't find an answer elsewhere), the below seems to work as a non-volatile formula alternative.
=LET(InputArray,A1#,
RowCount,ROWS(InputArray),
Temp,1*(InputArray=TRANSPOSE(InputArray)),
MMULT(TRANSPOSE(IF(SEQUENCE(RowCount,1)>SEQUENCE(1,RowCount),0,Temp)),SEQUENCE(RowCount,1,1,0)))
=SUM(SUMIFS('Output'!$H$50:$H$69,'Output'!$C$50:$C$69,{"*PLoan","Deficit Loan"},'Output'!$G$50:$G$69,X97:AC97))
I have the above array formula. X97:AC97 is where I have my array literal and the values in this array literal are conditioned on some other parameters (let's say the values are a,b,c,d,e if the conditions are met). Because the values are conditional, it might show up as a "" "" "" e "". So in the sumifs array formula it takes the array literal as values {"a","","","","e",""}, which causes an error. How do I make it so that the array is {"a","e"}?
edit: I realized after much effort that sumifs might not be the correct method as it requires alignment. For example, I might have a (1,0) criteria met from {PLoan, Deficit Loan} while X97:AC97 = (0,1,0,0,0,0). I do want the sum for that number but it won't sum due to the mismatch.
I got the below to work. If anyone can think of a way to incorporate wildcards as part of my string search, let me know...
=SUMPRODUCT(IFERROR(ISNUMBER(MATCH('Output'!$G$50:$G$69,$X$97:$AC$97,0)*MATCH('Output'!$C$50:$C$69,{"PLoan","Operating Deficit Loan"},0))*('Output'!$H$50:$H$69),0))
COUNTIF(S)/SUMIF(S) do not necessarily "require alignment" of the criteria being passed. It all depends upon what it is you wish to count.
If you require a count which comprises all combinations of the two sets of criteria, then it is sufficient that the two arrays containing those criteria be orthogonal.
At the moment, both of the criteria arrays which you are passing are of the same vector-type, i.e. row-vectors, since:
X97:AC97
is clearly a row- (horizontal) vector, as also is:
{"*PLoan","Deficit Loan"}
since, for English-language versions of Excel, the comma and semicolon represent, respectively, separators within row (horizontal) and column (vertical) array constants.
As such, in your case you simply need to transpose one of these two arrays, viz:
=SUM(SUMIFS(Output!$H$50:$H$69,Output!$C$50:$C$69,{"*PLoan";"Deficit Loan"},Output!$G$50:$G$69,X97:AC97))
(We could also transpose the other, viz:
=SUM(SUMIFS(Output!$H$50:$H$69,Output!$C$50:$C$69,{"*PLoan","Deficit Loan"},Output!$G$50:$G$69,TRANSPOSE(X97:AC97)))
or else move the entries within X97:AC97 to some other, vertical, range.)
See here for more information.
Regards
I am interested in spreadsheet functions, not VBA solutions, to be included in a single cell formula.
[A1:A15 contain numeric values from 1 to 127, B1:B15 contain integers from 1 to 7 that set a divisor.]
Given the function:
=SUMPRODUCT(MOD(FREQUENCY(A1:A15;A1:A15);B1:B15))
FREQUENCY(A1:A15;A1:A15) gives a 1-column array of 15+1 rows, whereas the second part (B1:B15) is a 1-column array of 15 rows.
I would like to change the resulting array given by FREQUENCY (only in memory -not explicit in sheet-) from a 1-column 16 rows array to a 1-column 15 rows array with the first 15 cell values of that array.
[FREQUENCY documentation: https://support.office.com/en-us/article/FREQUENCY-function-44e3be2b-eca0-42cd-a3f7-fd9ea898fdb9 NB: for Excel, second remark states number of elements that depend on bins_array. ]
I would appreciate suggestions.
Thus, both arrays within MOD will have the same dimensions and SUMPRODUCT will not find cells with error values. I can disregard error values using IF and ISERROR within SUMPRODUCT, but I'd rather disregard the non-relevant part of the FREQUENCY resulting array if it is possible.
It has been thought that making it more specific might be more helpful, so it has been heavily reduced and simplified.
With external help, I have been able to fine-tune a way to solve my problem using INDEX in array formula mode. I am posting the answer in case it helps others.
One way: Put FREQUENCY(A1:A15;A1:A15), or any formula that produces an multi-cell array, within INDEX and have 2nd and/or 3rd arguments as array of consecutive values which will represent rows/columns.
INDEX(FREQUENCY(A1:A15;A1:A15);ROW(INDIRECT("1:" & ROWS(FREQUENCY(A1:A15;A1:A15)-1));1)
First argument within INDEX is the resulting array coming from a formula to shrink (from 16x1 to 15x1), which would be a multi-cell array formula if explicitly entered; second argument is the array 1..15 given by row numbers from 1 to the number of total rows of the "array from formula to shrink" MINUS 1: the first 15 (out of 16) values in the array from a formula; 3rd argument is the column of the shrank array (if need be, more than one could be selected using an analogue to the second argument).
In the particular case of FREQUENCY, because it is known that we are interested in the "bins" part of the function, the formula can be simplified by including the total rows of the "bins"/"intervals" array inside FREQUENCY (its second argument). We will have
INDEX(FREQUENCY(A1:A15;A1:A15);ROW(INDIRECT("1:" & ROWS(A1:A15)));1)
and the complete formula would become
SUMPRODUCT(MOD(INDEX(FREQUENCY(A1:A15;A1:A15);ROW(INDIRECT("1:" & ROWS(A1:A15)));1);B1:B15))
Now, both dividend and divisor of MOD have exactly the same dimensions (15x1) and because B1:B15 includes integers greater than 0 there are no errors.
Thanks all for helping me in making question more concise and better formatted.
ADDITIONAL INFORMATION: As pointed out correctly in comments by XOR LX, this does not seem to work in the widely popular spreadsheet software Excel. It has been developed for an INDEX function inside SUMPRODUCT as used in Open Office Calc which I had mistakenly thought 100% equivalent to Excel's version. A more complete answer perhaps using other functions would be appreciated.
In the previous answer, XOR LX points out very correctly that this formula cannot work in Excel, due to row_num/column_num argument behaviour. Very kindly XOR LX has shown me how that approach can work, and also thanks and credit for supplying a good answer: "INDEX can be used to redimension array (even dynamically created ones) if the row_num/column_num array is coerced to take an arbitrary array with the right dimensions, as shown on this blog entry " The following formula has been checked in Excel 2010 and has the expected results:
SUMPRODUCT(MOD(INDEX(FREQUENCY(A1:A15,A1:A15),N(INDEX(ROW(INDIRECT("1:" & ROWS(A1:A15))),,)),1),B1:B15))
NB: row_num argument of first INDEX, a ROW generated auxiliary array, has been nested inside N(INDEX([...],,)); at least one comma is necessary to account for the two arguments minimum of the nested INDEX. It is in itself interesting the discussion that applies generally to INDEX's arguments, and other functions', that need to be coerced to take arrays (see, here and here at XOR LX's blog). For Open Office users it might be worth stressing the point made at the blog
Unlike OFFSET, (...) for which the first parameter must be a
reference (...) in the worksheet, INDEX can also accept –
and manipulate – for its reference arrays which consist of values
generated e.g. via other subfunctions within the formula. XOR LX's blog
That would be indeed the case in changing the dimension in an array as in this question, but also useful in reversing or displacing the values in an array, for example. Open Office accepts arrays as row_num/column_num, so the coercion is not needed and some formulas rely on this, but without it, these formulas are unlikely to work when files are open in Excel.
Regrettably, this type of coercion is not passed correctly to Open Office, and formula need to be "decoerced" to work, at least in my casual tests.
In order to use a formula that would work in both spreadsheet programs regarding shortening arrays, the only thing I have managed is the following (required: arrays must be single-column)
SUMPRODUCT(
(COLUMN(INDIRECT("R1C1:R"& ROWS(vals_to_mod) &"C"& ROWS(FREQUENCY(vals_for_freq,vals_for_freq)),FALSE))
-ROW(COLUMN(INDIRECT("R1C1:R"& ROWS(vals_to_mod) &"C"& ROWS(FREQUENCY(vals_for_freq,vals_for_freq)),FALSE))
=0)
*MOD(TRANSPOSE(FREQUENCY(vals_for_freq,vals_for_freq)),vals_to_mod)
)
(it "shortens" one array to the shortest of the pair, by creating an auxiliary array with TRUE/1s on the diagonal starting top-left and FALSE/0s elsewhere, therefore disregarding all defined values outside the square section of the array. Thus, SUMPRODUCT adds values within the diagonal of the square section which are the product of the corresponding values up to the last value of the shorter array.)
I have a case in an Excel macro (VBA) where I'd like to dimension an array where the number of dimensions and the bounds of each dimension are determined at runtime. I'm letting the user specify a series of combinatorial options by creating a column for each option type and filling in the possibilities below. The number of columns and the number of options is determined at run time by inspecting the sheet.
Some code needs to run through each combination (one selection from each column) and I'd like to store the results in a multidimensional array.
The number of dimensions will probably be between about 2 to 6 so I can always fall back to a bunch of if else blocks if I have to but it feels like there should be a better way.
I was thinking it would be possible to do if I could construct the Redim statement at runtime as a string and execute the string, but this doesn't seem possible.
Is there any way to dynamically Redim with a varying number of dimensions?
I'm pretty sure there is no way of doing this in a single ReDim statement. Select Case may be marginally neater than "a bunch of If...Else blocks", but you're still writing out a lot of separate ReDims.
Working with arrays in VBA where you don't know in advance how many dimensions they will have is a bit of a PITA - as well as ReDim not being very flexible, there is also no neat way of testing an array to see how many dimensions it has (you have to loop through attempts to access higher dimensions and trap errors, or hack around in the underlying memory structure - see this question). So you will need to keep track of the number of dimensions, and write long Case statements every time you need to access the array as well, since the syntax will be different.
I would suggest creating the array with the largest number of dimensions you think you'll need, then setting the number of elements in any unused dimensions to 1 - that way you always have the same syntax every time you access the array, and if you need to you can check for this using UBound(). This is the approach taken by the Excel developers themselves for the Range.Value property - which always returns a 2-dimensional array even for a 1-dimensional Range.
As I understood your users can specify dimensions and their seize by filling in the excel-sheet. This means you have to get the last row containing a value and the last column.
Therefore, have a look at: Excel VBA- Finding the last column with data
Use Redim to change the array's size. If you want to keep some kind of entries use Redim Preserve
"Some code needs to run through each combination (one selection from
each column) and I'd like to store the results in a multidimensional
array."
To begin with, I would transpose desired Range object into a Variant.
Dim vArray as Variant
'--as per your defined Sheet, range
'this creates a two dimensional array
vArray = ActiveWorkbook.Sheets("Sheet1").Range("A1:Z300").Value2
Then you could iterate through this array to possible find the size and data you need, which you may save it to an array (with the dimensions) you need.
Little Background:
Redim: Reallocates storage space for an array variable.
You are not allowed to Redim an array, if you are defining an array with a Dim statement initially. (e.g. Dim vArray(1 to 6) As Variant).
UPDATE: to show explicitly what's allowed and what's not under Redim.
Each time you use Redim it resets your original Array object to the dimensions you are defining next.
There's a way to preserve your data using Redim Preserve but that only allows you to change the last dimension of a multidimensional array, where first dimension remains as the original.
=INDIRECT(ADDRESS(ROWS(ADF$1:ADF1642),MATCH(sheet2!$C$25:$C$29,sheet1!1:1,0)))
That formula returns {"$AFX$1642";"$AJR$1642";"$AKF$1642";"$AJD$1642";"$AIP$1642"} without the Indirect, and the values in those cells with the Indirect prefix. I need to sum them all, but it only returns the first: $AFX$1642.
There are indeed similar topics, but their solutions do not include the lookup nature of matching an array from sheet 2, to the array in sheet 1 that has a multitude of columns to quote, unquote, "exclude".
Halp!
I'm not particularly fond of your current set-up, I have to say (a worksheet which references up to column AIP?!), nor of the current formula you are using (there is almost always a better alternative to constructions using INDIRECT/ADDRESS). However, without knowing precisely what you are trying to achieve, and not being able to see an actual workbook, it's difficult to suggest improvements.
The coercion you require with your current construction is:
=SUMPRODUCT(N(INDIRECT(ADDRESS(ROWS(ADF$1:ADF1642),MATCH(Sheet2!$C$25:$C$29,Sheet1!1:1,0)))))
Regards