+ Reply to Thread
Results 1 to 22 of 22

Complicated IF formula

  1. #1
    Registered User
    Join Date
    08-29-2016
    Location
    Bali, Indonesia
    MS-Off Ver
    MS Office for Mac 15.24
    Posts
    29

    Complicated IF formula

    Hi all,

    I hope to find some help in preparing my dataset for analyses. To save me from a looot of time-consuming manual work, I need a formula for the following:

    I have the following variables:
    week no.
    week day
    tv contacts
    purchase

    And I want to compute the new variable:
    tv contacts before a purchase in that week

    This new variable should give the number of tv contacts of that day, IF there has not (yet) been a purchase in that week, until that day.
    (If the tv contacts are on the same day as the purchase, the formula should give the number of tv contacts.)

    I hope the Excel sheet I attached clarifies. Looking forward to your responses.

    Kind regards,
    M
    Attached Files Attached Files

  2. #2
    Forum Expert avk's Avatar
    Join Date
    07-12-2007
    Location
    India
    MS-Off Ver
    Microsoft Office 2013
    Posts
    3,223

    Re: Complicated IF formula

    Your data & criteria does not link with each other.
    > tv contacts before a purchase in that week : week no & week day always calculate with date. In your data, date not mentioned.
    > (If the tv contacts are on the same day as the purchase) : Which day assum?
    Can you explain what analyses required. which output you required.


    atul


    If my answer (or that of other members) has helped you, please say "Thanks" by clicking the Add Reputation button at the foot of one of their posts.

    Also: if your problem is solved, please take the time to mark your thread as SOLVED by going to the top of your first post, selecting "Thread Tools" and then "Mark thread as solved".

  3. #3
    Registered User
    Join Date
    08-29-2016
    Location
    Bali, Indonesia
    MS-Off Ver
    MS Office for Mac 15.24
    Posts
    29

    Re: Complicated IF formula

    Hi avk,

    Thanks for your quick reply.

    > tv contacts before a purchase in that week : week no & week day always calculate with date. In your data, date not mentioned.
    My dataset also contains a date variable. In the Excel file I only included the variables that I thought were necessary for creating the IF formula.

    > (If the tv contacts are on the same day as the purchase) : Which day assum?
    I want to have a variable that shows the number of tv contacts a consumer has on a day, IF they took place before that consumer purchased, in that given week.
    IF the tv contacts took place on the same day as the purchase, the formula should give the number of tv contacts.
    IF the tv contacts took place on a later day than the purchase, the formule should NOT give the number of tv contacts.

    Can you explain what analyses required. which output you required.
    I need a variable "tv contacts before first purchase in a week". Later I will aggregate the data to week level, and use this new variable as one of the explanatory variables in explaining the purchases of each week.

  4. #4
    Registered User
    Join Date
    08-29-2016
    Location
    Bali, Indonesia
    MS-Off Ver
    MS Office for Mac 15.24
    Posts
    29

    Re: Complicated IF formula

    I hope I made clear what formula I need. If not, please let me know.

  5. #5
    Registered User
    Join Date
    01-03-2017
    Location
    United States
    MS-Off Ver
    2013
    Posts
    12

    Re: Complicated IF formula

    You will need to enter this into the 2nd row(A2) of your sheet. Then press Ctrl+Shift+Enter so it becomes an array formula. Then you can fill it down and it should give you the output you are looking for.

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    I used 1000 as an arbitrary number. You can change it to any number so long as you change all instances to match. This doesn't capture if two purchases are made in a day.

  6. #6
    Registered User
    Join Date
    08-29-2016
    Location
    Bali, Indonesia
    MS-Off Ver
    MS Office for Mac 15.24
    Posts
    29

    Re: Complicated IF formula

    Hi Raven,

    It worked!! This is awesome. Thank you so much!
    Now I need an even more complicated formula, but I will first try it myself.
    Last edited by mavaspa; 05-02-2017 at 11:20 AM.

  7. #7
    Registered User
    Join Date
    08-29-2016
    Location
    Bali, Indonesia
    MS-Off Ver
    MS Office for Mac 15.24
    Posts
    29

    Re: Complicated IF formula

    The formula Raven created for me does exactly what I asked for and thus works great. However, I have an additional request for it.
    I added individual #2 to the dataset, and tried dragging the formula down. However, the formula so far only looks at week number and does not differentiate between individuals.
    Can someone tell, show or explain me how to include this in the formula?

    Also, I tried making one myself for "online contacts before purchase", by simply changing the column letters in the formula, but it didn't work.
    I used the formula:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Can someone check what I did wrong?

  8. #8
    Registered User
    Join Date
    01-03-2017
    Location
    United States
    MS-Off Ver
    2013
    Posts
    12

    Re: Complicated IF formula

    M,

    The matching happens within the MATCH function. The initial value is what is being matched, so if you need it to differentiate between individuals, you will need to put that into the match command. For instance, if the individual is now in column A, and the week in column B, and you need it to be per individual, per week, then you will need to change the match function to the following:

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    This will differentiate between any purchases made by Bob on week one from purchases made by Jim on week one, for instance. The rest of the function should be exactly the same.
    Please report back should you experience any more difficulty.

  9. #9
    Registered User
    Join Date
    08-29-2016
    Location
    Bali, Indonesia
    MS-Off Ver
    MS Office for Mac 15.24
    Posts
    29

    Re: Complicated IF formula

    It took me a while, but it worked! Thanks again!

    Now I need an even more complicated formula, but I will first try myself tomorrow .

  10. #10
    Registered User
    Join Date
    08-29-2016
    Location
    Bali, Indonesia
    MS-Off Ver
    MS Office for Mac 15.24
    Posts
    29

    Re: Complicated IF formula

    Hi Raven and the rest,

    I need three new formulas. (Six more in total, but they are very similar so I hope to find out how the other 3 work after someone helps me with the first three.)

    Now I have a column with tv contacts before purchase, and one with online contacts before purchase. I also created the column tv & online before purchase (on the same day).
    What I want to create are variables that differentiate different routes or sequences. For example: a customer first sees the tv commercial, and later the online commercial; or first the online commercial, and later the tv commercial; or tv commercial and tv commercial on the same day.

    In my dataset, this means I need the following:
    For tv & online: if within a & b, i = 1, before or at the same time f or h = 1, insert 1 (otherwise 0).
    For tv > online: if within a & b, from f h and i, the first one larger than 0 = f, and later h = larger than 0, insert 1 (otherwise 0).
    For online > tv: if within a & b, from f h and i, the first one larger than 0 = h, and later f = larger than 0, insert 1 (otherwise 0).

    (I again added the Excel sheet for clarification purposes)
    Attached Files Attached Files
    Last edited by mavaspa; 05-03-2017 at 08:10 PM.

  11. #11
    Registered User
    Join Date
    01-03-2017
    Location
    United States
    MS-Off Ver
    2013
    Posts
    12

    Re: Complicated IF formula

    M,

    Hello again. For these, there are some steps you can walk through in order to discover these formulas for yourself. I'll help guide you with the first formula so you can see the steps breaking down and how you pull it all back together. First, you need to determine what separate logic forks you will need. So first, within a&b, which is fairly simple and can be applied later. Second, i=1, also a fairly intuitive one. Third, before or at the same time, which is a little tricky, but not impossible. And finally, f or h=1, which is also fairly easy.

    Based on this, the only difficult part of this is figuring out the third logical fork. Fortunately, we already did something eerily similar in a previous post on this, so let's start there.
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Now this formula is checking to see if the row is less than or equal to the first row in the A column grouping that had a 1 in the D column, and returning C2 if so. We only need to make a few minor modifications to make this work for our purposes. And while we're at it, let's take care of the first logic check at the same time, since it works well to do so.
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    I put the place holders in the formula so we know where to put other formulas later in case we need that. It also helps out tremendously when you are putting together what ends up being a fairly large formula. This is just something I personally do to help myself out, but I recommend putting something you will recognize there so you fill out the entire formula the first time and avoid those annoying Excel errors that usually suggest changing your formula to something completely different than what you intended. We need this again for ensuring that we are before h=1, so it's the exact same formula with two letter changes.
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    So, that takes care of the last two logic checks, along with the first one. All we need to do now is check the second logic check.
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Now it's simply combining things. Because the I2=1 is the basis for everything else, it's a lot easier on your processor to evaluate that first. It may not make a difference in a sheet with 30-50 rows of data, but if you have a sheet with 1000s of rows of data, it can be noticeable. So if I does not equal 1, it returns 0, so that's easy.
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Now, you need to add the other part in. We didn't quite finish it, since we didn't include the logic to check either/or in regard to f and h. Logically, you can use the OR function, and change the formulas to check and see if the row is higher than the first row of either f or h, or you can use an AND function and not change the formulas. It's your choice, but I prefer the AND as it makes for less changes in the end.
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Now all we have to do is fill in the last true and false values to match what we want and we're on to testing.
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Remember with these formulas that use the INDEX function and build arrays that they need to be entered as array formulas (Ctrl+Shift+Enter) to work correctly.

    Try going through these steps to come up with your other formulas and let us know if you get stuck. I feel the best way to learn is to do, and figuring it out on your own always feels just a bit more satisfying to me. However, please let us know if you need help with anything and we'd be happy to go through the steps again.
    Last edited by Raven19528; 05-03-2017 at 10:21 PM. Reason: Grammar Correction

  12. #12
    Registered User
    Join Date
    08-29-2016
    Location
    Bali, Indonesia
    MS-Off Ver
    MS Office for Mac 15.24
    Posts
    29

    Re: Complicated IF formula

    Hi Raven,

    Thanks again for your help and explanation. I understand it partly, but I don't understand the formula yet that you gave me in the previous post.

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    What does this mean: ROW()<=ROW(...) -- Which row compared to which row?
    What does this mean: (INDEX(A$2:A$1000,IFNA(MATCH(A2&B2&1,A$2:A$1000&B$2:B$1000&F$2:F$1000,0),ROW())))
    What is the before or at the same time part, and how would I change this in after or at the same time?

    And for the second formula:
    For tv > online: if within a & b, from f h and i, the first one larger than 0 = f, and later h = larger than 0, insert 1 (otherwise 0).
    Could you give me formula? Then I will try to break it down like you did, and change it into the third formula.

    Looking forward to your response again!

  13. #13
    Registered User
    Join Date
    08-29-2016
    Location
    Bali, Indonesia
    MS-Off Ver
    MS Office for Mac 15.24
    Posts
    29

    Re: Complicated IF formula

    I am now trying to copy paste the formulas in my actual dataset, and I have some difficulties. My dataset is 105.840 lines long. When I drag down the function my Excel takes forever and at some point freezes. So far I have only tried the "tv contacts before purchase" variable, so maybe I should do the more complicated formulas (the last three I asked for) manually...

    Is there any way to make my Excel/laptop faster? Or at least prevent it from freezing? (I work on a MacBook Air)

  14. #14
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2404
    Posts
    13,407

    Re: Complicated IF formula

    My dataset is 105.840 lines long.
    Yikes!!

    mavaspa you hadn't mentioned this before. So far solutions offered have included array formulas. If you are not aware of it array formulas are resource hungry. Also concatenating columns puts additional load on the system. Normally none of these things are of great concern. Too many of them will slow the workbook down and even cause it to freeze as you have experienced.

    Thank you for telling us about this.

    Is there any way to make my Excel/laptop faster? Or at least prevent it from freezing?
    Very likely yes.

    Though I have not looked at your uploads yet I suspect these solutions will benefit from helper columns. These can remove much of that calculation overhead. Do you have room in your project for those? They can be hidden if you prefer they not be seen.
    Dave

  15. #15
    Registered User
    Join Date
    08-29-2016
    Location
    Bali, Indonesia
    MS-Off Ver
    MS Office for Mac 15.24
    Posts
    29

    Re: Complicated IF formula

    Quote Originally Posted by FlameRetired View Post
    mavaspa you hadn't mentioned this before.
    No, I'm sorry. I thought it was easier to upload a fictitious smaller dataset and then copy pasting them myself, but I didn't know it would make my Excel so slow.

    Quote Originally Posted by FlameRetired View Post
    Though I have not looked at your uploads yet I suspect these solutions will benefit from helper columns. These can remove much of that calculation overhead. Do you have room in your project for those? They can be hidden if you prefer they not be seen.
    I only use Excel for data preparation. Afterwards, I will export my data back in my SPSS sheet to do the analyses there. I'm sure I have room for them, and I don't mind if they are visible, as I will export only the relevant data to the SPSS sheet anyway.

  16. #16
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2404
    Posts
    13,407

    Re: Complicated IF formula

    mavaspa no worries. I will be looking at this likely in the next few days. Getting ready to sign off now.

  17. #17
    Registered User
    Join Date
    08-29-2016
    Location
    Bali, Indonesia
    MS-Off Ver
    MS Office for Mac 15.24
    Posts
    29

    Re: Complicated IF formula

    That would be great. Thanks already! I am still waiting on my Excel to finish dragging down the formula (started almost 10 minutes ago and it's at 3%. Waiting for it to freeze again.)

    I will upload my Excel sheet with the relevant columns (and all the lines), and include the formulas I already have only in the first line.

  18. #18
    Registered User
    Join Date
    01-03-2017
    Location
    United States
    MS-Off Ver
    2013
    Posts
    12

    Re: Complicated IF formula

    Oh my. I just saw this. Yes, array formulas take quite a bit of processing power, though normally it is negligible due to there not being too much data. 105,000 rows is definitely going to put a strain on any system.

    I agree with FlameRetired that helper columns are your best bet for something like this. I'll see if I can put some brain cells toward this in the next few days too. Unfortunately I am traveling for work, so I may not be able to get back with you for a while. Hopefully FlameRetired will be able to help with the solution while I'm traveling, and I'll check back when I get back to ensure you got a solution.

  19. #19
    Registered User
    Join Date
    08-29-2016
    Location
    Bali, Indonesia
    MS-Off Ver
    MS Office for Mac 15.24
    Posts
    29

    Re: Complicated IF formula

    Raven and FlameRetired, you guys are the best!

    I just kept my Excel running for a few hours to drag down the formula in column F. Then I copied and pasted (paste special) to only save the values instead of the formula. Now I have the right data in my dataset and the sheet it not slow because of complicated formulas. I'll let my Excel run through the night to do the same thing for column H.
    In columns I and J, I only included the formula in the first row. I will do the same trick with these columns, unless one of you have a better suggestion. I might code the variables in K, L, M, N and O manually, since dragging the formulas takes such a long time, and I only have 217 online exposures in the 105.840 lines anyway..

    I attached the datasheet again. It would be awesome if you have advice how to do it with formulas in a better way, but otherwise I'll manage manually as well.

    Thanks again, and safe travels!

    EDIT: I get upload errors. I'll try again tomorrow morning.
    EDIT2: Now I understand why it fails: the Excel doc is 4MB...
    Last edited by mavaspa; 05-05-2017 at 02:06 PM.

  20. #20
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2404
    Posts
    13,407

    Re: Complicated IF formula

    @ Edit 2

    Have you tried uploading a *.zip file?

    For the first formula in column F "with formula???" try this non array formula. I have never tried this kind of construction on so much data so I don't know what you can expect performance wise. Try it and let me know.

    =(INDEX(B:B,MATCH(A2,A:A,0)):INDEX(B:B,MATCH(A2,A:A,1))<=
    MATCH(1,INDEX(D:D,MATCH(A2,A:A,0)):INDEX(D:D,MATCH(A2,A:A,1)),1))*
    INDEX(C:C,MATCH(A2,A:A,0)):INDEX(C:C,MATCH(A2,A:A,1))

  21. #21
    Registered User
    Join Date
    08-29-2016
    Location
    Bali, Indonesia
    MS-Off Ver
    MS Office for Mac 15.24
    Posts
    29

    Re: Complicated IF formula

    Yes, even as a zip file it's still 2.5 MB.
    I tried the formula in column F, but it is not working for me. It simply returns the values in my C column: the day of the week. I'm probably doing something wrong in inserting the formula...

    I've kept my laptop running last night, to drag down the formulas for "tv before purchase" and "online before purchase". I copy-pasted them, to keep them as values instead of formulas. Then I searched went the whole sheet to search for sequences starting with tv>online and online>tv, and for sequences ending with tv>online and online>tv (before purchase). I'm almost done with it, and then I can finally start analysing.

    Hence, the first formulas have been super helpful! The last 3 I ended up doing manually, but since the number of online contacts was very low anyway, it was feasible doing it manually. For now, I will keep it like this, and start analysing. (That's more urgent now than deep-diving in Excel.)

    Thank you both very very much for your help, for making me realise the limitless possibilities of Excel and for making me realise I should really sign up for an Excel masterclass.

    M

  22. #22
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2404
    Posts
    13,407

    Re: Complicated IF formula

    mavaspa I failed to mention that I applied that formula to your first upload. Please find attached where I did this.

    I have had a chance to rethink my formula and have come up with something shorter. You will find that formula in column a G.

    =IFERROR(IF(IF(INDEX(B:B,MATCH(A2,A:A,0)):INDEX(B:B,MATCH(A2,A:A,1))<=
    MATCH(2,1/INDEX(D:D,MATCH(A2,A:A,0)):INDEX(D:D,MATCH(A2,A:A,1)),1),1),C2,0),C2)
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need help with complicated formula(s)
    By kyleporter in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-23-2014, 11:08 AM
  2. Need help with a complicated If Formula
    By Chris602 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-23-2014, 04:14 PM
  3. Replies: 1
    Last Post: 03-26-2013, 07:46 PM
  4. Complicated use of IF formula
    By donkeybusiness in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 08-13-2012, 06:17 AM
  5. Complicated IF formula
    By jwaldmann in forum Excel General
    Replies: 3
    Last Post: 02-10-2012, 01:09 PM
  6. Complicated Formula
    By Exxodus in forum Excel General
    Replies: 2
    Last Post: 06-01-2011, 09:04 AM
  7. Rather Complicated Formula
    By paulmaddock in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-08-2008, 09:36 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1