 SOLVED

# Help with a big formula using and, xlookup, if and sumifs

Occasional Contributor

# Help with a big formula using and, xlookup, if and sumifs

Hello all! I'm working on a report (per company policy can't share) on which I would like to optimize with formulas. I made a dummy example as close as I could so I could explain myself better for a specific case I'm having trouble with. Looking to build a formula to do the following:

Search for the ID number on the master list ONLY IF:
On column [Repeated?] it has FALSE, AND on column [MODE] it has anything other than "COLLECT" / Otherwise return a 0.
-If found, retrieve the COST (if the number repeats then sum all costs for matching ID)
-If the ID is not on the master list then the cost must be calculated:
Sum the weight on all matching IDs and multiply that for the corresponding RATE matching the MODE

I'm showing what I have built so far, and the cost calculation works but cannot make the highlighted part work/not sure how to add it.

=IF(AND(E2=FALSE,C2<>"collect"),XLOOKUP(B2,L:L,M:M,XLOOKUP(C2,I:I,(J:J)*((SUMIF(B:B,B2,D:D))),FALSE,0,1),0,1),0)

Also, I would like to implement a minimum for below cases:
For GROUND IDs total cost may not be below 5.28, if the calculation falls below that it must return 5.28
For LTL IDs total cost may not fall below 86.11 so it must return 86.11 if it does.

I'm open to all suggestions as well as questions if something is not clear enough.

15 Replies

# Re: Help with a big formula using and, xlookup, if and sumifs

Since it's a dummy file to begin with, it would be far more helpful to post that dummy file than just an image of it. That way we could work with the file rather than having to re-create it.

What version of Excel do you have? Are you able to use the new functions like UNIQUE, or are you identifying the duplicates ("repeated") manually? The most recent version of Excel is needed for that function (and a few others) to operate.

# Re: Help with a big formula using and, xlookup, if and sumifs

Agreed, I have attached dummy file, also i'm currently using Office 365 for enterprise.

# Re: Help with a big formula using and, xlookup, if and sumifs

I'm not able to follow your explanation as to what you're trying to calculate. So let me send this back to you to suggest another function that might be helpful; feel free, though, to come back with clarification.

I entered this formula in cell O2: =SORT(UNIQUE(B2:C22))

to get this array of unique IDs and associated modes (UNIQUE does require the most current version of Excel, so if it doesn't work, let us know) Beyond that, though, it's not clear to me what the relationship is between all your tables and what you're trying to calculate. I think my UNIQUE list has eliminated the need to look for FALSE in your IF function...but, as I said, it's not clear to me. I'd like to make it a more elegant solution if possible, as I'm sure you would too.

best response confirmed by Ccrossed (Occasional Contributor)
Solution

# Re: Help with a big formula using and, xlookup, if and sumifs

If I understood the logic correctly that could be

``````=LET(
getSum, IF( ISNA( XMATCH([@ID],MasterList[Master list]) ),
XLOOKUP([@MODE],
Rates[MODE],
Rates[RATE]*SUMIF([ID],[@ID],[WEIGHT]),
FALSE,0,1),
SUM( FILTER( MasterList[Cost], MasterList[Master list] = [@ID]) )
),
adjustedSum, IFS( [@MODE] = "Ground", MAX( 5.28, getSum),
[@MODE] = "LTL", MAX( 86.11, getSum),
TRUE, getSum),
IF( AND( [@[Repeated?]]=FALSE,
[@MODE]<>"collect"),
0)
)``````

# Re: Help with a big formula using and, xlookup, if and sumifs

I think there is a mistake in your data. The master list contains a duplicate.

# Re: Help with a big formula using and, xlookup, if and sumifs

My understanding that is by design. If ID:s are repeated in Master List we calculate sum of cost for all such duplicates.

# Re: Help with a big formula using and, xlookup, if and sumifs

Thanks for your reply, I know how complex this worksheet is so I'm more than glad to clarify anything possible. The master list contains a record of IDs and their cost, these can be repeated and I'm trying to get a total cost for said ID, however if any ID is not found on the master list then it should be manually calculated by multiplying a base rate (J:J) by the weight of said ID (if there are multiple matching IDs it's ok we would just have to add the total weight before getting that specific ID's total cost) However!: If and ID has "collect" on C:C and TRUE on E:E then it shall be ignored and given a 0 on total cost. Let me know if that helps!

# Re: Help with a big formula using and, xlookup, if and sumifs

It looks like suggested LET() formula shall work.

# Re: Help with a big formula using and, xlookup, if and sumifs

Apparently @Sergei Baklan understands the spreadsheet and your intent. If his formula works for you, I'm happy to step out of the picture.

I still don't get it. Let me, as best I can, give the source of my confusion.

I do understand that the purpose is ultimately to figure out costs to be attributed to each account.

BUT

• master list contains a record of IDs and their cost :
• cost of what
• does that mean nothing further needs to be done with them? (pretty clearly not, but that doesn't help)
• and if they're repeated, as they are, what does that mean?

And maybe this is just my obsessive compulsive nature, but you are so close to it I think you take for granted the context. It's the "big picture" that's not clear. I have no idea what all this represents, and for me at any rate, knowing what that big picture is helps me navigate the details.

# Re: Help with a big formula using and, xlookup, if and sumifs

***

@Ccrossed wrote:
The master list contains a record of IDs and their cost, these can be repeated and I'm trying to get a total cost for said ID

A master list should contain unique IDs and no duplicates.

Why don't you change the two entries from 100+50 to one entry with 150?

# Re: Help with a big formula using and, xlookup, if and sumifs

@Detlef Lewin , I don't know what is the logic behind, but it's possible to do the calculation with duplicates. Not as easy as with unique ID:s, but it works.

# Re: Help with a big formula using and, xlookup, if and sumifs

Thank you Sergei, it seems to be doing its job. One thing tho, would it be possible to adapt this formula from a table to a range? Original document is continuously growing with now more than 2k rows and each of the tables here are on separate tabs. Thanks!

# Re: Help with a big formula using and, xlookup, if and sumifs

You may but I see no one reason to do that. Structured table automatically add formulas to new rows. It doesn't matter in which sheet you place the table, only table name is used.

Thus you don't need enter formulas to new rows, just add your data and table does the rest.

However, you may stay on the table, convert it to the range from menu and formula will be adjusted automatically. Reference on entire column in formulae is always not a good idea, obvious side effect is performance. To improve it you may use dynamic ranges like

``````=LET(
masterListID,    \$L\$2:INDEX(\$L:\$L,COUNTA(\$L:\$L)),
masterListCost, \$M\$2:INDEX(\$M:\$M,COUNTA(\$M:\$M)),
mode,           \$I\$2:INDEX(\$I:\$I,COUNTA(\$I:\$I)),
rate,           \$J\$2:INDEX(\$J:\$J,COUNTA(\$J:\$J)),
ID,             \$B\$2:INDEX(\$B:\$B,COUNTA(\$B:\$B)),
weight,         \$D\$2:INDEX(\$D:\$D,COUNTA(\$D:\$D)),
getSum, IF( ISNA( XMATCH(\$B2,masterListID ) ),
XLOOKUP( \$C2,
mode,
rate*SUMIF(ID,\$B2,weight),
FALSE,0,1),
SUM( FILTER( masterListCost, masterListID = \$B2) )
),
adjustedSum, IFS( \$C2 = "Ground", MAX( 5.28, getSum),
\$C2 = "LTL", MAX( 86.11, getSum),
TRUE, getSum),
IF( AND( \$E2=FALSE,
\$C2<>"collect"),
0)
)``````

but in any case you shall be sure nothing is added below main data in columns you use. Please check in attached.

In general structured tables were made to improve calculations in cases like yours. But what to use is always your decision.

# Re: Help with a big formula using and, xlookup, if and sumifs

Awesome! yes I also believe a table would be a more efficient way of dealing with the data, so I'm definitely gonna propose that change to our reports. One last thing I would like help with would be to update the formula to look into different sheets for the complementary tables, this so I can translate the formula to our main workbook as smoothly as possible. My knowledge on Excel is still limited so I'm having some trouble understanding some components on your formula.

New dummy attached.

# Re: Help with a big formula using and, xlookup, if and sumifs

Formula will be

``````=LET(
masterListID,   Sheet3!\$A\$2:INDEX(Sheet3!\$A:\$A,COUNTA(Sheet3!\$A:\$A)),
masterListCost, Sheet3!\$B\$2:INDEX(Sheet3!\$B:\$B,COUNTA(Sheet3!\$B:\$B)),
mode,           Sheet2!\$A\$2:INDEX(Sheet2!\$A:\$A,COUNTA(Sheet2!\$A:\$A)),
rate,           Sheet2!\$B\$2:INDEX(Sheet2!\$B:\$B,COUNTA(Sheet2!\$B:\$B)),
ID,             \$A\$2:INDEX(\$A:\$A,COUNTA(\$A:\$A)),
weight,         \$C\$2:INDEX(\$C:\$C,COUNTA(\$C:\$C)),
getSum, IF( ISNA( XMATCH(\$A2,masterListID ) ),
XLOOKUP( \$B2,
mode,
rate*SUMIF(ID,\$A2,weight),
FALSE,0,1),
SUM( FILTER( masterListCost, masterListID = \$A2) )
),
adjustedSum, IFS( \$B2 = "Ground", MAX( 5.28, getSum),
\$B2 = "LTL", MAX( 86.11, getSum),
TRUE, getSum),
IF( AND( \$D2=FALSE,
\$B2<>"collect"),