23 Nov Previous Value Function in Tableau
At the beginning of my Tableau Journey back then in 2018, I was going through a goal news edition and I came across something similar to the image below.
I thought about it, what if I can do something similar in Tableau with the previous function. So, I jumped onto Tableau with sample superstore to see the possibilities of this. It was surprising to see * in tooltip whenever the list of items per region exceeded 1.
I was cool and happy with the screenshot above but got surprised with the screenshot below;
I got that asterisk because Tableau is trying to aggregate by using (ATTR) and ATTR returns a returns value if it has a single value for all the rows else it throws it out *. I love investigating stuff, so I traced what this could be to how many products we have per state.
Notice that we have 103 marks which indicate 103 products (since this is more than one, the asterisk should be expected according to its definition).
Enough talking and let’s get down to the solution. With Tableau’s previous function we could build something similar to what I wanted. Here is a quick glance at Tableau’s definition of the Previous Function; returns the value of this calculation in the previous row.
Calculated Field
- IF FIRST () =0 THEN MIN ([Product Name]) ELSE PREVIOUS_VALUE (MIN ([Product Name])) + ‘ ,’ +MIN ([Product Name]) END
- LAST () = 0
Tableau worksheet.
- Build the bar chart by dragging States to row and sales to column.
2. Build another worksheet that will be used as a tooltip in the bar chart, here is where we will use the two calculated fields.
a. Create a new sheet
b. Drag States to row, followed by product names, then drag the first calculated field to row.
c. Compute the Table Calculation using Product Name as shown below.
3. Also drag the second calculated field to row, then compute using Product name as shown above.
4. Hold Ctrl and drag the second calculated field to filter, then check True.
5. Go back to the bar chart worksheet, click on the tooltip and insert the second worksheet you just built.
6. Hover on each bar to see if your tooltip is working fine. Then format to your taste.
Now that you know how the Previous value function works in Tableau, you can go ahead to try your hands on it. If you have any more questions, feel free to ask in the comment box. Thanks for reading.
No Comments