tableau-desktop

How do I create a third column in tableau that calculates the difference between this year and last year for drug purchases?


I would like to add the third column

I have tried using this calculation lookup(sum([Purchase Dollars]),last()) - lookup(sum([Purchase Dollars]),first())

I do not want the variance in a separate row

I also do not want to create sets for both 2022 and 2023


Solution

  • Create the Calculations

    We just want the sum of [Purchase Dollars] for a given year. So assuming you have a date field we will find the year of TODAY() and then return [Purchase Dollars] when the year of our date field matches the year of the TODAY() function.

    First lets find the dollar amount for the current year and name it [Current Year Purchase Dollars] :

    IF YEAR([your date field]) = YEAR(TODAY()) 
    THEN [Purchase Dollars]
    ELSE 0 #else statement is not really needed
    END
    

    Next lets find the dollar amount for the previous year and name it [Previous Year Purchase Dollars] :

    IF DATEADD('year', -1, [your date field]) = DATEADD('year', -1, TODAY()) 
    THEN [Purchase Dollars]
    ELSE 0 #else statement is not really needed
    END
    

    Lastly we'll make a calculation to compare the difference say [Current Year Purchase Dollar Difference]: (feel free to come up with a shorter name)

    [Current Year Purchase Dollars] - [Previous Year Purchase Dollars]
    

    Formatting

    From the way you described it in your question it seems like you wanted all of these values displayed in a grid format. To do this, drag these three calculated field pills to the right of [GenericName1] on your rows shelf, right click them and choose discrete. You can also do this with variance and it will have it's own column.