I am relatively new to Tableau and I am wondering if there is a way to calculate null values in a column.
I have a column called Email of type string and want to know how many people have not entered their email i.e. Null.
I tried to create a calculated field with
count(ISNULL([Email]))
But this gives me the total count and not the count of null.
Thanks.
You cannot count NULL since COUNT ignores NULLs.
You can do this, though:
SUM(IF ISNULL([Email]) THEN 1 ELSE 0 END)
Per your additional comment, if you wanted to count where two fields are both NULL then:
SUM(IF ISNULL([Email]) AND ISNULL([Phone]) THEN 1 ELSE 0 END)
You can continue this for any number of fields, as needed.
You can use a filter for that field. Set it to only show you Null values, and set the measure to Count instead of the default Sum.
You can create another column where all null values are set to 1 and anything else to 0, >then count that. It should be something like
>COUNT(IF "null" THEN 1 ELSE 0)
A working syntax for that would had been:
SUM(IF ([Your field] = null) THEN 1
ELSE 0
END)
You can create another column where all null values are set to 1 and anything else to 0, then count that. It should be something like
COUNT(IF "null" THEN 1 ELSE 0)
Related
i have created a query that returns users who placed order in a specific month and after how many months they have returned. the problem is when there's no user placed any order in a month it returns blank . I want to return zero instead .
I created a case statement as :
case when user_id is null then 0 else user_id end
I also tried:
case when user_id is null and delivery_month is null and returned_month is null then 0 else user_id end
if there's no row evaluated for the user, how can i replace that with 0 ?
this is how blank values looks like when I loaded data in the visualization tool:
Edited to provide sample data
I am trying to create a calculated column that sums 2 other columns, which should be easy. However, some values in both of the columns are null, so I want to use a case expression to replace null values in both columns with 0s and then add up the resulting values. The other complicating factor is that the second column contains text values with commas that need to be converted to numerical before I can add them. What I am currently trying to do is:
SELECT (case when pm."PS" is null then 0 else pm."PS" end) + (case when pm."PS-PREV1" is null then 0 else replace(pm."PS-PREV1", ',', '')::numeric end) AS "Sales"
FROM pm
Sample data:
PS
PS-PREV1
20000
null
30000
20,000
null
null
null
30,000
Desired output:
output
20000
50000
0
30000
This is just returning the value of the 1st column without adding in the second column. Where am I going wrong? Am I overthinking this?
your code should work , however you can write it a little bit more clean:
SELECT COALESCE(pm."PS",0)
+ COALESCE(replace(pm."PS-PREV1", ',', '')::numeric,0) AS "Sales"
FROM pm
for example is this query possible?
CREATE TABLE products (
name text,
price numeric not null,
CHECK (IF price > 0 THEN name IS NOT NULL ELSE name IS NULL END IF)
);
UPDATE:
seems like no
here https://rextester.com/l/postgresql_online_compiler
it throws error
Error(s), warning(s):
42601: syntax error at or near "price"
looking at documentation https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-EXCLUDE it says
Currently, CHECK expressions cannot contain subqueries nor refer to variables other than columns of the current row. The system column tableoid may be referenced, but not any other system column.
but IF is not subquery, dont understand why it doesnt work
UPDATE 2:
the
CREATE TABLE products (
name text,
price numeric not null,
CHECK ((price > 0 AND name IS NOT NULL) OR (price <= 0 AND name IS NULL))
);
works, but it makes tedious to write complex queries this way
IF is not a subquery, and it is not anything else either, in SQL. So it is assumed to be a column name. Having two (assumed) column names immediately in a row is a syntax error, and is assigned to the second column name.
SQL has CASE, not IF. You need to use the language you are using, not just make up things you hope to work.
CREATE TABLE products (
name text,
price numeric not null,
CHECK (case when price > 0 THEN name IS NOT NULL ELSE name IS NULL END)
);
I'm not 100% on what you're asking, but I think you're saying:
If the price > 0 then name CANNOT be NULL
In which case this should do it:
CHECK (price > 0 AND name IS NOT NULL)
If the name can be NULL on price being 0 then use this:
CHECK ((price > 0 AND name IS NOT NULL) OR (price = 0 AND name IS NULL))
You don't need to specify IF in the CHECK condition, it should essentially contain the actual statement to be tested rather than an IF statement.
Msg 8114, Level 16, State 5, Line 1 Error converting data type varchar to numeric.
Below is the code that I am using and the schema for the fields mentioned in the code. Some context behind this, I have a set list of indcodes. I only give one below but there are 350 in total. It is quite likely when i query by indcode, area, and ownership that the data does not exist. Hence, why I have the isnull language. Finally, I need rows that contains a value of suppress = 1 to show up as N/A.
For every indcode that I query, I need a result. The result could be actual data or some text (NULL, N/A, etc).
Last is the intended result. I only gave code for one indcode but have included six to give the example some depth.
select
CASE
WHEN suppress = 0 then isnull (mnth1emp, 0)
ELSE 'N/A'
END as mnth1emp,
CASE
WHEN suppress = 0 THEN isnull (mnth2emp, 0)
ELSE 'N/A'
END as mnth2emp,
CASE
WHEN suppress = 0 THEN isnull (mnth3emp, 0)
ELSE 'N/A'
END as mnth3emp
from dbo.industryimport20172f
where area='000003' and indcode='21' and ownership='00'
suppress char (1)
mnth1emp numeric(9,0)
mnth2emp numeric(9,0)
mnth3emp numeric(9,0)
area char(6)
indcode char (6)
ownership char(2)
123 456 789
1 2 3
Null null null
2 3 4
3 4 5
Null Null Null
So the first things that jump out here are:
a) While you're asking for numbers to be returned in the case statement, you're then asking for a string when you get to ELSE. Try casting all the fields as varchars and then entering them as strings:
CASE
WHEN cast(suppress as varchar) = '0' then isnull (cast(mnth1emp as varchar), '0')
ELSE 'N/A'
END as mnth1e
You may not need to cast suppress.
b) Does supress ever have anything other than a number in it? If so, you would need to put the zero as a string:
CASE WHEN supress = '0' --etc.
My goal here is to count the # of True, False and NULL per day using my boolean calculation. Next step would be to calculate the percentage of each T, F, N with overall day.
Below is the visualization I would like to achieve but instead of blue dots, I would need the count of them in number.
Initially, I tried
IF [boolean] THEN 1 ELSE 0 END
But only changing from True to 1, False to 0 and missing the NULL - not counting:
I also tried different Calculated Fields to get the count of it but always getting the same error:
SUM(IF [Field]=TRUE then 1 else 0 end)
COUNT(IF [Field]=TRUE then [ID] end)
ERROR:
Could someone please assist me with a Calculate Field or any other solution where I could get a count of T, F, and NULL that would also assist me with the percentage?
Thank you
You're working too hard.
Just put SUM(Number of Records) on one shelf with DAY(Disconnected ...) on another, with the Boolean field that classifies data rows as > 0 or not on some other shelf, such as Color. No need for a table calc.
Here is an image of a solution posted at https://community.tableau.com/message/601862#601862