I have a table with 3 columns: expense, client_id, date. The date has record of clients expense for half a year for each day. Some clients spend many each day, but some not. The expendure ranging from zero to few hundreds. I want to group/count active client, which spend over 50 dollars each day, and calculate how much clients was active between 0-30 days, 30-60 days, 60-90 days, 90-120 days, 120-150 days, 150+. I mean if client spend at least 50 dollars each day along 40 days, i add him to 30-60 days column. client id appears only once each day.
expense | client_id | date |
---|---|---|
20 | 1 | 01/01/2000 |
60 | 2 | 01/01/2020 |
70 | 3 | 01/02/2020 |
the result should be like that
0-30 days | 30-60 days | 60-90 days | 90-120 days |
---|---|---|---|
9 | 3 | 12 | 20 |
the values are count of active clients Thank you a lot
There might be better solutions with less helper columns, but this is what I can offer at the moment.
I generated some random data in columns A
to C
for 10 clients, one month and an expense limit of 100. You will have to adjust the expense threshold and the day ranges to your needs.
D2
is =IF(A2>=10,TRUE,FALSE)
.E2
is =IF(COUNTIFS(B$1:B1,B2,C$1:C1,C2-1)>0,TRUE,FALSE)
.F2
is =IF(AND(D2=TRUE,E2=TRUE),MAXIFS(F$1:F1,B$1:B1,B2),MAXIFS(F$1:F1,B$1:B1,B2)+1)
. Whenever the combination of criteria_met and is_consecutive is FALSE, the group number is increased by one.G2
is =COUNTIFS(B$1:B2,B2,F$1:F2,F2)
.H2
is =IF(G2=MAXIFS(G:G,B:B,B2,F:F,F2),G2,0)
.labels creates the headline. Formula in J3
is =J2&"-"&K2-1&" days"
values counts how often a number between the given thresholds occured. Formula in J4
is =COUNTIFS($H:$H,">="&I$2,$H:$H,"<"&J$2)
.
A:C
) has to be sorted by datePlease let me know, if you need more information.