2

Is there a way to do a Loop where it only Loops through Unique values?

Customer | Store
John     | 111
John     | 123
John     | 111
John     | 100

Right now I have a basic loop where it matches on the customers key, but I don't want to skip over values that have already been pulled.

Current Output:

Stores John has Visited
111
123
111
100

Target:

Stores John has Visited
111
123
100

The reason for this is that I want to then want to do a nested loop under each one where I could then list actions/products taken/purchased at each specific store, but I'm afraid that the nested content would duplicate since the outer loop is going to pull in the same store again.

2 Answers 2

4

Yes, if you sort by the column you're wanting to de-duplicate:

%%[
var @i, @rows, @row, @productName, @prevProductName, @rowsToReturn

set @rowsToReturn = 0 /* up to 2000 */

set @rows = LookupOrderedRows("SubscriberProducts" ,@rowsToReturn, "ProductName asc", "EmailAddress",emailaddr)

set @prevProductName = ""

for @i = 1 TO RowCount(@rows) DO

  set @row = Row(@rows,@i)
  set @ProductName = Field(@row, "ProductName")

  if empty(@prevProductName) or @productName != @prevProductName then

     set @prevProductName = @productName

]%%

     <h1>%%=v(@productName)=%%</h1>

%%[ endif ]%%

%%[ next @i ]%%
2
  • Just for clarification: this means that I would have to order by the thing I want to de-duplicate by, correct? In the example you have, I'm taking my rows and ordering them by "ProductName" and then setting "ProductName" as "PrevProductName" so that it can be used to compare the next thing in the line. If I didn't order by ProductName, (Example, if I had ordered by visit date), then there is a potential for duplication, correct? I just want to make sure I understand the caveats to ensure I don't break what I build.
    – Brian C
    Commented Jan 30, 2019 at 20:50
  • Yes, if you want to output it in a different order and still de-duplicate then this likely wouldn't work. You'd be better of adding a query step to the automation to create the data to iterate through (or use @Gortonington's method) Commented Jan 30, 2019 at 22:58
2

@AdamSpriggs answer is very efficient and is likely a stronger answer. I just wanted to share an alternative solution that I have come across as well.

Basically you would create an 'index' var that contains all your values processed in the for loop in it.

For this index var, I would fake a pipe delimiter and create something like set @index = "|123|1234|12345|" for indexing. Then in order to check if the product was already done, you do indexof(@index, "|123|") Notice that I have pipes before and after. This will remove the possibility of indexing 123 from the value of 1234

%%[

    ...

   set @index = "|" /* Starts the string with pipe - must be prior to FOR loop*/

   for @i = 1 TO RowCount(@rows) DO

  set @row = Row(@rows,@i)
  set @ProductName = Field(@row, "ProductName")

  IF Indexof(@index, CONCAT("|",@ProductName,"|")) > 0 THEN

]%%

     <h1>%%=v(@productName)=%%</h1>

%%[ endif 

    set @index = CONCAT(@index, @ProductName, "|")

    next @i
]%% 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .