Is it possible to create an array formula based on a table where it searches for distinct combinations of data before summing? Here is some sample data:
| A | B | C | D |
---------------------------------------------------
1 | 1 Jun | Charlie | D | 1.3 |
2 | 1 Jun | Charlie | N | 1.4 |
3 | 1 Jun | Dave | D | 1.3 |
4 | 2 Jun | Charlie | N | 0.6 |
5 | 2 Jun | Dave | D | 1.5 |
What I'd like the array formula to be able to do is look at the table, ignore column B and tell me which distinct rows (A, C and D or more criteria) and sum column D if they are unique. In this example, it should not sum Row 3 because on 1 Jun, "D" already has a 1.3.
PivotTables and VBA are out as I am designing this for the lowest level user on super restrictive computers.
EDIT: Picture of expected results, changed sample data to reflect results.
Kind of the data I am looking for.... I replaced the ID with the LName to visualize. I need to exclude individuals and get only aircraft hours by their modes. You can see toward the bottom of the image, 2 people are on one aircraft. I used MAX to keep it from SUMming it up and skewing the hours.
The levels on the left are by Date, Aircraft, Flight Number, then Person. I need to aggregate the hours down to the Flight Number of each date.
The original formula is
=SUM((FLTD_HRS)*(FLTD_DATE>=$D$3)*(FLTD_DATE<=$D$4)*(IF(FLTD_ACFT_MDS=CONFIG!$F$8,1,0)))
however, as it is, does not get me unique flights. I'd prefer an array formula to a PivotTable.... XD
Create a table with the unique combos of A, C, and D. This works assuming finite or at least non-changing but you can also just add every combination you could possibly have and add an IF(row="","",x) to the formula to not show.
You can add a new column (E) to your first table and then lookup the value in the new table or calculate in the new table.
E = SUMIFS(D$3:D$30,A$3:A$30,A3,C$3:C$30,C3,D$3:D$30,D3)
E1 = {=INDEX(A$3:E$30,MATCH(G3&H3&I3,A$3:A$30&C$3:C$30&D$3:D$30,0),5)}
E2 = SUMIFS(D$3:D$30,A$3:A$30,G3,C$3:C$30,H3,D$3:D$30,I3)
I ended up creating a calculation sheet to create a unique key for each flight by concatenating the fields I wanted to match and also eliminated the duplicates by searching for "VALID_PC" which searched for the highest rank of the person in the crew flying. The formulas I used in columns K and L were:
=IF(IFERROR(MATCH(H3,VALID_PC,0),"0"),A3&RIGHT(C3,5)&D3&E3,0)
and
=IFERROR(IF(A2=0,0,IF(COUNTIF($K$2:K3,K3)>1,0,1)),0)
This allowed me to do a simple {=SUM} on whether column L had a 1 in it on the main page..
Calculations Sheet
I do appreciate the help and effort you all put in to help me.
Related
I am working on a table in which I need to extract the contents of certain columns based on the row "Type". For example if Row 3 has a Version "Type 1", then I need to extract Columns 1, 3 and 5 from that row. I need to place an "x" for Columns 2 and 4.
I have already created an array formula that will let me know which columns I need to extract, what I can't figure out is how to merge the data into one column in the order that I need.
Cols | Type | 1 | 2 | 3 | 4 | 5 |
1,3,5 | TYPE 1 | A | B | C | D | E |
For the sample above, I should get: A_X_C_X_E
I have included a sample table via the link below to better explain.
As far as the table, this is coming from a query, so I cannot just clear the columns for each row that are useless.
I eventually will end up using the data in another table that has the same number of columns.
Any help would be appreciated.
https://docs.google.com/spreadsheets/d/1N0Z_czPBqp7Jb9XDdPLigDQAZ8WkDLyo0qGcHk5re6o/edit?usp=sharing
Thank you.
try:
=ARRAYFORMULA(REGEXREPLACE(SUBSTITUTE(TRANSPOSE(QUERY(TRANSPOSE(
IF(REGEXMATCH(""&G1:K1, SUBSTITUTE(E3:E, "_", "|")), G3:K, "x"))
,,99^99)), " ", "_"), "^_.*_$", ))
Ok, so I've been stuck on this for a while now trying to create an array formula that will do the following:
Let's pretend the data I'm working with appears as such:
SHEET 1 SHEET 2
| A | B | C | D | E | F | A | B |
1 | Up | Down | Down | Up | Down | 1 | Red | $50 |
2 | Red | Green| White| Black | Blue | 2 | Green | $100 |
3 | $50 | $200 | $15 | Hidden| | 3 | White | $10 |
4 | | $200 | | Hidden| $100 | 4 | Black | $70 |
5 | $50 | $200 | $15 | Hidden| | 5 | Blue | $100 |
Using that data, I need a formula in column F (that I can copy and paste for subsequent cells in the column).
My actual sheet has about 45 columns across and about 25 rows down with sensitive financial information, so I'm using pretend data above on a smaller scale.
So the formula should look something like this (this would be posted in column F of Sheet 1). It needs to check if $A$1:$E$1="Up" (disregard anything that is not "Up") in addition, IF that array = "up" then check to make sure $A3:$E3>$0 (I intend to exclude blank values and "hidden" values), then IF those 2 criteria are met, then return the corresponding value from $A$2:$E$2 and use that as a search key in a lookup formula (HLookup, Vlookup, offset+match, etc). So for example, the Formula in F3 would match those 2 criteria (which would be A2 [red] and then sum together all of the matching values - in this case, there is only 1 matching value, but usually there will be 3-5).
Attempt 1:
=ArrayFormula(SUM(OFFSET('Sheet 2'!$B$1,MATCH(IF(AND($A$1:$E$1="Up",$A3:$E3>"$0"),$A$2:$E$2,""),'Sheet 2'!$A$1:$A$5,0),0)))
Attempt 2:
=ArrayFormula(SUM(IF(AND($A$1:$E$1="Up",$A3:$E3>"$0"),OFFSET('Sheet 2'!$B$1,MATCH($A$2:$E$2,'Sheet 2'!$A$1:$A$5,0),0),"")))
My first attempt attempts to create an array for the Match search key that uses the IF statements to determine the results to be used in the match condition (In the example above, the only result would be "Red", but in the actual sheet, it could be an array like "Red, Blue, Yellow").
My second attempt attempts to first determine which cells we are using (based on IF conditions), then if the cell matches the IF then we will OFFSET the particular cell.
The errors I have noticed is that match doesn't seem to like my array in the search key. The first attempt results with an error saying it cannot find " in the range. (I guess I don't understand why it was searching for that...?). The 2nd attempt seems to fail because it uses the ENTIRE range ($A$2:$E$2), instead of only the cells that match the criteria.
I have tried such things as: VLookUp, HLookUp, LookUp (but it is not a sorted list). I've tried some other weird tactics, but non seem to give me what I need.
So far, the best I have come up with is:
=ArrayFormula(SUM(IF($A$1:$E$1="Up",IF($A3:$E3>"$0",0,OFFSET('Sheet 2'!$B$1,MATCH($A2:$E2,'Sheet 2'!$A$1:$A$5,0),0)),0)))
In my sheet, it found 3 cells that matched the criteria, but instead of adding Cell 1 value, Cell 2 value, and Cell 3 value, it just added Cell 1 value 3 times. So it seems it was only using $A2 (the first option in the range for the match search key) and repeated it for every cell that matched.
I have also tried to include the Filter function (instead of IF) to eliminate cells that do not have "up" as their value ($A$1:$E$1).
Use SUMPRODUCT:
=SUMPRODUCT(($A$1:$E$1="Up")*(VLOOKUP($A$2:$E$2,'Sheet 2'!$A$1:$B$5,2,FALSE))*($A3:$E3>0)*(ISNUMBER($A3:$E3)))
Lets say I have a record of logged flights in a range, example below
[A] | [B] | [C][D][...] | [G]
1 Date | Mode | More Data.... | Days Since
2 1 May | Day | .... | Formula here
3 4 May | Night | .... | Formula here
4 6 May | Day | .... | Formula here
5 8 May | Night | .... | Formula here
I can use a formula to get the datedif between each row in column G, similar to
=DATEDIF(A2,A3,"d")
and copy it all the way down the column, but I'm guessing I need an array formula to go back and find the first row above the current row that matches in column B and get the datedif or days between those two dates. I'm assuming an array formula, but what would the best way to go about that be? I need the result to be the days between row 5 and 3 (night) and 4 and 2 (day) and then copied down about 300 rows...
I was looking at another array formula for sorting rows and eliminated blanks, but not sure how to adapt it to this scenario.
To get the difference in days you only have to subtract one date from the other.
LOOKUP function can be used to find the previous match, so try this formula in G2 copied down
=IFERROR(A2-LOOKUP(2,1/(B$1:B1=B2),A$1:A1),"")
format result cell as number with no decimal places
I'm attempting to use sumproduct on 2 sets of timeseries data that is formatted in the following way:
Array 1
| 01/01/2016 | 02/01/2016 | ...
Stock1 | Price1a | Price1b |
Stock2 | Price2a | Price2b |
Array 2
| 01/01/2016 | 02/01/2016 | ...
Stock1 | Volume1a | Volume1b |
Stock2 | Volume2a | Volume2b |
Such that for a given date, the sumproduct formula will perform price * volume for all stocks in that date.
Example:
For 01/01/2016, the formula will return Price1a * Volume1a + Price2a * Volume2a.
Appreciate any help or questions in case it is unclear.
I put the ranges on two different sheets and used this formula:
=SUMPRODUCT((INDEX($A:$C,2,MATCH(E1,$1:$1,0)):INDEX($A:$C,MATCH("ZZZZ",A:A),MATCH(E1,$1:$1,0)))*(INDEX(Sheet3!$A:$C,2,MATCH(E1,Sheet3!$1:$1,0)):INDEX(Sheet3!$A:$C,MATCH("ZZZZ",Sheet3!A:A),MATCH(E1,Sheet3!$1:$1,0))))
The Index/Match define the extents of the arrays. The tables must have the same number of rows for this to work and the Prices to Volumes must be in the same order.
In the first and third INDEX there is ,2,. The 2 is the starting row. If your starting row is different than change it to your starting row number.
The Match("ZZZ"... will find the last row with text in it, so make sure there is nothing below the tables.
Also all the $C references need to change to the extents of your data. It can be larger than your data without detriment, so if you just want to set it to something like $CC it will be fine.
I have a table that I'm trying to populate based on the values of two reference tables.
I have various different projects 'Type 1', 'Type 2' etc. that each run for 4 months and cost different amounts depending on when in their life cycle they are. These costings are shown in Ref Table 1.
Ref Table 1
Month | a | b | c | d
---------------------------------
Type 1 | 1 | 2 | 3 | 4
Type 2 | 10 | 20 | 30 | 40
Type 3 | 100 | 200 | 300 | 400
Ref Table 2 shows my schedule of projects for the next 3 months. With 2 new ones starting in Jan, one being a Type 1 and the other being a Type 2. In Feb, I'll have 4 projects, the first two entering their second month and two new ones start, but this time a Type 1 and a Type 3.
Ref table 2
Date | Jan | Feb | Mar
--------------------------
Type 1 | a | b | c
Type 1 | | a | b
Type 2 | a | b | c
Type 2 | | | a
Type 3 | | a | b
I'd like to create a table which calculates the total costs spent per project type each month. Example results are shown below in Results table.
Results
Date | Jan | Feb | Mar
-------------------------------
Type 1 | 1 | 3 | 5
Type 2 | 10 | 20 | 40
Type 3 | 0 | 100 | 200
I tried doing it with an array formula:
Res!b2 = {sum(if((Res!A2 = Ref2!A2:A6) * (Res!A2 = Ref1!A2:A4) * (Ref2!B2:D6 = Ref1!B1:D1), Ref!B2:E4))}
However it doesn't work and I believe that it's because of the third condition trying to compare a vector with another vector rather than a single value.
Does anyone have any idea how I can do this? Happy to use arrays, index, match, vector, lookups but NOT VBA.
Thanks
Assuming that months in results table headers are in the same order as Ref table 2 (as per your example) then try this formula in Res!B2
=SUM(SUMIF(Ref1!$B$1:$E$1,IF(Ref2!$A$2:$A$6=Res!$A2,Ref2!B$2:B$6),INDEX(Ref1!$B$2:$E$4,MATCH(Res!$A2,Ref1!$A$2:$A$4,0),0)))
confirm with CTRL+SHIFT+ENTER and copy down and across
That gives me the same results as you get in your results table
If the months might be in different orders then you can add something to check that too - I assumed that the types in results table row labels might be in a different order to Ref table 1, but if they are always in the same order too (as per your example) then the INDEX/MATCH part at the end can be simplified to a single range