Picking rows with partitions

I’m constantly faced with situations with data in Salesforce Marketing Cloud where I need to select or identify rows based on some ordered criteria. The row_number() over (partition by x order by y) as ranking is my go-to method.

This particular query finds a random row for each bounce category in the _bounce data view:

Recently, found this shorter method:

Credit @TheGameiswar


Parsing Delimited Fields Values with SQL Cross Apply

Say you wanted parse some field values:

SubscriberListMembership

UUIDLists
AAAA1|2|3|4|5|6|7
BBBB2|4|6|8|10|12|14
CCCC1|2|3

…and output something like this in Marketing Cloud:

SubscriberListMembership_Parsed

UUIDListsList1List2List3List4List5List6List7
AAAA1|2|3|4|5|6|71234567
BBBB2|4|6|8|10|12|142468101214
CCC1|2|3123

Since you can’t use any of the newer T-SQL functions for parsing in Marketing Cloud, you can use Cross Apply to determine the positions of the delimiters and then simple sub-strings to extract each one.

One-Click Unsubscribe with AMPscript

Need to custom one-click unsubscribe landing page in AMPscript? Here’s how I generally code it:

This leverages the AMPscript API functions and mirrors the process you’d take if you were doing this with the Marketing Cloud SOAP API.

Reference:

20190320 Update
If you’re having trouble finding the Publication ListIDs in your account, you can drop this bit of Server-Side JavaScript in your page. It’ll output a list of them.

Send-logging Data Sources

In Marketing Cloud, there’s not really a good way of finding the original data source for a subscriber in a send — especially if you’re sending to multiple data sources for every send.

There is, however, a personalization string called _DataSourceName, outlined on the Data About the Recipient section of the Personalization Strings page in the documentation.

If you’d like to record this data, you can simply add an AMPScript variable in your email or template and set it to _DataSourceName. If your Send Log Data Extension has a column matching the variable name, it’ll get recorded for all of your sends. (Fields that start with an underscore aren’t allowed in Data Extensions, so you can’t just name it the same as the personalization string in order to record it.)

Email/Template Code:

Send Log Data Extension:

If I do a single User-Initiated Send to multiple Data Extensions (DataExtension1 and DataExtension2), the following is written to the Send Log:

JobIDDataSourceNameemailaddr
66172DataExtension1aspriggs@degdigital.com
66172DataExtension2aspriggs@degdigital.com

(I removed ListID, BatchID, SubID, TriggeredSendID, ErrorCode and SendDate columns from the above table for clarity)

Once you have this data in your Send Log Data Extension, you can correlate it with other data using a Query Activity or export for use outside Marketing Cloud.


Reference: