How to use values from snippets and filters as parameters for parameterized snippets

TL;DR:

name: parameterized_snippet([filter_value]) -- filter_value is the name of a filter or another snippet
snippet query for sql: select [[filter_value]]; -- note the additional pair of []
snippet query for string value: select "[[filter_value]]"; -- note the additional pair of []

---

I originally wanted to write a feature request, but I actually figured out how to do what I wanted, so I'll give you a small how-to instead.

Feeding values from snippets or filters into parameterized snippets makes it possible to reuse code across similar projects. It can be especially powerful when combined with drilldowns.

Let's say we have the following parameterized snippet defined:

name: parameterized_snippet([filter_value])
query: select [filter_value];

And `filter_value` is going to be the value from a filter (or another snippet). To get this replacement we'd have to write it as `[filter_value]`. 

It looks like the string replacement that happens for snippets is only single pass for now, so none of the following works:

[parameterized_snippet([filter_value])];
[parameterized_snippet(filter_value)];
[parameterized_snippet("filter_value")];
[parameterized_snippet("[filter_value]")];

In all cases the quotes and the brackets are stripped and the result is that only the string "filter_value" makes it to the snippet.

As I just realized this is no problem at all, you just have to turn it around: evaluation goes from the outside in, instead of the more usual inside out:

name: parameterized_snippet([filter_value])
query: select [[filter_value]]; -- note the additional pair of []

This will take the parameter string, turn it into a snippet or filter value name, which will then be replaced appropriately by the Periscope engine. :)

If you need a string instead of a column name, simply add some quotes:

query: select "[[filter_value]]";

I hope this is helpful to some other folks out there.

 

Happy periscoping! :)

1reply Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
  • Hey Jan,

    Fantastic post!!

    This is an awesome tutorial (and something we probably need to roll into our documentation 😜). I'm a huge fan of parameterized snippets, all of the data transformations for chart type posts are mine. The only item I'd add from helping a lot of Periscope users debug their Periscope code is to use the Query tab in the chart editor. It's an understated yet powerful component of the SQL Editor that shows you exactly how the query in the text editor is translating via Periscope. It helps identify that these types of transformations within transformations are working as intended. Here's a screenshot of what it looks like in-app:

    Reply Like 1
Like6 Follow
  • 6 Likes
  • 1 yr agoLast active
  • 1Replies
  • 1250Views
  • 2 Following