2
votes

I'm new to Tableau so I'm still learning.

So I have a basic graph. From the parameter, the user can choose to display the data in either a line graph or a slope graph. They can also filter on date range and location. It's basically a graph showing visits by location. The slope graph is just a straight line from the first point to the last point, depending on the date range the user chose.

If the slope graph is chosen, I need to display the numerical, calculated value of the slope somewhere on the graph, in addition to the line. The equation would be (y2 - y1) / (x2 - x1) but how do I calculate the slope in Tableau, when the date range and location can vary?

1
Can you please upload a screenshot of your view/dashboard (complete) so that i can understand it better? Moreover as no sample data is given, please try to translate your problem to superstore data!AnilGoyal
If your x axis is date field won't the slope be relative and not absolute as desired? I mean if x anf y axis represent different units how will you calculate the slope?AnilGoyal

1 Answers

2
votes

Since, you have not given any sample data, I am trying to give a solution/walkthrough in Superstore (sample) data provided with tableau.

Let's take an example that you want to build an area chart for sales over a chosen period (filter 1) and for a chosen sub-category (filter-2).

Step-1 Construct your desired view and add both filters to context (by right clicking them) . This will override the condition that calculations we are going to use in next step(s) will be calculated taken into account the filtered data only.

step-2 Calculate five calculated fields as -- X1 as

{min([Order Date])}

X2 as

{max([Order Date])}

Y1 as

{Sum(
IF [Order Date] = [X1] then [Sales] END
)}

Y2 as

{Sum(
IF [Order Date] = [X2] then [Sales] END
)}

Slope as

([Y2]-[Y1])/DATEDIFF('day', [X1], [X2])

Step-3 add all the five fields to details and create annotation as per taste. In the example I have created a mark annotation as

During the period from <DAY(X1)> to <DAY(X2)>, the rate of increase (+)/decrease (-) in sales for sub-Category <Sub-Category> was  $<SUM(slope)>  per day.

This gives me a view like this. Needless to say I have calculated the slope in $/day; you can tweak it as per taste while inputting datepart in slope.

output