How to divide values from the same dimension in Tableau? - tableau-api

I am trying to divide values from the same pill in Tableau. Per my screenshot, the pill is named "Animals" & the grouped values within that pill are dog, cat, hamster & horse.
How can I divide dog by cat? How can I divide hamster by horse? How I divide cat by hamster? etc.
How would I create a calculation to show the desired values?

Without the option of pivoting rows to columns available in Tableau Desktop, you have to manually create each of the desired ratio calculated field.
I have created a sample data by sampling 200 rows in a Programming language
Your created view is like this
Now create a calculated field for each of the desired ratio. say cat/dog like this
ZN(SUM(IF [Animals] = "cat" then 1 else 0 END))/
ZN(SUM(IF [Animals] = "dog" then 1 else 0 END))
Better use this field as discreet
Add this field to view you'll get desired ratios

Related

Percentage of total changes on applying filter

I have a data table with three dimensions and one measure. For each row, I am trying to calculate the percentage of total (calculated by taking the sum of rows) using a calculated field.
As seen in the screenshot attached, For the column titles 'Dec-19' I want the values to be a percentage of current value / grand total (calculated at the bottom as 122,187)
Screenshot of DataTable:
So e.g. for the Column B value of 2000, the Dec-19 column should be (97/122,187) * 100 = 0.079.
I have achieved this by creating a calculated field with the formula: SUM (sales) / MAX ({EXCLUDE (Column B): Sum (sales}), where sales is the measure used in the datatable.
However, upon application of filter on column B, the percentage value changes. e.g. if I select the value 2000 in my filter for column B, I get the percentage as 100%. It seems as if the percentage is being calculated based on only the rows in the filter.
Since you haven't included any sample data, I created some sample data like this, hope this resemble yours.
Thereafter, I built a cross-tab view in tableau, somewhat like yours
If that is the scenario, use a calculated field, say CF like this, instead of yours
([Sales])/
{FIXED [Col1], DATETRUNC('month', [Col3]) : sum([Sales])}
Dragging it in the view
and thus, filtering won't affect your calculation

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.

Tableau calculate percentage from a value in the table

I'm trying to calculate the percentage in Tableau from a value in a table.
For example. I have a table that contains the following
Name Value
a 10
b 20
c 30
Ref 100
How can I have the percentages calculated based on the Ref value like for example a/Ref
Name Value Percentage (x/Ref)
a 10 10%
b 20 20%
c 30 30%
Thanks
You can either
use tableaus built in functionality:
Right click the measure pill and select "quick table calculation -> Percentage of total"
2.Use calculated fields.
Create a calculated field(Analysis->Create Calculated Field) called reference. As the value enter the integer you want.
Create a second calculated field call it percentage. Here enter the mathematical formula to get a percentage using the reference calculated field as your total.
So something like:
[Value] / [refrence] *100
You don't need the reference calc field, you could just hard code the value into the calculation.
Drag the percentage calculated field into the row shelf.

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.

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.