I have a table as below. The goal is to extract only first two shop i.d.s from each country having the status either "Active" or "Completed" or "Discontinued". If there are no shops in the country with that status do not extract. If only one than extract only one. Finally the answer should be in a table showing all the responses with above mentioned criteria.
I tried using AGGREGATE with formula 15 and option 6. in the [K] argument i use MOD. but the problem is it does not stop after 2 extractions. if the country has more shops with the above mentioned status it start again and repeat till all the shops in that country are analyzed. My formula looks like this
INDEX(Sheet1!$B$2:$B$265,AGGREGATE(15,6,(ROW(Sheet1!$A$2:$A$265)-ROW(Sheet1!$A$2)+1)/((Sheet1!$A$2:$A$265=Sheet1!A2)*(Sheet1!$C$2:$C$265<>"SF")),ROUND((MOD(ROW(),2)+0.5),0)))
[TABLE="width: 0"]
<tbody>[TR]
[TD]Country Code
[/TD]
[TD]Shop ID
[/TD]
[TD]Status
[/TD]
[/TR]
[TR]
[TD]4101
[/TD]
[TD]4101001
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]4101
[/TD]
[TD]4101002
[/TD]
[TD]Discontinued
[/TD]
[/TR]
[TR]
[TD]4101
[/TD]
[TD]4101003
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]4101
[/TD]
[TD]4101004
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102001
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102002
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102003
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102004
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102005
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102006
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102007
[/TD]
[TD]Discontinued
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102008
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102009
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102010
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102011
[/TD]
[TD]Scr
[/TD]
[/TR]
[TR]
[TD]4103
[/TD]
[TD]4103001
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4103
[/TD]
[TD]4103002
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]4103
[/TD]
[TD]4103003
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]4103
[/TD]
[TD]4103004
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4103
[/TD]
[TD]4103005
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4103
[/TD]
[TD]4103006
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]4103
[/TD]
[TD]4103007
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]1201
[/TD]
[TD]1201001
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]1201
[/TD]
[TD]1201002
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]1201
[/TD]
[TD]1201003
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]1201
[/TD]
[TD]1201004
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]1201
[/TD]
[TD]1201005
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]4001
[/TD]
[TD]4001001
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4001
[/TD]
[TD]4001002
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4001
[/TD]
[TD]4001003
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4001
[/TD]
[TD]4001004
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]4001
[/TD]
[TD]4001005
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]4004
[/TD]
[TD]4004001
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4004
[/TD]
[TD]4004002
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4004
[/TD]
[TD]4004003
[/TD]
[TD]Discontinued
[/TD]
[/TR]
[TR]
[TD]4004
[/TD]
[TD]4004004
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]4004
[/TD]
[TD]4004005
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4004
[/TD]
[TD]4004006
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]4004
[/TD]
[TD]4004007
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]4004
[/TD]
[TD]4004008
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]4004
[/TD]
[TD]4004009
[/TD]
[TD]Discontinued
[/TD]
[/TR]
[TR]
[TD]8001
[/TD]
[TD]8001001
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]8001
[/TD]
[TD]8001002
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]8001
[/TD]
[TD]8001003
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]8001
[/TD]
[TD]8001004
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]2301
[/TD]
[TD]2301001
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]2301
[/TD]
[TD]2301002
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]2303
[/TD]
[TD]2303001
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]2303
[/TD]
[TD]2303002
[/TD]
[TD]
[/TD]
[/TR]
[TR]
[TD]2303
[/TD]
[TD]2303003
[/TD]
[TD]Screened
[/TD]
[/TR]
</tbody>[/TABLE]
I tried using AGGREGATE with formula 15 and option 6. in the [K] argument i use MOD. but the problem is it does not stop after 2 extractions. if the country has more shops with the above mentioned status it start again and repeat till all the shops in that country are analyzed. My formula looks like this
INDEX(Sheet1!$B$2:$B$265,AGGREGATE(15,6,(ROW(Sheet1!$A$2:$A$265)-ROW(Sheet1!$A$2)+1)/((Sheet1!$A$2:$A$265=Sheet1!A2)*(Sheet1!$C$2:$C$265<>"SF")),ROUND((MOD(ROW(),2)+0.5),0)))
[TABLE="width: 0"]
<tbody>[TR]
[TD]Country Code
[/TD]
[TD]Shop ID
[/TD]
[TD]Status
[/TD]
[/TR]
[TR]
[TD]4101
[/TD]
[TD]4101001
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]4101
[/TD]
[TD]4101002
[/TD]
[TD]Discontinued
[/TD]
[/TR]
[TR]
[TD]4101
[/TD]
[TD]4101003
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]4101
[/TD]
[TD]4101004
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102001
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102002
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102003
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102004
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102005
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102006
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102007
[/TD]
[TD]Discontinued
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102008
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102009
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102010
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]4102
[/TD]
[TD]4102011
[/TD]
[TD]Scr
[/TD]
[/TR]
[TR]
[TD]4103
[/TD]
[TD]4103001
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4103
[/TD]
[TD]4103002
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]4103
[/TD]
[TD]4103003
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]4103
[/TD]
[TD]4103004
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4103
[/TD]
[TD]4103005
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4103
[/TD]
[TD]4103006
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]4103
[/TD]
[TD]4103007
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]1201
[/TD]
[TD]1201001
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]1201
[/TD]
[TD]1201002
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]1201
[/TD]
[TD]1201003
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]1201
[/TD]
[TD]1201004
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]1201
[/TD]
[TD]1201005
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]4001
[/TD]
[TD]4001001
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4001
[/TD]
[TD]4001002
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4001
[/TD]
[TD]4001003
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4001
[/TD]
[TD]4001004
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]4001
[/TD]
[TD]4001005
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]4004
[/TD]
[TD]4004001
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4004
[/TD]
[TD]4004002
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4004
[/TD]
[TD]4004003
[/TD]
[TD]Discontinued
[/TD]
[/TR]
[TR]
[TD]4004
[/TD]
[TD]4004004
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]4004
[/TD]
[TD]4004005
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]4004
[/TD]
[TD]4004006
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]4004
[/TD]
[TD]4004007
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]4004
[/TD]
[TD]4004008
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]4004
[/TD]
[TD]4004009
[/TD]
[TD]Discontinued
[/TD]
[/TR]
[TR]
[TD]8001
[/TD]
[TD]8001001
[/TD]
[TD]Completed
[/TD]
[/TR]
[TR]
[TD]8001
[/TD]
[TD]8001002
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]8001
[/TD]
[TD]8001003
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]8001
[/TD]
[TD]8001004
[/TD]
[TD]Active
[/TD]
[/TR]
[TR]
[TD]2301
[/TD]
[TD]2301001
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]2301
[/TD]
[TD]2301002
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]2303
[/TD]
[TD]2303001
[/TD]
[TD]SF
[/TD]
[/TR]
[TR]
[TD]2303
[/TD]
[TD]2303002
[/TD]
[TD]
[/TD]
[/TR]
[TR]
[TD]2303
[/TD]
[TD]2303003
[/TD]
[TD]Screened
[/TD]
[/TR]
</tbody>[/TABLE]