+ Reply to Thread
Results 1 to 17 of 17

Lambda MAP combined with HSTACK

  1. #1
    Forum Contributor
    Join Date
    08-03-2021
    Location
    London
    MS-Off Ver
    365
    Posts
    771

    Lambda MAP combined with HSTACK

    Hi,

    I have got this working but not sure why initial approach caused an error ;

    sum figures for each year ;

    Please Login or Register  to view this content.
    but I wanted to stack the result alongside each year Year / value , so I put Hstack in front of the sum;

    Please Login or Register  to view this content.
    this gives a calc error LET(yr, UNIQUE(YEAR(D3:D96)), MAP(yr, LAMBDA(x, x ))) does return the years so I don't see why the problem,

    I have found I can get what I want using ;

    Please Login or Register  to view this content.

    But if someone can see why the Hstack in front of SUM does not work I would like to know; I have attached workbook all examples;


    Richard
    Attached Files Attached Files

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,387

    Re: Lambda MAP combined with HSTACK

    What you need to do is perform the sum calculation first, then stack and then pull unique values:

    =LET(y,YEAR(D3:D96),u,F3:F96,UNIQUE(HSTACK(y,BYROW(y,LAMBDA(r,SUMPRODUCT((y=r)*u))))))

    LAMBDA doesn't like SUM(IF or SUMIF(S).
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  3. #3
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,741

    Re: Lambda MAP combined with HSTACK

    The MAP function returns an array that is the same size & shape as the array you give it. Therefore it cannot return two columns when you give it one, hence the error.

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,973

    Re: Lambda MAP combined with HSTACK

    Quote Originally Posted by AliGW View Post
    LAMBDA doesn't like SUM(IF or SUMIF(S).
    That is not true. LAMBDA will work fine with SUMIF(S), subject to the usual constraints of those functions.
    Last edited by AliGW; 08-09-2024 at 03:48 AM. Reason: Typed response into post by accident - response removed and added to a new ppost.
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Forum Contributor
    Join Date
    08-03-2021
    Location
    London
    MS-Off Ver
    365
    Posts
    771

    Re: Lambda MAP combined with HSTACK

    Thanks to both, I know this is not the same but it is why I wanted to put the Hstack where I did , this does work;;

    =LET( n, E4:E16,it,F4:F16, f, H4:H6, REDUCE(E3:F3, f, LAMBDA(x,y,VSTACK( x, IFNA( HSTACK( y, UNIQUE( FILTER(it,n = y ))),y)))))


    but this does not have a sum or if

    Richard.
    Attached Files Attached Files

  6. #6
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,741

    Re: Lambda MAP combined with HSTACK

    Glad to help & thanks for the feedback.

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,973

    Re: Lambda MAP combined with HSTACK

    REDUCE and MAP are very different. REDUCE returns one result (though that can be an array) whereas MAP returns a result for every item of the input array. If you try and use HSTACK with MAP, you'd be trying to return increasingly large arrays for each item of the original array, which couldn't possibly work.

  8. #8
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,387

    Re: Lambda MAP combined with HSTACK

    Quote Originally Posted by romperstomper View Post
    That is not true. LAMBDA will work fine with SUMIF(S), subject to the usual constraints of those functions.
    I didn't say it didn't work, rather that it didn't like those functions. I was specific in my choice of words. There are occasions where they won't work, but SUMPRODUCT will.

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,973

    Re: Lambda MAP combined with HSTACK

    Yes, there are occasions where they won't work, but that has nothing to do with the use of LAMBDA, which has no effect on the operation of functions it calls. Your statement was misleading at best.

    BTW, if you're going to edit people's posts it would be nice if you left a note indicating why?

  10. #10
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,387

    Re: Lambda MAP combined with HSTACK

    I didn't edit it - I accidentally clicked the wrong button when aiming for the Reply With Quote button and typed my reply into your post before I realised what I'd done. I then had to remove it - that's all. I can send you a log of the changes, if you'd like. I'll add an edit note - should have done that. Need more coffee ...

    I accept that my statement might heve been misleading, but I've seen those functions trip so many people up with LAMBDA that I think it's useful to float the possibility of using SUMPRODUCT.

  11. #11
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,973

    Re: Lambda MAP combined with HSTACK

    I don't need a forensic accounting: I was curious precisely because I couldn't see any change! Now it makes sense.

    Incidentally, the OP didn't even use SUMIF - it was SUM(IF( which doesn't have the same limitations anyway...

  12. #12
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,387

    Re: Lambda MAP combined with HSTACK

    OK - for the benefit of everyone here and for the record, could you please explain these limitations so that the OP knows what they are? I'm not very good at this sort of thing and don't want to cause any further misunderstandings.

    I don't need a forensic accounting
    Sorry - damned if you do, damned if you don't!

  13. #13
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,973

    Re: Lambda MAP combined with HSTACK

    Sure: the limitations of the xxxIF(S) functions (e.g. SUMIF/SUMIFS/COUNTIF/AVERAGEIF) are that everything apart from the criteria for the IF part have to be actual ranges on a sheet, not arrays. So, for example, with SUMIF:

    =SUMIF(criteria_range, criterion,sum_range)

    The first and third arguments have to be ranges. (the criterion itself may or may not be)

    The main limitation of that is that you can't do other calculations on a range and then pass it to the functions. So, let's say you have dates in A2:A10 and values in B2:B10 for example, and you want to sum values for a specific year. You cannot do something like:

    =SUMIF(YEAR(A2:A10),2024, B2:B10)

    because the YEAR function will return an array of numbers, not an actual worksheet range. You either have to use a different function, or use SUMIFS and specify start and end dates of the relevant year:

    =SUMIFS(B2:B10,A2:A10,">="&DATE(2024,1,1), A2:A10,"<="&DATE(2024,12,31))

    Similarly, you can't use functions like UNIQUE or FILTER to restrict the ranges passed to these functions because, again, they will return arrays, not ranges.

  14. #14
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,110

    Re: Lambda MAP combined with HSTACK

    But, as Ali, has suggested (and I have used), SUMPRODUCT does not have the same limitation. Yes?
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  15. #15
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,110

    Re: Lambda MAP combined with HSTACK

    @romperstomper: are these limitations documented somewhere, or have you just learned (and dissected) over time?

  16. #16
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,973

    Re: Lambda MAP combined with HSTACK

    SUMPRODUCT doesn't have the same limitations, nor does SUM(IF(

    I don't know that there is documentation that states you can't use arrays, but the help for those functions only mentions ranges, not arrays, so I guess it's implied. It's why they don't work with closed workbooks.

  17. #17
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,110

    Re: Lambda MAP combined with HSTACK

    Thanks Rory. Until recently, I’d never even thought about Ranges and Arrays, let alone a difference between them.

+ 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. [SOLVED] CALC Error within LAMBDA
    By Dicken in forum Office 365
    Replies: 2
    Last Post: 07-25-2024, 05:24 AM
  2. [SOLVED] Question about Lambda
    By FT00 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 05-03-2024, 11:48 AM
  3. Recursive lambda
    By Glenn Kennedy in forum Excel Formulas & Functions
    Replies: 18
    Last Post: 02-13-2024, 11:51 AM
  4. Need help getting LAMBDA to work
    By Newtonus_Prime in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-21-2023, 09:15 AM
  5. [SOLVED] Incorporate SUM by cell color in a formula with HSTACK, MAP and LAMBDA.
    By lixxy in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-21-2023, 06:02 AM
  6. Replies: 14
    Last Post: 09-21-2023, 12:22 AM
  7. [SOLVED] LAMBDA iteration
    By Slabu in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 07-20-2021, 08:39 PM

Tags for this Thread

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