How to show previous value based on selection from quick filter in Tableau - tableau-api

I like to show the previous period value with difference based on the period selected from the quick filter in Tableau.
For example:
If I select 2019 from quick filter I would like to see the value with the difference compared with previous period (i.e 2018)
(means 2019 (from quick filter) vs 2018).

You can't access a quick filter value directly in tableau instead use parameters where you can access the parameters in calculated fields.
For this requirement LoD serves the purpose, where use a Fixed LoD which will bypass the filtering and get you the values.
For E.g
{Fixed :Sum(if year = 2018 then sales end)}
In worksheet you have 2019 value then use a calculated field to find the difference
Edit-------------------------------------
If quick filter is a single value selection then you can subtract 1 from year field. like below:
{Fixed :Sum(if year = [datbasefield.year]-1 then sales end)}

Instead of using a filter to select your period, use a parameter. Then you can use a calculated field to filter to both the year selected by the parameter and the prior year.
If you then want to calculate the difference between two years, you can use a Quick Table Calc (right click on the green measure pill on whatever shelf you are using).
If you then want to hide that first (empty) value, use the hide command or a table calc filter.
So in a nutshell, read about parameters and table calculations in the on-line help

Related

Tableau Target Vs. Actual - Can not get totals to show correctly

I am having trouble showing the correct totals in my tableau worksheet.
I have supervisors that are part of specific zones that need to complete a certain number of tests in different categories. For example, supervisor 15716 must complete 8 tests in category 1. I need to show the target, which is a number stored in the database and show the actual number of tests in that category that have been completed within a date range. I have it working, but Im not sure if I did it correctly because I can not show any totals.
System target - number stored in database
CountOfSheetID - calculated field
Percent Compliant - calculated field
Try this approach -
First define a calculated field called [Within Date Range?] as
[Date] >= [MyStartDate] AND [Date] <= [LastSelectedDayOfMonth]
and put that new field on the filter shelf, only including data where [Within Date Range?] is True. (You could also just filter the [Date] field if that is flexible enough for you)
The you don't need the CountofSheetId calculated field at all. If you want to know how many records have a non-null value for [SheetID] within your date range, you can simply drop [SheetID] on a shelf and choose to treat it as Measure with the aggregation function COUNT()
Then just build your visualization to show the counts you want (not percentages, the actual counts)
Finally, you can convert counts into Percentages by clicking on the pills for your Measures and choosing Percentage under Quick Table Calcs. You'll want to experiment with the "Compute Using" setting to tell Tableau how to compute your percentages -- i.e. define percentage of "what".
Percentages are implemented as table calcs in Tableau. Read the help to understand table calcs, especially the description of partitioning and addressing.

Have Max value of range of dates filter be todays date

I have a "Range of Dates" filter and what I want is for the max (or right most value) to always be the most recent date which should be today's date. What seems to be happening is that if I leave the dashboard open and come back the next day the max value is yesterday's date and I must manually move the slider over to be today's date. How can I accomplish this?
I find a calculated field is the best way to do this as I have run into the same issues using the out of the box max date filter.
Create a calculated field as follows:
[date] = {FIXED: max([date])}
This creates a True False field where only the records that have the max data and carried through.
Now drag this onto the filter pane and select 'TRUE'.
I've generally seen two basic approaches for this problem: Calculated fields and relative dates.
Use a calculated field or parameter or some combination of calculated fields and parameters with filters. This is similar to what smb suggests in their answer to this question. It also seems to be the most popular approach.
If you don't particularly care about being able to set the end-date with the slider, you could try using relative dates, using the approaches detailed in the accepted answer to this Tableau forum question and in this Tableau Knowledge Base article. Jennifer Vonhagel also gives a second answer to the Tableau forum question farther down that uses a parameter plus calculated field approach.
Additionally, this Tableau Knowledge Base article offers another option (Option 1, in the article) if you have Tableau 10.3+: You can use the "Latest Date Preset" (see here for details) check box in the date filter dialog box. I haven't used this, but it looks promising if you're using Tableau Desktop (seems like it wouldn't work for Tableau Web). The article's Options 2-4 are just riffs on calculated fields, in my opinion.
Two more approaches I've heard of – but never personally seen in the wild:
Push the max date down into the view you put Tableau on top of and let the view do the work.
Use a script to modify the Tableau workbook's XML.

Sorting Data by Latest Date, selecting top 10 and charting (v. 10.0.1)

This is the data table that I have created
I need to sort the data by 'April2017' in descending order and then select the top 10 projects.
When we select top10 basis April2017, the output should be
Instead what I get is
Here is what I've tried out so far,
Created a calculated field
Calculation1 = iif([Year_Month]=MAKEDATE(2017,4,1),[Claim Count],0)
Sorted Projects based on 'Calculation1'
Drag Project to Filter and select Top10 based on sum([Calculation1])
I am unable to understand how the top10 here is being derived.
Where am I going wrong?
The chart that I am trying to get should be similar to
Please help me with this problem.
You can filter to a selected portion of data in a calculation and use as desired. So create a calculated field called, say April_2017_Foobars, defined as:
if datetrunc('month', [Year_Month]) = #04/01/2017# then [Foobars] end
This field return [Foobars] for the April 2017 rows and null for other rows. Nulls are ignored by aggregate functions, so if you aggregate with SUM() or AVG() etc, the effect is to filter to April 2017 for that field alone.
Then you can use April_2017_Foobars for sorting and defining top filters for your Project field. This is a very general technique that is useful in all kinds of situations.
You can generalize it a bit to use a parameter for the special month rather than hard code it - or use an LOD calc to find the last month in your dataset if you always intend to use the latest month.
P.S. You can use the makedate() function instead of a date literal if you prefer and your data source supports that function. Might avoid any confusion about date literal formats being different in various countries.
create Calculation1 field: iif([Year_Month]="April 2017",[Number],0)
sort Project in descending order on Calculation1 Sum
drag Project to filters, and do Top > By Field > Top: 10 by Calculation1 Sum

Tableau - Filter/Parameter For Different Date Ranges

I am trying to set up a filter or parameter for different date ranges in Tableau, such as:
Previous Week
Previous 4-Weeks
Previous 8-Weeks
based on the most recent data in the database. Is this possible?
Edit:
I should have been more clear here.
Is it possible to only have these options in the filter and have it based on the maximum date in the database?
Update
Based on your comment and updated question, you can do it as you intend, though it isn't as straight-forward.
NOTE: I did this very quickly and with a random data set, so it will be important for you to test this thoroughly with your data set.
Steps:
Create a parameter control that the users will use to change the selected date range. I created this as a list of strings with values that I can use directly in a calculated (after simple casting):
Create a calculated field that you will use as a filter that references the selection of the parameter control. It tests to see if the difference (in weeks) between the maximum date in the view and the date of any given row is less than the value of the parameter.
Calculation: INT([Date Filter Parameter]) > DATEDIFF("week",ATTR([Date Field]),WINDOW_MAX(MAX([Date Field])))
Place the calculated field in the filter box and set it to True.
You will be able to filter your data like so:
Original Answer
Yup - that is built into Tableau.
Add your date to the filter and select the "Relative Date" option:
You can set the defaults - users can change these later:
Then just "Show Filter":

How to control number of bars in a bar chart based on date selection by user in Tableau

I have a date parameter which has a week split.
When I select Week 4 I need 4 bars(one for each week viz Week1, Week2, Week3, Week4) in a chart with sum of sales data.
When I select Week 3 I need 3 bars(one for each week viz Week1, Week2, Week3) in a chart with sum of sales data
and so on ...so when I select Week 1, i will have just one bar in my chart.
I tried using DATEDIFF with last() but I am not able to control the number of bars in the bar chart
Here is what works for me:
Create a parameter based on your date field, call it timeframe (or whatever), Data Type: Date, allowable values: all
Create a calculated field with the following formular
If [Date] < [Timeframe] then [Value] ELSE null END
with [Date] being your date field and [Value] being the original measure you want to display
Use the new calculated field instead of the original measure in your graph
Right click on the created parameter and choose " Show parameter controls"
Now when you change the date in the parameter controls, the amount of bar charts will adjust that only the weeks up to that date are displayed.
It should look like this:
Edit:
If you wanted an "All" option, you could go to the parameters settings, choose "List" as the "allowable Values" and add the values from your date field.
You then just create an additional item manually with the Value of "01/01/2011" or any other date far in the future) and Display as "All".
However this will only work if you have static data! If you update your source and new, more recent values were added, they will not appear in the list since you specified explicitely which values to be available.
I also realized the following:
If you do it like I explained, you will be able to choose particular days, that might lead to different graphs for different days of the same week. Ie if I choose tuesday orfriday of the same week, I will have different sized bars although the sales of the whole week stay obviously the same. If that is alright for your use case, go for it. Otherwise you should create another calculated field with DATETRUNC("week", [date]) and use that for the paramter and calculation. With that you can only choose the week, so no matter which day of a week you choose the bar will always have the same height.