Shopping List: utilize Tableau parameter strings

by Caitlin Walsh

During my DS Tableau training, I remember an exercise we completed to learn about Sets; we created a simple dashboard using Sample Superstore data that used dashboard actions to let us add Products to a Set that acted as our 'Shopping List', and found the total cost of the Products we would want to buy.

However, there was a limitation here: we were only adding one of each Product to the Set.  
What if we had wanted to know the cost of having multiple of each Product?

The following solution was inspired by ideas from fellow DSers Morgan and Finn, which I then adapted for a client-facing project during my P2.

The brief:

Create a dashboard where the user can select a certain quantity of a given product, before adding it to a 'Shopping List' to see the total expected cost.

For instance: what can a user expect to pay for 1 apple, 2 oranges, and 3 peaches?

Product Name

Price per Unit

Apple

£0.30

Orange

£0.40

Peach

£0.80

The concept:

We can meet this brief by using parameters in Tableau.

Sure, in this example we only have three Products to worry about: we could create a parameter for each one, where users can set a quantity, and then calculate Price per Unit times Quantity.  However, in most cases we would need this to be far more scalable.

Whereas with Sets we can only add dimension members into a Set or remove them from it, we can use a string parameter to store information from several fields at the same time while we build our Shopping List, and then later extract this information to calculate a final total value.

For example, if I had the following string...

PRODUCT NAME: Apple QUANTITY: 1 PRODUCT NAME: Orange QUANTITY: 2 PRODUCT NAME: Peach QUANTITY: 3

...I could use REGEX functions to extract the information that I need.

How to do it:

  1. Create parameters and fields for selection

First, I'll create the parameter 'Shopping List parameter', and set the data type to string.
I'll then create a second parameter, 'Quantity parameter', with an integer data type.

We will need to create a Sheet where users will select the Product Name and Quantity, then add this information to the Shopping List parameter using a Dashboard Action.  

I will need to create a Calculated Field that allows me to concatenate the information I want to store in my parameter each time I make a new selection; I'll name it 'Shopping List Selection'.
N.B: this field will also need to include any values that have already been added, i.e. the current contents of the Shopping List parameter

[Shopping List parameter] + ' PRODUCT NAME: ' + [Product Name]
+ ' QUANTITY: ' + STR([Quantity parameter])

2.  Create a Sheet where user selections can be made

In a new Sheet, add the 'Shopping List Selection' field to rows; you will see that Tableau creates a row for each unique Product Name:


We will add Product Name to filters on this Sheet, and then show both this filter and the Quantity parameter for users to interact with; make sure the filter is set to Single select, and remove the 'All' option.

We should now have a sheet that displays the most current user selection of both Product and Quantity, and updates as we change this selection:

3. Set up Dashboard/Worksheet Action

We can now add a Change Parameter Action, where clicking on this workbook will update the Shopping List Parameter to reflect what the user has selected.

To make this more user-friendly, we can make sure our Shopping List Selection field is added to detail instead of Text, and create a string field to display as a text button instead; we could also change the Marks type to a shape, which will look even more like a clickable button:

Set up a new Change Parameter Action, with Shopping List Parameter as the target, and our concatenated Shopping List Selection field as the source field:


Now, clicking the button after each new selection should add the Product and its Quantity onto the end of the parameter string:

4. Create a table showing the chosen Shopping List and its total value

We will need to build a new calculation to extract the information we want from our parameter string; we can call it 'Selected Product Quantity'.

What we are looking for is the Quantity associated with each Product Name.

We can use the following functions together in Tableau to extract the Quantity for each Product Name:
REGEXP_REPLACE (string, pattern, replacement)
FIND (string, substring, start)
MID (string, start, length)

INT(
REGEXP_REPLACE(
 MID([Shopping List parameter],
  FIND([Shopping List parameter], ' PRODUCT NAME: ' + [Product  Name] + '  QUANTITY: ') + LEN(' PRODUCT NAME: ' + [Product Name] + ' QUANTITY: '),
 4),
'\D',
'')
)

This calculation looks more complicated than it is!

Let's walk through how this calculated field would help us if the full string looked as follows, and we wanted to know our Orange Quantity:

PRODUCT NAME: Apple QUANTITY: 1 PRODUCT NAME: Orange QUANTITY: 2 PRODUCT NAME: Peach QUANTITY: 3

FIND returns the position number of the character where our Orange information begins (33rd character in the whole string).  We then add this onto the length of the string up to the end of the word 'QUANTITY: ' to work out where our Quantity info for Oranges begins (32 characters, found using LEN).

We can then use this info (i.e. that our Orange Quantity substring will start at the 65th character of the whole string) in a MID function to return our substring, and use it in our REGEXP function.  I have used a MID length of 4, which will return any quantity amount that is 4 digits or fewer (e.g. no more than 9999), so this is what will be returned: '2 PR'

REGEXP_REPLACE then takes this 4 character string, and replaces any non-digit values with a blank: '2'

Our REGEXP result is still a string, so we wrap the whole calculation in INT() to convert it to an integer datatype.

As a last step for this calculation, we will want to add in a check to make sure that the Quantity number is only allocated to the relevant Product: we can use FIND for this again, and return 0 if the current Product is not in the string at all.

IF FIND( [Shopping List parameter],' PRODUCT NAME: ' + [Product Name] + ' QUANTITY: ')=0  
THEN 0
ELSE
  INT(
  REGEXP_REPLACE(
    MID([Shopping List parameter],
     FIND([Shopping List parameter], ' PRODUCT NAME: ' + [Product  Name] + '  QUANTITY: ') + LEN(' PRODUCT NAME: ' + [Product Name] + ' QUANTITY: '),
    4),
  '\D',
  '')
  )
END

We can check that this is working for all of our Products when we add Product Name and our new Selected Product Quantity calculation to a new Sheet:

5. Create a calculation to find combined price based on Quantity

Our final step will be to make a Total Price calculated field that multiplies 'Price per Unit' by 'Selected Product Quantity'

[Selected Product Quantity] * [Price per Unit]

We can add this to our table, and make it more user friendly with currency formatting and a Grand Total:

6. Assemble as a Dashboard

We can now add all of these elements together to create a simple dashboard that gives users a clear selection method, and then shows them what their current Shopping List contains.

N.B: if you had set up your Change Parameter Action as a Worksheet Action, you will need to update it to be a Dashboard Action.

More formatting and functionality can now be added to the dashboard depending on users' needs and requirements (e.g. will they need a 'Reset' button for their Shopping List? Do they want more information about each product?  Will you need to include tax when calculating your total price?).

If you have read this far, I hope this blog has given you some new ideas about how we use parameters in Tableau!
A more extensive example of this method can be seen on my Tableau Public here

Green Apple
Photo by Estúdio Bloom / Unsplash