1

I have a table where column A is the date/time, and column B fruits sold. I need a formula that automatically counts how many fruits were sold per day. With the following additional conditions:

  • I need the present-day included.
  • I need weekends excluded.
  • I also need this formula to respond to a menu with the desired time period.
  • Days with "0" count should not appear.

Here is a sample data:

Date Fruit Period
20/10/2022 18
21/10/2022 15
22/10/2022 10
23/10/2022 4
[...]
27/11/2022 19
28/11/2022 21

I have this formula (not mine tbf):

=LET(d,HubLog[Date],s,INT(MIN(d)),e,INT(MAX(d)),days,SEQUENCE(e-s,,s),
 weekdays,FILTER(days,MOD(days,7)>1),qty, 
   COUNTIFS(HubLog[Date],">="&weekdays,HubLog[Date],"<"&weekdays+1),
 CHOOSE({1,2},weekdays,qty))

But it does not seem to be working properly here. Also, in another file I have it was counting but not including the present day and not responsive to the time period menu. The file below is a simplified version of my real file, but for some reason it does not even seem to work.

Is there anything that can be changed in the formula to achieve the requirements explained above?

Dummy file: https://1drv.ms/x/s!AhJ6NsWJczYBhSjKQVMab8WlYINT?e=uxZOJs

Verminous
  • 490
  • 3
  • 14
  • Nah - you've described/explained this beautifully mate - cheers. RE: "does not seem to be working properly" -- function working fine ignoring drop down. WITH drop down adj. max as req, eg. month vs week: =LET(d_,HubLog[Date],s_,INT(MIN(d_)),e_,INT(MIN(MAX(d_),IF(--ISNUMBER(SEARCH("week",D1)),s_+SEARCH(" ",D1)-1,EDATE(s_,1*MID(D1,1,SEARCH(" ",D1)-1))))),days_,SEQUENCE(e_-s_,,s_),weekdays_,FILTER(days_,MOD(days_,7)>1),qty_,COUNTIFS(HubLog[Date],">="&weekdays_,HubLog[Date],"<"&weekdays_+1),CHOOSE({1,2},weekdays_,qty_)) – JB-007 Nov 29 '22 at 00:19
  • Ty @JB-007 Actually I see it is not responding correctly to the "period" menu. e. g. If I choose the last 3 days it will still count up to the 25th of October. – Verminous Nov 29 '22 at 00:39
  • @Verminous how do you want the result? Average per day for the entire period or the fruits sold on each day of the period considered? – David Leal Nov 29 '22 at 02:50
  • @DavidLeal I just need the total count of fruits sold per day as per the above example, excluding weekends, including always "today", and on the date column always in the format "dd/mm/yyyy". – Verminous Nov 29 '22 at 10:56
  • @Verminous please check the answer I provided. I also tested in the file you shared added DavidLeal tab. It is not clear to me what do you mean by including always "today". I used some logic for the period range, but this is easy to adapt to a different logic. Please provide more details about this logic. Thanks – David Leal Nov 29 '22 at 14:37
  • 1
    @Verminous - if you read what I wrote in entirety you'll see I just offered a souiton for the week.month variation - there's only so muhc space in the comment block after all love! :) cheers. – JB-007 Dec 01 '22 at 23:03

2 Answers2

2

In cell E2 you can use the following formula. This approach uses XMATCH/FREQUENCY, it is just a modification from the answer I provided to your previous question: In Excel: List items, count and sort by descending order, combine with validate menu for search period, just adapted to specific needs of this question (check the answer for additional information):

=LET(dates, A2:A400, endDate, MAX(dates), period, SWITCH(D1,
    "24 HOURS", 1, "3 DAYS", 3, "7 DAYS", 7, "2 WEEKS", 14, "1-MONTH", 30,
    "3-MONTHS", 90, "6-MONTHS", 180, "1 YEAR", 360, "2 YEARS", 720,
    "3 YEARS", 1080),startDate, IF(D1="TOTAL", MIN(dates), endDate-period),
  fDates, INT(FILTER(dates, (dates >= startDate) * (dates <= endDate)
    * (MOD(WEEKDAY(dates),7)>1))), fDatesUx, UNIQUE(fDates),
  match, XMATCH(fDates, fDatesUx), freq, FREQUENCY(match, UNIQUE(match)),
  SORT(CHOOSE({1,2},fDatesUx, FILTER(freq, freq<>0)),1)
)

If you prefer to use Excel Racon's functions (RAnge-based CONditional functions), then you can try the following, which is a similar approach to the formula shared in the question, you get the same result of the below-shared screenshot:

=LET(dates, A2:A400, endDate, MAX(dates), period, SWITCH(D1,
    "24 HOURS", 1, "3 DAYS", 3, "7 DAYS", 7, "2 WEEKS", 14, "1-MONTH", 30,
    "3-MONTHS", 90, "6-MONTHS", 180, "1 YEAR", 360, "2 YEARS", 720,
    "3 YEARS", 1080), startDate, IF(D1="TOTAL", MIN(dates), endDate-period),
  fDates, FILTER(dates, (dates >= startDate) * (dates <= endDate)
    * (MOD(WEEKDAY(dates),7)>1)), fDatesUx, UNIQUE(INT(fDates)),
  counts, COUNTIFS(dates, ">="&fDatesUx, dates, "<"&(fDatesUx+1)),
  SORT(CHOOSE({1,2},fDatesUx, counts),1)
)

Here is the sample output sample excel file

Explanation

The solution looks backward, based on the input parameter on cell D1, but it can be customized differently based on your preferences. In the endDate, in order to get data I use the maximum data from the input dataset, but it can be customized differently, for example, you can use TODAY() but then the result can change once the day changes.

The main idea is to filter the dates based on ranges and weekdays. The condition for weekdays is the following:

MOD(WEEKDAY(dates),7)>1 -> TRUE

Once we have fDates. We cannot use Excel Racon's function, because they require ranges, and since we applied it to the filter result INT is not a range anymore and instead an array. To do a count-if we can use instead XMATCH/FREQUENCY pattern explained in the answer to the previous question.

David Leal
  • 6,373
  • 4
  • 29
  • 56
  • Hey, @David Leal thank you. Is there a way to refer to the data by Tables and not the cells/columns? In this example the table "HubLog". – Verminous Nov 29 '22 at 19:31
  • 1
    change this: `A2:A400` with this: `HubLog[Date]` – David Leal Nov 29 '22 at 19:33
  • Ah pretty basic... :) Also just 1 more thing: it seems not to be counting today. I added some more entries with today's date: 29/11/2022 it is not including these. Can anything be changed in order to include today's count? Also for some reason this formula is not working in the online file in OneDrive, but if I download it and open in Excel app it works. Any reason? That's 2 things now... – Verminous Nov 29 '22 at 19:43
  • Ah after adding the table ```HubLog[Date]``` it counts today! – Verminous Nov 29 '22 at 19:45
  • Forget it now it is working in OneDrive too. – Verminous Nov 29 '22 at 19:48
  • 1
    I have accepted your solution. Thank you for your time. :) – Verminous Nov 29 '22 at 19:49
  • 2
    change `MAX(dates)` with `TODAY()` for `endDates`, but then sometimes you might not get a result if you don't enough data from today backward. I tested my formulas in the file you shared, but you have defined wrong the filter conditions each letter is delimited by space like this: `2 4 H O U R S`. In the tab I created I fixed this. – David Leal Nov 29 '22 at 19:50
  • Actually it will show the todays count with ```MAX(dates)``` not ```TODAY()```. So it's working after I changed to table instead of cells. Also the spaces between letters are as intended. I have changed your formula to work with the periods with spaces and it is working fine now. So thanks again! Cheers. – Verminous Nov 29 '22 at 20:16
  • 2
    SWITCH is indeed a better option than VLOOKUP (like used in his previous post https://stackoverflow.com/q/74248865/12634230) and Yes, why the use of spaces and caps.. – P.b Nov 29 '22 at 20:19
  • Because I needed something a bit more visually spacious. Sorry it's my thing. But it is working so that's what matters. Thanks for your input @P.b – Verminous Nov 29 '22 at 21:21
  • 2
    @Verminous change the font instead of adding spaces, there are fonts that may produce the visual effect you want. For example CooperPlate Gothic Light or Engravers TM, etc. – David Leal Nov 29 '22 at 21:47
2

Just another approach at the definition of 24 hours or 1 month.

I got a bit confused by you starting the filter at a condition named 24 hours while refering to qty's a day. This would imply that if you set the filter at 22:13 today it would have to filter anything between 22:13 yesterday and now. Also 1 month is not equal to 30 days, but EDATE() allows you to calculate in real months. In that case the following solution could be usefull:

=LET(d,HubLog[Date],
     s,SWITCH(D1,
                 "2 4 H O U R S",   NOW()-1,
                 "3  D A Y S",      NOW()-3,
                 "7  D A Y S",      NOW()-7,
                 "2  W E E K S",    NOW()-14,
                 "1  M O N T H",    EDATE(NOW(),-1),
                 "3  M O N T H S",  EDATE(NOW(),-3),
                 "6  M O N T H S",  EDATE(NOW(),-6),
                 "1  Y E A R",      EDATE(NOW(),-12),
                 "2  Y E A R S",    EDATE(NOW(),-24),
                 "3  Y E A R S",    EDATE(NOW(),-36),
                 "A L L",           INT(MIN(d))),
     e,NOW(),
             days,              SEQUENCE(1+e-s,,s),
             weekdays,          FILTER(days,MOD(INT(days),7)>1),
             hourcorrection,    VSTACK(TAKE(weekdays,1),TAKE(INT(weekdays),1-COUNTA(weekdays))),
             qty,               COUNTIFS(HubLog[Date],">="&hourcorrection,HubLog[Date],"<"&hourcorrection+1),
CHOOSE({1,2},INT(hourcorrection),qty))

Or to filter out days without values:

=LET(d,HubLog[Date],
     s,SWITCH(D1,
                 "2 4 H O U R S",   NOW()-1,
                 "3  D A Y S",      NOW()-3,
                 "7  D A Y S",      NOW()-7,
                 "2  W E E K S",    NOW()-14,
                 "1  M O N T H",    EDATE(NOW(),-1),
                 "3  M O N T H S",  EDATE(NOW(),-3),
                 "6  M O N T H S",  EDATE(NOW(),-6),
                 "1  Y E A R",      EDATE(NOW(),-12),
                 "2  Y E A R S",    EDATE(NOW(),-24),
                 "3  Y E A R S",    EDATE(NOW(),-36),                 "A L L",           INT(MIN(d))),
     e,NOW(),
             days,              SEQUENCE(1+e-s,,s),
             weekdays,          FILTER(days,MOD(INT(days),7)>1),
             hourcorrection,    VSTACK(TAKE(weekdays,1),TAKE(INT(weekdays),1-COUNTA(weekdays))),
             qty,               COUNTIFS(HubLog[Date],">="&hourcorrection,HubLog[Date],"<"&hourcorrection+1),
res,CHOOSE({1,2},INT(hourcorrection),qty),
FILTER(res,qty<>0))
P.b
  • 8,293
  • 2
  • 10
  • 25
  • Interesting. In my real file (not the dummy) after I use this it starts counting from the 29th of August if I use 3MONTH period, even though I have no cells with that date. The HubLog table starts at the 25th of October. Same happens with 6MONTH, goes all way back to May 2022. All those days up to the 24th October show as zero with this formula. Any idea why? – Verminous Nov 29 '22 at 21:27
  • It's also including some weekends like the 20th November and 27th November when changing the periods. – Verminous Nov 29 '22 at 21:29
  • 1
    yes, I assumed everything is translated to days so the Month is to be interpreted as the last 30 days, because there was not so much indication in the question about how to interpret a Month, Year, etc. – David Leal Nov 29 '22 at 21:34
  • 1
    I forgot toention that this version shows each date in selected period excluding the weekends as stated. So if a weekday falls within the selected range it'll show regardless if any fruits are found for that day. So it shows an overview of each day for your selection. – P.b Nov 29 '22 at 21:39
  • 1
    I edited the MOD part to work with integers to avoid weekends turning up. – P.b Nov 29 '22 at 22:05
  • What about days that go all the way back to August and May when I change period? Is there a way to remove these? Is it related perhaps with the use of ```EDATE()```. – Verminous Nov 30 '22 at 11:31
  • 1
    It shows each day in selected period excluding weekends. We could exclude days without values, but that was not specified. And I deliberately included them to have a different approach than the answer provided by David Leal. Also I figured, if you select a period and specify you want to know what happened day by day, you'd want to know what happened each day in given period (also the days without data, but within given period) – P.b Nov 30 '22 at 12:33
  • I have edited the specification on my question now. So in this case, what change would have to be made not to show days with zero count, no fruits sold? – Verminous Dec 01 '22 at 08:37
  • 1
    See edit for days with fruit sold only – P.b Dec 01 '22 at 17:05