How do you write a function that can utilize sumif across an array? - excel-formula

Is there a way to write a single function to do the following in excel/sheets?
Sum measure_a and measure_b by date
Filter by employee and the new sum_measure_a >= 30
Calculate the mean for all of the new sum_measure_b
Here is an example...
employee
date
measure_a
measure_b
AA
2/1
10
50
AA
2/1
10
50
AA
2/1
20
80
AA
2/2
10
40
AA
2/2
10
50
AA
2/3
20
80
AA
2/3
20
90
BB
2/1
30
100
[![enter image description here][1]][1]
So the expected output would be a function that
Would filter all employee values by lookup (here AA)
Would calculate the sum of measure_a for each date
Would identify 2/1 and 2/3 as the dates required (based on the sum of measure_a >= 30)
Would sum the total for each measure_b for each of those day (here 2/1 = 180 and 2/3 = 170)
And finally finds the mean for the daily totals. So final answer is 175 for AA.
employee
mean_measure_b
AA
175
BB
100

you can try:
=QUERY(QUERY({A2:D},"Select Col1, Col2, SUM(Col3), SUM(Col4) WHERE Col1<>'' GROUP BY Col1, Col2"),"Select Col1, AVG(Col4) Where Col3>=30 GROUP BY Col1 LABEL AVG(Col4)'' ")

In Excel this could be done using Microsoft 365:
=LET(data, A2:D9,
empl, INDEX(data,,1),
date, INDEX(data,,2),
m_A, INDEX(data,,3),
m_B, INDEX(data,,4),
e_d, HSTACK(empl,date),
u, UNIQUE(e_d),
u_1, INDEX(u,,1),
u_2, INDEX(u,,2),
mm_cond,(TRANSPOSE(empl)=u_1)*(TRANSPOSE(date)=u_2),
mm_A, MMULT(mm_cond,m_A),
mm_B, MMULT(mm_cond,m_B),
filtered, FILTER(HSTACK(u_1,mm_B),mm_A>=30),
Unq, UNIQUE(INDEX(filtered,,1)),
mm_cond_B,--(TRANSPOSE(INDEX(filtered,,1))=Unq),
sum_B, MMULT(mm_cond_B,INDEX(filtered,,2)),
count_B MMULT(mm_cond_B,SEQUENCE(COUNTA(INDEX(filtered,,2)),,1,0)),
HSTACK(Unq,sum_B/count_B))
It first calculates the sum of measure a for each unique employee/date combination and the same for measure b.
Then it filters out the lines where the sum of measure a is smaller than 30.
Than it takes the average of the listed values for each employee.

Related

Excel Average Calculation of a Moving Criteria Range

I need to create a logic excel formula that calculates the average of 3 months prior to a specified start date. There are multiple start dates. For instance:
For ID 1, The Start Date is May-2021. I would need for the logic to calculate the average between Feb-2021 to Apr-2021 to get 91.67. For ID 2, the Start Date is Jun-2021, so I would need to calculate the average of Mar-2021 to May 2021 to get 108.33. I also would need to calculate the average of 6 months prior in a separate column.
ID
Start Date
Calculation Result
Jan-2021
Feb-2021
Mar-2021
Apr-2021
May-2021
Jun-2021
1
May-2021
91.67
50
100
75
100
25
0
2
Jun-2021
108.33
0
25
100
175
50
125
3
Apr-2021
83.33
100
150
0
75
0
200
Any help is greatly appreciated! (Not opposed to VBA suggestions either)
use INDEX to create the range.
=AVERAGE(INDEX(D2:I2,MATCH(B2,$D$1:$I$1,0)-3):INDEX(D2:I2,MATCH(B2,$D$1:$I$1,0)-1))
Or if they are true dates we can use AverageIfs:
=AVERAGEIFS(D2:I2,$D$1:$I$1,">"&EOMONTH(B2,-4),$D$1:$I$1,"<="&EOMONTH(B2,-1))

Averaging percentages across filtered groups in Power View

I import data into Excel using Power Query, where I add a new column to calculate a usage percent. I then want to use Power View to visualize this data. However, when applying filters on the data and wanting to view the average usage percent across filters, the results are incorrect as Power View is simply averaging the percentages, rather than calculating the totals of each parameter and applying the formula afterwards. Is there a way to write my formula, so the percentage will be calculated after filters are applied in Power View?
In my example, Usage% = Direct/(Total-Fringe).
Because the denominator is not constant, the usage percent for Group A is not simply an average of the Usage% cells assigned to Group A. The average of Usage% cells in group A is 75%, but the correct usage percent calculated for the Group A using parameter totals is 98/(168-40)= 76.56%. Power View shows the incorrect 75%, as it is simply averaging the cells that correspond to Group A.
I would like to use the filters in Power View to view charts showing usage percent at various levels, including Group and Division, along with other information not shown in the example.
ID Group Division Direct Total Fringe Usage%
1 A AA 40 40 0 1.00
2 A AA 20 40 10 0.67
3 A AB 18 40 15 0.72
4 A AB 20 48 15 0.61
5 B BA 40 40 0 1.00
6 B BA 18 40 12 0.64
7 B BB 12 40 20 0.60
8 B BB 40 48 0 0.83
Create Usage % as a Measure in your Data Model, instead of as a column:
Usage % =
DIVIDE (
SUM ( MyTable[Direct] ),
SUM ( MyTable[Total] ) - SUM ( MyTable[Fringe] ),
BLANK()
)

Display the 3rd highest value of each week

I have Data with a Price Column and a Week Column and I want to display the 3rd lowest price for each of those weeks. I know about the Large function and am assuming that there is a way to combine array formulas with that to receive the wanted results but am stuck on that thought.
Setup:
Week| Price
01 100
01 300
01 200
02 500
02 300
02 600
I want to automatically have the 3rd best price for each of those weeks available so Week 1 : 100 and Week 2: 300, keep in mind that each week has way more pricepoints than 3.
If anything is unclear feel free to ask.
Let's suppose your data would fill a table A1:B6.
In column C write the following formula:
=SUMPRODUCT(($A$1:$A$6=A1)*(B1<$B$1:$B$6))+1
This will rank all the values in column B based on the same value of column A. You will end up with the following table:
1 100 3
1 300 1
1 200 2
2 500 2
2 300 3
2 600 1
All you have to do is to take the rank=3.

Find SUBTOTAL of List at Specific Interval

In Excel, I need to find which row has total sum of next nearest 100 (minimum 100), next total sum must using next value to get total sum.
As example below, because row number will be use to VLOOKUP a data series only when the sum of value equal or minimum every 100.
Row Value Mapping
--- ------ -------
1 1
2 50
3 52 1 # (1+50+52=103) Row #3 in Data series return: David
4 72
5 55 2 # (72+55=127) Row #5 in Data series return: Alex
6 70
7 80 3 # (70+80=150) Row #7 in Data series return: Alicia
At mapping column where I will put VLOOKUP code.
In C2 as,
=IF(AND(SUM(INDEX(B:B, IFERROR(MATCH(1E+99, C$1:C1)+1, 1)):B2)>=100,SUM(INDEX(B:B, IFERROR(MATCH(1E+99, C$1:C1)+1, 1)):B1)<100),MAX(C$1:C1)+1,"")
        
Fill down as necessary.

Return max index value in vlookup

I'm trying to pair a vlookup with a max function. For some reason it only returns #ref every time I try to use it though.
My sheet looks like this:
A - B - C - D - E - F - G
1...
5 - Prod5 id1 $100 $125 $155 $110 $150
6...
A:G is named buyAverages
C:G is named buyAveragesPrices
What I want to do is have a vlookup go and find a value in Col A and then return the highest value in that Col. So example:
A - B
1 - Prod5 *return highest price for Prod5
What I wrote in B1, which failed:
VLOOKUP(A1,buyAverages,MAX(buyAveragesPrices))
So how do I achieve this lookup? Everything I have found is how to use MAX for the lookup value, but nothing to use max on the returned index.
Try this
=MAX(IF(A:A="Prod1",C:G))
This is an Array Formula. i.e you have to press Ctrl+Shift+Enter
If there's only one instance of each Product then you can use INDEX/MATCH like this
=MAX(INDEX(C2:G100,MATCH("Prod 1",A2:A100,0),0))
Longer than Sid's suggestion but doesn't need CSE and might be more efficient if you only have a single match
If you have that formula in Z2, for example, you can use this version to get the location from row 1
=INDEX(C1:G1,MATCH(Z2,INDEX(C2:G100,MATCH("Prod 1",A2:A100,0),0),0))
You can have all in one cell using Vlookup and Max with a nested formula. For example at the top of the page:
A1 = Select the name of the product you want to find the max
A2= MAX(BUSCARV($A$1;$A$3:$F$11;3;FALSO);BUSCARV($A$1;$A$3:$F$11;4;FALSO)
;BUSCARV($A$1;$A$3:$F$11;5;FALSO);BUSCARV($A$1;$A$3:$F$11;6;FALSO))
It's long but you only have to type it once. With this formula we get all the different amounts in each column and then we ask for the maximum. It works if all the products are different. Change the name of the product and you'll find the MAX in the table.
Example Prod8
Prod8 41 ; If we change and you select in A1 Prod4 you'll
get 70 and so on..
Prod1 id1 100 125 155 110
Prod2 50 25 20 75
Prod3 60 65 15 90
Prod4 70 12 50 43
Prod5 100 200 80 25
Prod6 20 28 40 40
Prod7 14 43 60 80
Prod8 22 33 15 41
Prod9 65 48 50 70
Select your range accordingly.
You also could include in A1 a match code to select the name of your products..

Resources