How to count occurrence of value and percentage of a subset in tableau public?

本小妞迷上赌 提交于 2019-12-18 08:55:51

问题


I have a set of data in the following format:

Resp | Q1 | Q2
P1   | 4  | 5
P2   | 1  | 2
P3   | 4  | 3
P4   | 6  | 4

I'd like to show the count and % of people who gave an answer greater than 3. So in this case, the output would be:

Question | Count  | Percent
Q1       |  3     | 75%
Q2       |  2     | 50%

Any suggestions?


回答1:


Although it sounds like a fairly easy thing, it is a bit more complicated. Firstly your data is not row based so you will have to pivot it.

  1. Load your data into Tableau
  2. In the DataSource Screen choose column Q1 and Q1, right click on them and chosse "Pivot"
  3. Name the column with the answers "Answers" (just for clarity. You should get a table that looks like this:

Now you need to create a calculated field (I called it Overthreshold to check for your condition:

if [Answer] > 3 then
[Answer]
End

At this point you could substitute the 3 with a parameter in case you want to easily change that condition. You can already drop the pills as follows to get the count:

Now if you want the percentage it gets a bit more complicated, since you have to determine the count of the questions and the count of the answers > 3 which is information that is stored in two different columns.

  1. Create another Calculated field with this calculation COUNT([Overthreshold]) / AVG({fixed [Question]:count([Answer])})
  2. drop the created pill onto the "text" field or into the columns drawer and see the percentage values
  3. right click on the field and choose Default Propertiess / Number Format to have it as percentage rather than a float

To explain what the formular does: It takes the count of the answers that are over the threshold and devides it by the count of answers for each question. This is done by the fixed part of the formular which counts the rows that have the same value in the Question column. The AVG is only there because Tableau needs an aggregeation there. Since the value will be the same for every record of the question, you could also use MIN or MAX.

It feels like there should be an eassier solution but right now I cannot think of one.




回答2:


Here is a variation on @Alexander's correct answer. Some folks might find it slightly simpler, and it at least shows some of the Tableau features for calculating percentages.

  1. Starting as in Alexander's answer, revise Overtheshold into a boolean valued field, defined as Answer > 3
  2. Instead of creating a second calculated field for the percentage, drag Question, Overthreshold and SUM(Number Of Records) onto the viz as shown below.
  3. Right click on SUM(Number of Records) and choose Quick Table Calculation->Percentage of Total
  4. Double click Number of Records in the data pane on the left to add it to the sheet, which is a shortcut for bringing out the Measure Names and Measure Values meta-fields. Move Measure Names from Rows to Columns to get the view below, which also uses aliases on Measure Names to shorten the column titles.
  5. If you don't want to show the below threshold data, simply right click on the column header False and choose Hide. (You can unhide it if needed by right clicking on the Overthreshold field)
  6. Finally, to pretty it up a bit, you can move Overthreshold to the detail shelf (you can't remove it from the view though), and adjust the number formatting for the fields being displayed to get your result.

Technically, Alexander's solution uses LOD calculations to compute the percentages on the server side, while this solution uses Table calculations to compute the percentage on the client side. Both are useful, and can have different performance impacts. This just barely nicks the surface of what you can do with each approach; each has power and complexity that you need to start to understand to use in more complex situations.



来源:https://stackoverflow.com/questions/33714710/how-to-count-occurrence-of-value-and-percentage-of-a-subset-in-tableau-public

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!