SOLVED

Let function used with Index.

%3CLINGO-SUB%20id%3D%22lingo-sub-2383781%22%20slang%3D%22en-US%22%3ELet%20function%20used%20with%20Index.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2383781%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20try%20to%20figurer%20out%20how%20to%20use%20the%20new%20LET%20function%2C%20in%20this%20example%20wich%20has%20the%20same%20INDEX%20function%20several%20times.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20it%20possible%2C%20if%20yes%2C%20aht%20is%20the%20solution%3F%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EExample%20file%20is%20attached.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBest%20regards%3C%2FP%3E%3CP%3E-%20Geir%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2383781%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2383859%22%20slang%3D%22en-US%22%3ERe%3A%20Let%20function%20used%20with%20Index.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2383859%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F9195%22%20target%3D%22_blank%22%3E%40Geir%20Hogstad%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAfraid%20that%20won't%20work%20without%20lambdas%20since%20required%20array%20of%20arrays.%20If%20only%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F214174%22%20target%3D%22_blank%22%3E%40Peter%20Bartholomew%3C%2FA%3E%26nbsp%3Bknows%20some%20trick.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2383864%22%20slang%3D%22en-US%22%3ERe%3A%20Let%20function%20used%20with%20Index.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2383864%22%20slang%3D%22en-US%22%3EYou%20mean%20like%20this%3A%3CBR%20%2F%3E%3DLET(Found%2CINDEX(t_priser%2C%2CXMATCH(J11%2Ct_priser%5B%23Headers%5D))%2CXLOOKUP(SMALL(Found%2C1)%2CFound%2CFound))%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2384121%22%20slang%3D%22en-US%22%3ERe%3A%20Let%20function%20used%20with%20Index.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2384121%22%20slang%3D%22en-US%22%3ESurer%20did.%20Thank%20you%2C%20it%20worked%20perfect.%3CBR%20%2F%3E%3CBR%20%2F%3EI%20was%20a%20bit%20to%20fast%20with%20%22best%20reponse%22%20here.%20Sorry.%3CBR%20%2F%3E%3CBR%20%2F%3EThank%20you.%3CBR%20%2F%3E%3CBR%20%2F%3EBest%20regards%3CBR%20%2F%3E-%20Geir%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2384409%22%20slang%3D%22en-US%22%3ERe%3A%20Let%20function%20used%20with%20Index.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2384409%22%20slang%3D%22en-US%22%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F22322%22%20target%3D%22_blank%22%3E%40Jan%20Karel%20Pieterse%3C%2FA%3E%2C%20Can%20I%20ask%20what%20is%20the%20point%20of%20the%20second%20XLOOKUP%20in%20that%20equation%20is%20for%3F%3CBR%20%2F%3EXLOOKUP(SMALL(Found%2C1)%2CFound%2CFound)%3CBR%20%2F%3EDoesn't%20that%20just%20find%20the%20smallest%20value%20then%20look%20it%20up%20in%20the%20same%20array%20and%20return%20that%20same%20value%20again%3F%20I%20guess%20I'm%20asking%20why%20SMALL(Found%2C1)%20wouldn't%20work.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2384547%22%20slang%3D%22en-US%22%3ERe%3A%20Let%20function%20used%20with%20Index.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2384547%22%20slang%3D%22en-US%22%3ESince%20I%20started.%20It%20was%20just%20to%20practice%20and%20learn%20mostly.%20Good%20observation%2C%20SMALL(found%2C1)%20will%20work%20perfect.%3C%2FLINGO-BODY%3E
Regular Contributor

Hi,

 

I try to figurer out how to use the new LET function, in this example wich has the same INDEX function several times. 

 

Is it possible, if yes, aht is the solution? 

 

Example file is attached.

 

Best regards

- Geir

9 Replies
best response confirmed by Geir Hogstad (Regular Contributor)
Solution

@Geir Hogstad 

Afraid that won't work without lambdas since required array of arrays. If only @Peter Bartholomew knows some trick.

You mean like this:
=LET(Found,INDEX(t_priser,,XMATCH(J11,t_priser[#Headers])),XLOOKUP(SMALL(Found,1),Found,Found))
Surer did. Thank you, it worked perfect.

I was a bit to fast with "best reponse" here. Sorry.

Thank you.

Best regards
- Geir
@Jan Karel Pieterse, Can I ask what is the point of the second XLOOKUP in that equation is for?
XLOOKUP(SMALL(Found,1),Found,Found)
Doesn't that just find the smallest value then look it up in the same array and return that same value again? I guess I'm asking why SMALL(Found,1) wouldn't work.
Since I started. It was just to practice and learn mostly. Good observation, SMALL(found,1) will work perfect.

@Geir Hogstad 

If the objective is to examine the use of the LET function, the following demonstrates the way in which a solution may be built using formula-local names

= LET(
  Found,XLOOKUP(@Modell,t_priser[#Headers],t_priser),
  BestPrice, SMALL(Found,1),
  Utileier, IF({1,0}, t_priser[Utlleier], Found),
  XLOOKUP(SMALL(Found,1),Found,Utileier)
  )

The additional trick is to combine the seller and their prices for the vehicle type into a table and then perform the second lookup to select the best offering.

 

If the entire table were required as a single spilt range, that would need a plunge into LAMBDA to perform the array of array calculations and collect the results for each.

I honestly don't know, I simply converted the formula to leverage the LET function. I did wonder about that though.

@Peter Bartholomew 

I meant one spill for all results is required

@Geir Hogstad 

This is just a 'cobbled' together solution.  There are standard functions which scan a function over an array and collect the results together.  The problem is that they are only standard if you have a functional programming background and I need time to study to acquire such.  What I did was to wrap the previous formula in a lambda function so that it becomes a single function call, e.g.

= ModelBestλ(5)

= LAMBDA(k,
      LET(
        Found,XLOOKUP(INDEX(Modell, k), t_priser[#Headers],t_priser),
        BestPrice, SMALL(Found,1),
        Utileier, IF({1,0}, t_priser[Utlleier], Found),
        XLOOKUP(SMALL(Found,1),Found,Utileier))
    )

I then set up a recursion passing through the indices in decreasing order.

= ForEachλ(5,ModelBestλ)

= LAMBDA(p,fλ,
    LET(
       mb, fλ(p),
       result, IF(p>1, ForEachλ(p-1,fλ), mb),
       IF(p>1, xStackλ(result, mb), mb) )
   )

 Note, the function to calculate the best price for each model is passed to this function as a parameter.  The other thing I needed was a function to add the new 'model best' to the list of results already accumulated.  For this I wrote xStackλ

= xStackλ({1,2;3,4},{5,6})

= LAMBDA(prior,new,
     LET(
        n, ROWS(prior),
        k, SEQUENCE(n+1),
        IF( k<=n, INDEX(prior,k,{1,2}), INDEX(new,{1,2})))
   )

As I suggested at the beginning, this needs considerable tidying up if it is to respond to adding model types dynamically or to be reusable for classes of related problems.