Last N Days Parameter in Tableau
Parameters are really powerful Tools in tableau. Let's take a look at how we can use a parameter to control last n days in a chart. The use case that we're talking about here is maybe a user doesn't want to view all of the chart they only want to see a certain number of days back. 1. Create a parameter as an integer 2. connect the parameter to the view with a calculation referencing the parameter Parameters are really powerful Tools in tableau let’s take a look at how we can use a parameter to control last n days in a chart All right now that we’re in Tableau desktop let’s take a look at how to use a parameter to control last n days I have a new sheet here just called last end days I’m going to build a simple line chart first using date and sales Just double clicked on date and double clicked on sales to bring them into the view and if I drag sales up to rows it gives me a line chart by default I’m going to change it from here to uh week number give us a couple more marks to work with simple line chart and the use case that we’re talking about here is maybe a user doesn’t want to see all of the the chart they only want to see a certain number of days back my chart now goes from January 6 all the way until February 26 2022 to December 25th 2022. So I’m going to create a parameter it’s going to be an integer first so if I put this drop down here create parameter I get my create parameter pane I’m just going to call it last n I’m going to give it a value of 365. Let’s say 365 days is going to be your default and change it to an integer although a float would have worked as well and the user can choose whatever number they want so allow values to be all I’m going to add my parameter to The View by clicking on the parameter drop down in the parameters pane and show parameter of course parameters don’t do anything until you connect them to your view so I could change this to 300 it’s not doing anything click this drop down to create a calculated field I’m going to create a Boolean so a true or false a yes or no value true or false I guess not yes or no so create calculated field and call this in range And zoom in a little bit I just need to do a test so I need to test if the date is greater than 300 days back and day so my 300 value so I’m going to test if date Is greater than or equal to Um today just as a starting point so this is um testing if date is greater than or equal to today but I need to need to adjust today back to be 300 days ago and the today function is just like it sounds just like it looks it’s just grabbing today’s date which is October 14th 2022. If I wrap today with a date add function so I’m going to type date add put an open parentheses I’m going to add a number of days so I want to put a string value of day Comma negative I’m going to drag in my last n parameter so negative last end days I have another comma and then we have today I’m going to close that out and just read that back here for a second I’m subtracting 300 day days from today and of testing if the date is greater than that date yes or no true or false click apply and okay and if I grab this in range and just put it on color to begin with You can see If I show the color here that true is orange and blue is false so all of these days right here are greater than 300 days ago and all those days in blue are less than 300 days ago click ok and let’s just take a copy of in range so I can hold the control key down or command key down put in range onto filters and I only want True Values click apply okay and I have a line chart of everything in the last 300 days Since my data has a future data in it I might want to add to it so this has this is going all the way to December 25th 2022. Uh which hasn’t happened yet I’m actually going to adjust my in-range calculation I’m going to add a little bit of logic to the if statement to the statement I guess it’s not NF so I’m going to test that date is greater than or equal to 300 days ago let’s zoom in so we can see it and date is less than or equal to today and that’s going to filter out your future values you might not have them in your case uh but you know as a kind of a rule of thumb that I have I always uh when I’m doing in-range calculations I always test if the date is less than today as well click fly and we only have the last 300 days change it to 200 and it adjusts to 200.