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?
Take SUMIF and COUNTIF as an example. Tableau has an IF function, a SUM function and a COUNT function, but not SUMIF or COUNTIF.
Count Distinct – whose syntax is COUNTD (expression) - this function returns the number of distinct items in a group. Each unique value is only counted once. Note, NULL values are not counted.
COUNTD() – This function will always return the number of UNIQUE values in the selected field. This means that if you have a field with two values 0 and 1 in a table with 100 rows, this function will return the value 2, unlike COUNT () where the result would be 100.
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) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With