how to count occurrences of a value in Tableau? - tableau-api

I have a dataset with dog ids, dog names, and neighborhoods. I would like to display the n most popular dog names for each neighborhood. How can I do that?
I figured how to display counts for each name in each neighborhood, by simply dragging 'Neighborhood' and 'Animal Name' in 'Rows', and 'CNT(Animal Name)' in 'Columns'. But I don't know how to select the top 3 or 4 names for each neighborhood.

I'm going to use Tableau's Sample Superstore dataset to walk through one way you can show the top N number of Products by Category. This example will easily transfer to Dog Names by Neighborhood.
I'll start by creating a Tableau calculation we'll use as a filter later.
RANK(COUNT([Product Name]))
I'll then put the Category and Product Names dimension on the Rows shelf and the Count of Product Names on the Text marks card.
We'll then place our Production Popularity table calculation on the filter shelf.
When the dialog box appears, just click OK.
Now we need to edit how our Table calculation runs to get the Top N Product Names within each Category.
Select Specific dimension and then uncheck Category or Neighborhood in your case.
Now we need to go edit our filter.
For this example, I'll set the upper limit to 3.
Click OK and you should see the top 3 Product Names by Category.
Of course, you'll want to adjust this example to fit your data.
Hope this was helpful. Happy vizzing!

Related

Tableau - Toggle between 2 measures by single select filter on one basic text grid

looking to have a basic text grid by month by product, and then have a single filter on only 2 of the measures. For example:
Measures Filter
------------------
[ ] Sales <-- only show this
[X] Quantity <-- and this as options
[ ] Interest (don't show in filter)
[ ] Fee ( don't show in filter)
JAN | FEB | MAR | APR | MAY | etc ...
products
ITEM1 x x x x x
ITEM2 x x x x x
ITEM3 x x x x x
So the user can basically toggle between Sales or Quantity, and whichever they click on will fill out the x by month.
I was able to get this to work for everything except only showing the 2 measures I want to filter for. It only shows all measures for me in the filter and I can't figure out how to hide them.
What I did was
Drag Date to columns
Drag Product and MeasureNames to Rows
Can someone please help me on getting this correct? Any help or guidance is appreciated!
This is not the correct use case of FILTERS. Filters are normally used to filter (read subset) the data (read rows). Say for example If your dataset has 1000 rows/records and you want some report/visual/aggregation on say 500 rows (based on some criteria of course) you have to filter the data. For selection of argument (user based) in some report/viz, etc. Parameters are used.
Your desired example output is also a perfect case of use of Parameters. You may proceed as follows-
Step-1 Create a parameter (say parameter 1) with values as SALES and QUANTITY.
Step-2 Create a calculated field (say CF1) with the following calculation
CASE [parameter 1]
WHEN 'SALES` THEN [sales]
WHEN 'Quantity' then [Quantity]
END
Note: It is assumed that your field/column names in dataset are sales and Quantity
Step-3 Build your view (Crosstab here) with
months on Columns shelf
products on rows shelf
CF1 on text in Marks card (alternatively just double click it)
Choose measure for aggregation of CF1 as desired (default will be sum)
Step-4 right click paramter 1 and click show parameter. (select desired measure for aggregation in crosstab)
VOILA your desired view is complete. Good luck.
There is another options to add to Anil's good suggestion. You can also use Measure Names as a filter. Hide all measures apart from those you want in the filter, then put Measure Names on the filter shelf.
However, this means you can't use the hidden measures from the data source elsewhere, which can be a deal breaker. Duplicating the data source is a way around that limitation.
But if you only want to show the 2 measures and are happy with single select, then choosing the measure by parameter is the better option, as Anil suggests.

A filter to apply only if only one item from two other filters are selected

I have two filters for WeekNumber and Employee (name). I also have a column that shows the SUM of hours the employee has worked. Let us call it SumHours.
What I want to do is to specify the employees who have overworked, which is
if the number of weekly hours is more than 38.
If the condition applies I'd like to highlight them by adding a background color to their row.
Adding the above filter to the entire table is not a problem. What I want to do is for this filter to appear only if (1) one week and (2) one employee have been selected. Because otherwise the calculated field will highlight the employee if they have overworked in average.
How can I create a condition in my calculated field so that a condition is checked in two different places? I have found the SIZE() function but then we should also select the option Compute Using... to assign it to a specific filter. But apparently one cannot do that to two filters.
If I understand your setup correctly, I think a level-of-detail calculation might do the trick. This is pseudo-code, you will need to adjust for your dimension names. With LOD, you are getting the value of the hours worked per employee and week, regardless of which dimensions you have in your rows or columns.
if {FIXED Employee, WeekNumber : sum(HoursWorked)} > 38 then "Highlight" end
Then add this calculated field on the color marks card.
More about level-of-detail calcs can be found in Tableau's online help.

Tableau: Plot percentage of binary column

Simplified example: column of 'measure' data is binary 0's and 1's. The sum of the column (all the 1's summed) is 3948. The length of the column is 4567. 3948 is 86% of 4567. How do I create a bar chart showing this percentage. All I can get is a bar adding up to 100%.
I've tried absolutely everything in the percentage of dropdown, calculated fields, online tutorials, etc.
Two approaches:
Right click on your measure field on whichever shelf you placed it. Choose Quick Table Calc -> Percentage of Total. Experiment with different settings for "Compute Using" and read about addressing and partitioning of table calculations to learn how to specify percent of "what".
Or create a calculated field specific to your problem defined as below:
sum(int([my measure field] = 1)) / count(1)
Be sure to set the default number format for your calculated field to a percentage, by right clicking on it in the data pane (left margin) and revising the default properties
Note, the second calculation works because the INT() function converts True to 1 and False to 0, which is useful in several contexts.
You can go about this using the following method
Add 'measure' to Columns and make it a Discrete Dimension
Add SUM(Number of Records) to Rows. This should give you the bar chart you need.
This would help achieve what you are looking to do. For more customization and a better look for the graph you are looking to build in Tableau.
Add 'measure' to the colour legend and make it a Discrete Dimension.
Add SUM(Number of Records) as a Label and do a '% of Total' quick table calculation.

Counting and finding sum of combination of Dimensional attributes

Got a field in Excel:
Treament Status
IT
IT
SD
USD
SD
SD
USD
I need to use a calculated field to find a Percentage of total using this mathematical formula, based on the number of times certain dimensional attributes appears in the column i.e. Count:
% of R = [Count(SD)+Count(IT)]/[Count(Total of everything in Treatment Status)]
I know how to use the COUNT and SUM functions but I'm trying to connect the dots here.
One simple approach without writing any calculations is
Put Treatment Status on the Row shelf (double click should do it)
Number of Records on the Text Shelf (double click on it for a shortcut)
Then right click on the Number of Records field on the marks card and select Quick Table Calc -> Percent of Total
If you want to combine the SD and IT rows, select both SD and IT, then use the paperclip icon to group them together
The effect is to generate a query like select count(1) from xxx group by TreatmentStatus, and then to compute the percentages in Tableau acting upon the query results.
You need the TOTAL function. This should do it.
% of R = [Count(SD)+Count(IT)]/TOTAL(COUNT([Treatment Status]))

Does Window_SUM really sort the data in Tableau?

Does WINDOW_SUM actually sort the data? what if my table looks like this
userid | price
1 | 5
2 | 6
3 | 7
1 | 10
and say I need the output like below (just one column)
total_price_by_user
15
6
7
clearly I need a sum distinct as a calculated field so I can do the below but I wonder if window_sum actually sorts the data or not and the userid should not be displayed in the output so it should exactly look like above?
WINDOW_SUM(IIF(ATTR([userid])==ZN(LOOKUP(ATTR([userid]), -1)), 0, [price]))
You are working too hard.
Just put make sure Userid is a discrete (blue) dimension and then place it on a shelf, such as the Rows shelf. Then make sure price is a continuous (green) measure and place it on a shelf, such se the Columns shelf. Tableau will aggregate the data and display the sum of all the price values for each user id.
To change a field from dimension to measure, simply drag it to the appropriate section in the data pane on the left (i.e. the pane showing all the field names)
If you want Tableau to sort your users by sum, click the sort button in the toolbar.
Save table calculations like Window_Sum() and lookup() for the occasional special cases that the simpler approaches don't cover.