google-sheets

Create grouping "bands" for rows with the same details, Google Sheets


I have a dataset in Google Sheets, and I want to add a new column "E" for "BAND". A band is defined as instances where the "Customer" and the "Amount" are the same and the amount is the same as the previous one. For example, if Jan 200, Feb 200, Mar 300, and Apr 200 are given, Jan and Feb are in the same band but Apr is different, even though they have the same amount.

Here is an example of my data:

Customer    Date    Amount  Currency    Expected Bands
Chogm   2023-04-01  100000  Euro    1
Chogm   2023-05-01  100000  Euro    1
Chogm   2023-06-01  400000  Euro    2
Domasi  2023-03-01  3000    US Dollar   1
Domasi  2023-04-01  3000    US Dollar   1
Domasi  2023-05-01  3000    US Dollar   1
Domasi  2023-12-01  2550    US Dollar   2
Domasi  2024-01-01  2550    US Dollar   2
Domasi  2024-02-01  2550    US Dollar   2
Domasi  2024-03-01  2550    US Dollar   2
Domasi  2024-04-01  2550    US Dollar   2
Domasi  2024-05-01  2550    US Dollar   2
Domasi  2024-06-01  3000    US Dollar   3
Domasi  2024-07-01  3000    US Dollar   3
Domasi  2024-08-01  3000    US Dollar   3
Domasi  2024-09-01  3000    US Dollar   3
Domasi  2024-10-01  3000    US Dollar   3
Chogm   2024-02-01  -200000 Euro    3

Another thing is Chogm has values 100000, 400000, and -200000 at the end of the table.

I would like to use a dynamic formula like ARRAYFORMULA to automatically add the BAND column. How can I achieve this in Google Sheets?

Please see the example sheet provided here https://docs.google.com/spreadsheets/d/1PBaP0sbLkh1Depqz8ORA2Q6640Yu_C28ZZNF52GGcTQ/edit?usp=sharing


Solution

  • Here's one approach you may test out:

    =map(A2:A,C2:C,lambda(Σ,Λ,if(Σ="",,let(Δ,filter(C2:Λ,A2:Σ=Σ),reduce(1,sequence(rows(Δ)),lambda(a,c,max(a,if(index(Δ,c)=index(Δ,max(1,c-1)),a,a+1))))))))
    

    enter image description here