Feeds:
Posts
Comments

Posts Tagged ‘If’

In one of the earlier posts, I extracted the first, last, min and max occurence from an array for a particular lookup. Refer this post.

The next step perhaps would be to answer if it is possible to extract the second, third occurences and so on. Consequently to extract all occurences for a particular lookup_value.

Consider the same example, as earlier:

tip32-1st-last-min-max

Each of the 4 persons randomly appear for a test and their scores are recorded. I had given methods to extract the scores for 1st and last attempt by each of the 4 perons. Now lets find out their scores for 2nd, 3rd, etc attempts

tip34-nth-occurence

The table above gives the results. Row 1 gives the number of attempt.

The formula used for getting to the result is

F2=IF(COUNTIF(Person,$E2)>=F$1,INDIRECT(ADDRESS(SUMPRODUCT(LARGE((Person=$E2)*ROW($B$2:$B$15),COUNTIF(Person,$E2)+1-F$1)),3)),”-“)

Person is B2:B15

As you would see, I have used sumproduct to avoid using array formula and a combination of indirect and address to extract results.

Here are the steps to understand this formula –

  • Lets begin with the component LARGE((Person=$E2)*ROW($B$2:$B$15),COUNTIF(Person,$E2)+1-F$1
  • Remember Large function has the syntax LARGE(array,k) which returns the kth largest value from the array. For example if k=2, then the function returns second largest value in the array.
  • Now, (Person=$E2)*ROW($B$2:$B$15) would create an array like {0,2,0,0,5,0,0,0,9,0,0,0,0,0,0} which is essentially the row numbers of cells in the array where Person=Mike is true
  • For the k part in large function, I have entered COUNTIF(Person,$E2)+1-F$1. Mike occurs 3 times in array Person, so the k would be 3,2,1, such that 1st attempt score is followed by 2nd attempt score and so on
  • Once this is clear, rest is easy
  • Address function has syntax Address(row_num, column_num, [abs_num]). row_num is given from the large function, column_num is entered as 3, which is the column number of Score, [abs_num] is omitted which returns address in absolute eg $C$2
  • Indirect(“$C$2”) returns the value inC2.
  • If condition at the beginning simply evaluates if we have exhausted all occurences of Mike. Once done, it returns a “-“.

§ Note: This also is a better way for multiple extraction because sorting of original table is not required. 

There are 3 other alternative formulas given in the earlier post to achieve the same result.

Else, if one method is sufficient for you, use this and have fun 🙂

Read Full Post »

Consider a set-up where 4 users randomly appear for a test. Their scores are recorded in the order in which they appear for the test. Our objective is to find their scores in the first and last attempts and also their minimum and maximum scores. Here are the users and their scores:
tip32-1st-last-min-max

The objective is to find the first, last, min and max score for each user. The solution looks like this:

tip32-solution

For first attempt score, the formula is a simple vlookup

F2=VLOOKUP(E2,$B$2:$C$15,2,FALSE)

Person is B2:B15 and Score is C2:C15

For max score, the formula is simply:

H2 {=MAX(IF(Person=E2,Score,0))} entered as an array formula with ctrl+shift+enter

Similarly, for min score, the formula is

I2 {=MIN(IF(Person=E2,Score,100))} entered as an array formula with ctrl+shift+enter

I have entered 100 as it is greater than the largest number in Score. You may replace this with max(Score)+1.

Now for last attempt score, you could use one of the 3 sets of formulas

1. Using Index function – array formula

G2 {=INDEX(Score,MAX((Person=E2)*ROW(Person))-ROW(Person)+1)} entered as an array formula with ctrl+shift+enter

In this formula, max function finds out the last row number for a particular user. For instance for user Andy, MAX((Person=E2)*ROW(Person)) returns 14. To return the score for Andy in the last attempt, I have used index function. The minus part of ROW(Person)+1 simply takes care of cells above array Score.

2. Indirect-address function – array formula

G2 {=INDIRECT(ADDRESS(MAX((Person=E2)*ROW(Person)),3))} entered as an array formula with ctrl+shift+enter

If you feel uncomfortable with entering a number in the formula, you could replace 3 with column(Score)

3. Index – Sumproduct function

If you are tired of using array formulas, sumproduct offers some respite

G2=INDEX(Score,SUMPRODUCT(MAX((Person=E2)*ROW(Person)))-ROW(Person)+1)

The formula is similar to the Index with array formula. Here sumproduct is used differently then the normal usage. Notice that there is only one array in sumproduct. The function treats the parameters entered as an array, so we need not enter it as an array formula.

Similarly, indirect address formula can also be entered with sumproduct without entering it as an array formula.

Give it a try. Have fun 🙂

Answer:

tip32-quiz

Read Full Post »

Days past due analysis, two condition sum, bucketing data, frequency function

Here I have carried out a days past due (DPD) analysis. This requires organizing data in various days bucket and makes use of conditional sum function.
tip31-conditional-sum

14 cases are presented. Each case has a days past due and a corresponding amount. The objective is to put these numbers in various previously identified buckets

tip31-conditional-sum-result

The days past due is defined as (0,1] which means 0<DPD<=1 and so on. This is re-written as in column F.

To calculate the amount in column G the following formulas are used, where DPD is the array B2:B15

G2 {=SUM(IF((DPD>0)*(DPD<=$F2),C$2:C$15,0))}

G3 {=SUM(IF((DPD>$F2)*(DPD<=$F3),C$2:C$15,0))} which can then be dragged till end of column

Both entered as array formula with ctrl+shift+enter

To count occurences in each DPD bucket the following formulas are used

H2 {=SUM(IF((DPD>0)*(DPD<=$F2),1,0))}

H3 {=SUM(IF((DPD>$F2)*(DPD<=$F3),1,0))} which can then be dragged to end of column

You could also use the frequency function for counting occurences

Enter formula in I2 =FREQUENCY(DPD,F2:F11)

Select I2 to I11, press F2 followed by ctrl+shift+enter

Note that the two conditions entered for summing is akin to using SUMIF with criteria as AND(cond1, cond2). However, AND criteria does not work with SUMIF.

Also one could use nested if to achieve the same result. The above format simplifies nested if through using the following logic

TRUE*TRUE=TRUE

All other permutations give a FALSE result, which is exactly a two condition AND criteria.

Have fun figuring this out 😉

Read Full Post »

A quick post.

Here is how you can you can count errors, blanks, numbers, texts in a list.

tip27-count

List A2:A11 is defined as Count_list

To count

  • Errors       {=SUM(IF(ISERROR(Count_list),1,0))}
  • Blanks       {=SUM(IF(ISBLANK(Count_list),1,0))}
  • Numbers {=SUM(IF(ISNUMBER(Count_list),1,0))}
  • Text           {=SUM(IF(ISTEXT(Count_list),1,0))}

The formulas are entered as array formuals with ctrl+shift+enter

Read Full Post »

How do you sum up an array which is a combination of numbers and errors? I faced this problem when carrying out a vlookup. The results_array is a combination of #N/A error and numbers as below:

On summing up tip23-sumwitherrorsResults_array through the formula

=sum(A2:A16) gives #N/A error.

To overcome this problem I made use of the following formula

A17  {=SUM(IF(ISNUMBER(A2:A16),(A2:A16),0))}

This is entered as an array formula with ctrl+shift+enter

The formula works in the same way as the results given in explanation array, where

B2=IF(ISNUMBER(A2),A2,0) and then summing up in B17

If you wish to count the occurences of numbers in the array A2:16 you could use this formula

A18 {=SUM(IF(ISNUMBER(A2:A16),1,0))}

Entered as an array formula with ctrl+shift+enter

Now some not so useful information 😉

724 incidentally is a sum of squares of 18 and 20. Try this =18^2+20^2
Or here is a function that you may use = SUMX2PY2(18,20)

Have fun!

 

 

Read Full Post »

Populate blank rows

Populate blank rows so that they can be filtered or conditionally added

For eg. Zone wise sales figure is given but zone is not populated in each row making it difficult to sum sales figures zone-wise
Data as given in Table 1

tip1-revE2 = A2

E3 = IF(ISBLANK(A3),E2,A3)

This populates all the blank rows of the table

Read Full Post »