问题
I have a field such that
Color
Pink
Blue
Pink
Blue
Pink
Pink
Pink
Pink
Pink
Blue
Blue
Pink
Pink
Pink
Blue
Pink
Blue
Pink
Blue
How to create calculated field to count number of times Pink appears?
Total numbers of Blue?
回答1:
If you're grouping by Color and only Color in the view, then you can simply use SUM(Number of Records)
. For example, put Color in the Rows shelf and SUM(Number of Records)
into Text, and you'll get a table with the counts for each Color.
If you need to specify the level of detail, Tableau has LOD (level-of-detail) expressions that will make this easy.
{ FIXED [Color] : SUM([Number of Records]) }
That statement says to calculate the SUM()
of [Number of Records]
and to group by [Color]
, regardless of the level of detail in the view.
FYI, [Number of Records]
is a calculated field that Tableau automatically tosses into every data source. Here's the code:
1
This simply places a 1 in every row of your partition. If you SUM()
those ones, you'll get the number of records. So your LOD calculation could have just as easily been:
{ FIXED [Color] : SUM(1) }
来源:https://stackoverflow.com/questions/32725936/tableau-count-number-of-times-same-value-appears-in-column