1

I have this formula:

=LET(a;FRUITS!A2:INDEX(FRUITS!B:B;LOOKUP(2;1/(FRUITS!A:A<>"");ROW(FRUITS!B:B)));
     aa;INDEX(a;;1);
     ab;INDEX(a;;2);
     u;UNIQUE(INDEX(a;;2));
     c;COUNTIF(ab;u);
     d;COUNTIFS(ab;u;
                aa;">="&TODAY()
                      -VLOOKUP(SUBSTITUTE(D2;" ";"");
        {"24HOURS"\0;"2DAYS"\1;"3DAYS"\4;"7DAYS"\7;"2WEEKS"\14;"1MONTH"\30;"3MONTHS"\90;"6MONTHS"\180;"1YEAR"\365;"2YEARS"\730;"3YEARS"\1095;"TOTAL"\999999};
         2;0));
SORT(CHOOSE({1\2\3};u;c;d);{2\1\1};{-1\1\1}))

This is in one sheet where the formula is:
enter image description here

This is the other sheet containing the table with the raw data:
enter image description here

It is not counting by period in my real work file. I don't know why. Probably something to do with date formats? Now when I made this dummy file, I just changed names, and now it is working. The names in the real file are of people instead of fruits, like: "Doe, John", "Jane, Mary" etc. Could this be the problem and not the date format?

Also, I would like to have only 2 columns: one with the names of the unique items, and the 2nd with count "By Period". If I want the total count, I will just choose from the validation menu. The name's column on the left should be sorted by descending order according to the count by period.

Here is a file:
https://1drv.ms/x/s!AhJ6NsWJczYBhSjKQVMab8WlYINT?e=akPt6d

EDIT:
The expected result is below. The count would change according to the period chosen above. In this case by month:

enter image description here

Verminous
  • 490
  • 3
  • 14
  • 1
    The formula is hard to follow, what are you trying to achieve? The screenshot is the expected result from your excel file or the current (wrong result). How do you define a period? For example for 24hrs. Do you want to count from a reference date, correct? Where is this reference date? I would say you need two input parameters: reference date and the period (better a drop-down list of values, rather than having it embedded in the formula). Then those parameters are the input in your formula. Please clarify. Thanks – David Leal Oct 29 '22 at 21:32
  • Hi @DavidLeal I added the expected result as an image. What do you think? – Verminous Oct 29 '22 at 21:42
  • 1
    Still, you need a reference date, right? – David Leal Oct 29 '22 at 21:49
  • I have updated the images I think you will understand better now. I do need dates. Column A has the dates, on the sheet containing the raw data. I am not sure what you mean by "reference" date. – Verminous Oct 29 '22 at 21:51
  • 2
    Check your dates row 86 and 99 and higher. They're text, not dates/time – P.b Oct 30 '22 at 07:54
  • 2
    If you fix the dates you can use table references: `=LET(a,HubLog[[Date]:[Fruit]], aa,INDEX(a,,1), ab,INDEX(a,,2), u,UNIQUE(INDEX(a,,2)), c,COUNTIF(ab,u), d,COUNTIFS(ab,u, aa,">="&TODAY() -VLOOKUP(SUBSTITUTE(D2," ",""), {"24HOURS",0;"2DAYS",1;"3DAYS",4;"7DAYS",7;"2WEEKS",14;"1MONTH",30;"3MONTHS",90;"6MONTHS",180;"1YEAR",365;"2YEARS",730;"3YEARS",1095;"TOTAL",999999}, 2,0)),SORT(CHOOSE({1,2,3},u,c,d),{2,1,1},{-1,1,1}))` – P.b Oct 30 '22 at 07:58
  • 2
    And skipping the middle column: `=LET(a,HubLog[[Date]:[Fruit]], aa,INDEX(a,,1), ab,INDEX(a,,2), u,UNIQUE(INDEX(a,,2)), d,COUNTIFS(ab,u, aa,">="&TODAY() -VLOOKUP(SUBSTITUTE(D2," ",""), {"24HOURS",0;"2DAYS",1;"3DAYS",4;"7DAYS",7;"2WEEKS",14;"1MONTH",30;"3MONTHS",90;"6MONTHS",180;"1YEAR",365;"2YEARS",730;"3YEARS",1095;"TOTAL",999999}, 2,0)),SORT(CHOOSE({1,2},u,d),{2,1},{-1,1}))` – P.b Oct 30 '22 at 08:17
  • 3
    @David Leal the reference date is `Today()` the vlookup value would set the allowable values being `today()-lookupresult`. The initial formula was referenced that way to get dynamical range reference, without creating an array, so it could be used in COUNTIF(S). Using FILTER and MMULT would be possible also – P.b Oct 30 '22 at 10:03
  • Hey @P.b thanks for replying. The dates are formatted for "custom" > "dd/mm/yyyy hh:mm" Is this wrong? Also I tried that formula, the last one skipping middle column, and Excel says there is an error and inserts the formula as if it were text: https://i.stack.imgur.com/3nKcF.png . File here: https://1drv.ms/x/s!AhJ6NsWJczYBhSjKQVMab8WlYINT?e=akPt6d – Verminous Oct 30 '22 at 16:02

2 Answers2

2

Keeping the logic from the initial formula, but switching to table references to simplify the formula and dropping the total count per name, this works:

=LET(a,HubLog[[Date]:[Fruit]],
     date,INDEX(a,,1),
     name,INDEX(a,,2),
     uniquename,UNIQUE(name),
     datecount,COUNTIFS(name,uniquename,                  
                        date,">="&TODAY()
                             -VLOOKUP(SUBSTITUTE(D2," ",""),                               
                                      {"24HOURS",0;                                
                                       "2DAYS",1;                                
                                       "3DAYS",4;                                
                                       "7DAYS",7;                                
                                       "2WEEKS",14;                                
                                       "1MONTH",30;                                                               
                                       "3MONTHS",90;                                
                                       "6MONTHS",180;                                
                                       "1YEAR",365;                                
                                       "2YEARS",730;                                
                                       "3YEARS",1095;                                
                                       "TOTAL",999999},                                 
                                      2,0)),
     SORT(CHOOSE({1,2},uniquename,datecount),{2,1},{-1,1}))
P.b
  • 8,293
  • 2
  • 10
  • 25
  • 1
    I changed the references (`date`,`name`,`uniquename`&`datecount`) for readability – P.b Oct 30 '22 at 20:14
  • Hey @P.b do you know if it is possible to have another formula to count the number of fruits that arrived per day? So in this case we would count not the fruits per name, but any number of fruits that come per day, using the exact same table as a reference. If it is possible I can ask a new question about this. – Verminous Nov 28 '22 at 09:48
  • 1
    It's possible. Do you mean something like: `=LET(d,HubLog[Date],s,INT(MIN(d)),e,INT(MAX(d)),days,SEQUENCE(e-s,,s),qty,COUNTIFS(HubLog[Date],">="&days,HubLog[Date],"<"&days+1),CHOOSE({1,2},days,qty))` ? – P.b Nov 28 '22 at 18:01
  • Yes that works. Do you think there is anyway to remove weekends from it? – Verminous Nov 28 '22 at 19:32
  • Ah I noticed one thing, it's not including the today's date. In my real life file, it's not counting the "fruits" received today – Verminous Nov 28 '22 at 20:42
  • `=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))` – P.b Nov 28 '22 at 20:47
  • 1
    You need to open a new question and share sample data with markdown table. https://www.tablesgenerator.com/markdown_tables – P.b Nov 28 '22 at 20:49
  • I have posted the question here: https://stackoverflow.com/questions/74606961/count-items-sold-per-day-excluding-weekend-and-including-present-day-by-selec – Verminous Nov 28 '22 at 22:05
1

This is what I understand, please clarify if my understanding is correct. In cell: G2 enter the following formula:

=LET(period, SWITCH(E2, "24HRS", 1, "7-DAYS", 7), refDate, E1,
 set, FILTER(TB_Fruits, (TB_Fruits[Date]>= refDate) * 
   (TB_Fruits[Date]<= refDate+period)),
 fruits, INDEX(set,,2),uxFruits, UNIQUE(fruits),
 match, XMATCH(fruits, uxFruits), freq, DROP(FREQUENCY(match, UNIQUE(match)),-1),
 SORT(HSTACK(uxFruits, freq),2,-1)
)

and here is the output:

sample excel file

The screenshot doesn't show all the input data in TB_Fruits. The formula is looking for future dates based on refDate, if you want the opposite, i.e. looking into the past, see the note at the end of this answer.

In cell E1 I have a reference date (starting date) and on cell E2 a drop-down list with possible values: 24HRS, 7-DAYS for example.

Notice that you cannot use COUNTIFS if you have an array (it only works with ranges). I use instead FREQUENCY combined with XMATCH (removing the last bins [greater than the last value] at the end via DROP). It was hard for me to understand your formula, so I took a different approach based on my understanding.

If you need to add more periods you can add additional SWITCH elements. If you need a logical condition or expression (not a value) to define the period, then you can use IFSinstead.

If you still don't have DROP or HSTACK function available in your Excel version, you can use the following approach:

=LET(period, SWITCH(E2, "24HRS", 1, "7-DAYS", 7), refDate, E1,
 set, FILTER(TB_Fruits, (TB_Fruits[Date]>= refDate) *
   (TB_Fruits[Date]<= refDate+period)),
 fruits, INDEX(set,,2),uxFruits, UNIQUE(fruits),
 match, XMATCH(fruits, uxFruits), freq, FREQUENCY(match, UNIQUE(match)),
 SORT(CHOOSE({1,2},uxFruits, FILTER(freq, freq<>0)),2,-1)
)

Note: The above formulas are looking at the future based on refDte, if you want to look into the past just change the filter conditions as follows:

(TB_Fruits[Date]<= refDate) *
       (TB_Fruits[Date]>= refDate-period)
David Leal
  • 6,373
  • 4
  • 29
  • 56
  • I get an error, no idea why, everything looks the same: https://i.stack.imgur.com/kwRoK.jpg – Verminous Oct 29 '22 at 22:49
  • 1
    @Verminous make your shared file editable, so I can check your file. – David Leal Oct 29 '22 at 23:04
  • 1
    @Verminous probably `DROP` is not available for you (Office Insider Beta only). Then remove it from `freq`, and in the final `HSTACK` part use instead of `freq` the following: `FILTER(freq, freq<>0)`. Probably you would have a similar issue with `HSTACK`. – David Leal Oct 29 '22 at 23:19
  • 1
    To replace `HSTACK` use insted as first argument of `SORT`: `CHOOSE({1,2},uxFruits, FILTER(freq, freq<>0))` with those changes it should work. – David Leal Oct 29 '22 at 23:35
  • Its editable this version on my personal OneDrive. The other is on my work environment I can't share. Does not seem to be working for me. Throws an error but does not say what error it is. https://1drv.ms/x/s!AhJ6NsWJczYBhSjKQVMab8WlYINT?e=akPt6d – Verminous Oct 30 '22 at 16:00
  • @Verminous I checked that both formulas are working in your file. I just copy the formula, nothing else and added a drop-down menu for the parameeter – David Leal Oct 30 '22 at 17:21
  • Thx @David Leal Now it works. But why does the data validation have no effect on the counting? If I switch to other time frames I get no change. Is it the format of dates? I double checked and to me everything looks ok. Btw my local time config can be many: reply in the file. – Verminous Oct 30 '22 at 17:59