2 Replies Latest reply on Sep 9, 2016 4:10 AM by Martin Hastrup

    Slope of fitted trend lines on multiple products

    Martin Hastrup



      I'm sitting with some sales data over multiple products, and would like to rank the products based on how much their sales are currently trending.
      The data is formatted like this:


      Week:            1       |       2        |     3       |       4        |       5

      Product A:   25$     |     30$     |     6$     |     10$     |     45$

      Product B:   4$       |     5$        |     9$     |     35$     |     50$



      My approach would be to fit a polynomial for each product over a time period, and then use the slope of each polynomial to determine whether or not a product is trending at a point in time.

      Tableau has a nice build in feature for fitting a polynomial to a single product's sales over time. But from there I have two problems:


      1. I want to be able to get the slope of the polynomial at each data point.

      2. I want Tableau to fit a polynomial function for all products in my table, and then use this as a way of sorting the products.


      Is there a way to do this? I imagine it might be able to do with some R integration, but how?





        • 1. Re: Slope of fitted trend lines on multiple products
          Bora Beran

          Slope is the rate in change in y values over change in x value. When you say you want to rank products based on slope do you imply there is one slope per product? If that's the case, you can use the difference between values of 1st week and 5th week. Since difference in x value is same for each product, you can compare or sort on this without making the division. If this is what you want you can write this using an LOD calculation as 1st value is the minimum for a given product for the time series and 5th is the maximum.


          If you want a different sort for each week e.g. sort based on slope for 1-2 in a viz, 2-3 in a different viz, 3-4 in a different viz etc.you can do this using a table calculation, you need to look back (or forward) using LOOKUP function so you can compare current point with previous or next.


          If you really want to fit a Nth degree polynomial and use the estimate the polynomial gives for each point to calculate these slopes (as opposed to the observed sales values) then you will need R integration.


          If you can clarify, it would be make it easier for forum users to help.


          On a relevant note, you can add multiple trendlines in the same view. Please try putting Product on color shelf.

          • 2. Re: Slope of fitted trend lines on multiple products
            Martin Hastrup

            Hi Bora


            Thanks for the answer. I think my solution will be to do something like you recommended first, and just look at the difference between the sales per week, and forget about the polynomials for now.