SOLVED

Formulas

Copper Contributor

I have an excel spreadsheet that has information in a table. The table shows prices for a flight based on destination and class (first or economy). I am trying to make it so that when the input is a destination and type of class, the output becomes a price for that "flight" if you will. Anyone know the correct formula to do this? 

12 Replies

@CFrench11 

It would help if you made a small sample workbook available through OneDrive, Google Drive, Dropbox or similar.

best response confirmed by CFrench11 (Copper Contributor)
Solution

@CFrench11 

I don't think it is a case of a 'correct' answer.  There are many variations that will work and it also depends upon the version of Excel that you use.  Using modern functions but stopping short of Lambda functions one might have

= XLOOKUP(
    Destination,PriceTbl[Destination],
      SWITCH(Class,
        "First",PriceTbl[First],
        "Economy",PriceTbl[Economy])
  )

image.png

@CFrench11 

Or then, to pursue my interests rather than the user requirement,

 

= Priceλ(Destination,Class)

 

where

 

Priceλ = LAMBDA(dest, cl,
    LET(
        priceList, SWITCH(
            cl,
            "First",   PriceTbl[First],
            "Economy", PriceTbl[Economy]
        ),
        XLOOKUP(Dest, PriceTbl[Destination], priceList)
    )
);

 

 

Is there an alternative to xlookup, because I do not have the option for xlookup. Is it still true that xlookup, as well as Lambda is only 'rolled out' for insiders?

@CFrench11 

XLOOKUP is available to 'ordinary' users of Microsoft 365 and Office 2021, but not to users of older versions, and perhaps not to users of Microsoft 365 with delayed updates.

INDEX/MATCH should work fine on legacy Excel. You could also use a second MATCH to replace the SWITCH function by a column index to use within the INDEX.
Thank You!

@Peter Bartholomew 

On a side note I was playing around withREF problem.jpgValue Error.jpg substituting xlookup with match/index like you said. I got this reference error, as well as value error. Any idea how to fix that or what I did wrong?

@CFrench11 

I write for newer versions of Excel.  Perhaps this will work.

= INDEX(
    SWITCH([@Class], "First class", Price[First class], "Economy",Price[Economy]),
    MATCH([@Destination], Price[Destination], 0)
  )

Even SWITCH requires 2016 (I think).  I like it because my objective is to make the code as readable as possible.  Other developers aim at as fast as possible to write or as concise as possible.  Normal users do not regard their formulas as code.

 

@Peter Bartholomew Do you know how to fix the problem with the SWITCH formula? Whenever I put in the destination and class the price comes out for the first class regardless of the class I pick. Switch Formula.jpgPrice Formula.jpg

I copied the one in your example exactly. It might not be possible to do what I want given the circumstances, so thanks for working with me.

@CFrench11 

The oddity is that SWITCH does not appear to spill in order to return a full column of prices in the picture you show. 

 

As I said earlier though, it is not a case of 'the correct solution'.  I could write solutions using CHOOSE, SWITCH, a second XLOOKUP or a second MATCH embedded within the INDEX.  Any of the formulae may be placed within a Lambda function or left as part of the main formula; it is possible to search for class first and than destination or the reverse.  As long as you have something that works for you.

1 best response

Accepted Solutions
best response confirmed by CFrench11 (Copper Contributor)
Solution

@CFrench11 

I don't think it is a case of a 'correct' answer.  There are many variations that will work and it also depends upon the version of Excel that you use.  Using modern functions but stopping short of Lambda functions one might have

= XLOOKUP(
    Destination,PriceTbl[Destination],
      SWITCH(Class,
        "First",PriceTbl[First],
        "Economy",PriceTbl[Economy])
  )

image.png

View solution in original post