IF statement inside formulafill lookup?

Hello and thanks for reading. Hope you’re having a great weekend.

I have the following code in a procedure and it works as expected:

formulafill lookup(“PATrackerDatabase2020”,«RecordNumber»,«RecordNumber»,«P1Skills»)

…pulling P1Skills into the search database from the tracker database. However, what I really want to do is a bit more complicated.

In the search database, there is a field called Order, and the value of Order can be P1 or P2. What I’d like to say is “If the Order field matches P1, get P1Skills, else P2Skills.”

This is one of my failed attempts at the formula:

formulafill lookup(“PATrackerDatabase2020”,«RecordNumber»,«RecordNumber»,?(Order=P1,«P1Skills»,«P2Skills»))

Thank you for any help with the syntax, and have a great night.

Best,
Biagio

1 Like

You could retrieve Order, P1 and P2 from the search database, then decide based on Order whether to use P1 or P2. I would also look at superlookup to see if that could do this.

1 Like

Thanks, @CooperT - being relatively new to the program (and programming) I tried to implement your suggestion as follows, but I’m doing something wrong:

if Order = “P1”
field Skills
formulafill lookup(“PATrackerDatabase2020”,«RecordNumber»,«RecordNumber»,«P1Skills»)
endif
if Order = “P2”
field Skills
formulafill lookup(“PATrackerDatabase2020”,«RecordNumber»,«RecordNumber»,«P2Skills»)
endif

I’m sure I screwed that syntax up :blush: Right now it only prints P1Skills to every record. Thank you for any thoughts on how to fix my mistake and implement your suggestion correctly.

Thanks again,
Biagio

I think this would work:

field Skills
formulafill ?(lookup(“PATrackerDatabase2020”,«RecordNumber»,«RecordNumber»,Order)="P1", lookup(“PATrackerDatabase2020”,«RecordNumber»,«RecordNumber»,«P1Skills»), lookup(“PATrackerDatabase2020”,«RecordNumber»,«RecordNumber»,«P2Skills»))

When I wrote my earlier comment, I misunderstood your database so you should ignore it.

Thank you. I tried it out and it gives me this error:

lookup( function: data field Order does not exist.

I double checked to make sure it’s named correctly and it’s in the search database. I know this is a tricky one! As always, any direction appreciated.

Best,
Biagio

Is Order a field in the PATrackerDatabase2020?

No, sorry if I explained that wrong. Order is in the search database. It’s so that I can notate who the school lists as parent 1 and 2. So I’m using that field to decide what to pull from PATrackerDatabase2020, which has both parents in one record. Sorry if I explained that wrong. Thank you so much for your responses. Hope this cleared it up.

Best,
Biagio

I think what you want is:
formulafill ?(lookup(“PATrackerDatabase2020”,«RecordNumber»,«RecordNumber»,Order)=“P1”,lookup(“PATrackerDatabase2020”,«RecordNumber»,«RecordNumber»,«P1Skills»),lookup(“PATrackerDatabase2020”,«RecordNumber»,«RecordNumber»,«P2Skills»))

This is not formatting very well.

Anyway, this seems to be what Cooper said, so what I would suggest is to look at each element of the ?( using the Formula Workshop to isolate what the problem is. Plug each lookup into it, and see what the result or error is.

Here is a reformatted version of Bruce’s formula.

formulafill ?(
    lookup(“PATrackerDatabase2020”,
    «RecordNumber»,
    «RecordNumber»,
    Order)=P1,
    lookup(“PATrackerDatabase2020”,
        «RecordNumber»,
        «RecordNumber»,
        «P1Skills»),
    lookup(“PATrackerDatabase2020”,
        «RecordNumber»,
        «RecordNumber»
        «P2Skills»))

I think this is what you need given your last comment.

formulafill ?(Order="P1",
   lookup(“PATrackerDatabase2020”,
       «RecordNumber»,
       «RecordNumber»,
       «P1Skills»),
   lookup(“PATrackerDatabase2020”,
       «RecordNumber»,
       «RecordNumber»
       «P2Skills»))

You might want to add a default value to your lookups in case the parent has no skills listed. My confusion arose from interpreting your reference to the “search database”. I thought you meant the database being searched, i.e. PATrackerDatabase2020, but now I think you mean the database that is conducting the lookup. The field Order is located in the database conducting the lookup, right?

I note also that this approach assumes that Order in every record is either P1 or P2 and never empty or something else. If that is not the case, then this approach would need to be revised to account for that possibility.
Thanks to Jim for showing us how to format code for the forum, and in a procedure, to make it more legible.

First of all, thank you so much @CooperT, @admin, and @BruceDeB for working through this with me.

Right now, I’m getting a “Missing Right Parenthesis” error on this:

I’ve tried tucking in one and/or several of them in various places where it seemed to make sense, but didn’t get it working.

Hope I’m not being too much of a bother, and appreciate any help getting it there!

All the best,
Biagio

No bother. I don’t think you have the correct formula in your most recent post. See my last post. The first part of the ?( function should be Order=“P1”.
I do not see a missing parenthesis in the formula that I posted. (The last ends with two parentheses, one completing the lookup( function and one completing the ?( function.) Also “P1” must be quoted, since it is a text value that you looking for in the Order field.
Another tool, in case you haven’t found it yet, is the Formula Workshop. You can post a formula there to check if it is correct, and make changes, etc. before you employ it. (I think you may have to omit the carriage returns used for clarity in the forum.) There you can practice with the ?( function and lookup( function, or any other functions.

Thanks @CooperT - when I enter that as in a procedure I still get “Missing right parenthesis”. I tried in the Formula Workshop and it gives me the error “Expression contains a function when an operator was expected.”

Appreciate all the advice – this is easily the most complex part of this database. It’s the last big thing I need to knock out, so thanks again everyone for all the help, and have a great Monday!

Best,
Biagio

Looks like you are missing a comma between these last two parameters.

Try commenting out this line and see if the error goes away. (Put // at the beginning the formula fill statement and that tells the procedure to ignore that line.) I could be wrong, and sometimes it’s hard to see our own mistakes, but I don’t see a missing parenthesis in the statement. Maybe it’s in another statement. I hope someone smarter than I can see the problem.

There is one comma missing in the formula, that is part of the syntax of the P2 lookup.

Thanks everyone. I’m really close. After @gary and @KJM noticed the missing comma, this code now pulls in the proper skills per parent:

formulafill ?(Order=“P1”,
lookup(“PATrackerDatabase2020”,
«RecordNumber»,
«RecordNumber»,
«P1Skills»),
lookup(“PATrackerDatabase2020”,
«RecordNumber»,
«RecordNumber»,
«P2Skills»))

The last fly in the ointment is that for some reason, this operation knocks out the Record Number in the Search Database. Previously, each parent had a record number matching one record in the tracking database (where one record contains both parents.) But after this procedure, the Record Number in the search database goes blank or goes to 0. Here are some visuals:

This didn’t happen when I wasn’t using an if statement, like this:
formulafill lookup(“PATrackerDatabase2020”,«RecordNumber»,«RecordNumber»,«P1Skills») so I’m wondering if somehow the if statement is affecting the record number in a way I don’t understand.

BTW, the way it’s numbered is so each individual parent has the same record number as the matching record in the tracking database. The record has both parents in one record. So two consecutive P1s and P2s in the search database will have the same record number, like this:
1
1
2
2…

Skills ARE updating correctly now (thank you!) I appreciate all the help - and see the light at the end of the tunnel :slight_smile:

Thanks again for any thoughts,
Biagio

Hello,

Just a quick update. As a workaround, I wrote a loop procedure to replace the blanks and zeroes with with the correct “doubling” sequence. Since I never re-sorted the records, it works, but it feels a little fragile and takes awhile, so I don’t think it’s a long-term solution.

If anyone has an idea why using the if statement wiped out my record number I’d really appreciate it (database is almost done!) Appreciate all the previous replies that helped get me here.

Thanks and have a great night,
Biagio

I can’t imagine how the formulafill statement could be changing the Record Number field. If you post more of your code, somebody might figure it out.

Thanks @CooperT - there’s not really any other code in there, so I’m thinking I must’ve done something weird/wrong/silly along the way. Tonight I’m going to try to rebuild the search database from scratch, and see if anything changes. Thank you for all the support.

Best,
Biagio