简体   繁体   中英

Tableau / SQL Filling In Missing Data

So, I have a bit of a strange thing - it's come up before, but my tool of choice in the past has been either R or Python and I can usually take care of this with a bit of code magic, but being new to Tableau, I'm not sure how to handle this....

I have a SQL statement as a data source that joins three tables. Let's call the first one the "Customer" table and the 2nd one the "Questions" table. The Customer table contains basic information, including the customer ID number. The "Questions" table is formatted in a way that makes it a little difficult to deal with. It contains the customer number, of course which is used as the key to join the two. It also contains two other columns - ATTRIBUTE_NM (the questions name) and ATTRIBUTE_VALUE_TXT (the answer to said question). My problem is there are several questions- 9 to be exact. It looks like this:

CustID    ATTRIBUTE_NM    ATTRIBUTE_VALUE_TXT
000001    Question 1      NULL
000001    Question 2      Blah Blah
....      .....           .....
000001    Question 9      Declined to Answer  

Ok, so there are some potential combinations here. Either a customer can have all questions answered, in which case, they all appear for that customer. Some cases a customer can answer some questions, but not all, in which case, some questions are "NULL" and others are answers. And finally, we have customer that were in the database before the questions were a thing, so they do not appear in that Questions table at all.

I know all of the questions that are possible. My goal is to use Tableau to create a dashboard that looks like this:

CustomerID    Question_1    Question_2    Question_3    ....
00001         Answered      Not Answered  Answered

This is an internal request for a team that is working to identify customers, which questions they answered and which ones they didn't (if the customer doesn't exist in the question table, we assume they didn't answer them).

So a few things here - I need to figure out how to turn the ATTRIBUTE_NM from data in a column/field into columns themselves. This isn't that hardd - I can create a dimension with each question name and some logic to look at ATTRIBUTE_NM to see if that answer exists or is NULL. My issue is when the questions for that customer do not exist at all. How do I check for a "DOESN NOT EXIST" or something of the like?

In the past I have done this with dates, for example in R, but what I had to do is create a reference table that had all the date combinations and merge it with the read data to fill in the gaps. I think that is a possibility here, but I've never done that with Tableau or SQL. I'm wondering if, during the join, I can somehow fill those gaps in, since I'm doing a LEFT JOIN between the Customer Table and the Questions... Thank you all so much, in advance, for any help you can give me!

Maybe try something like this:

SELECT
  CUSTOMERID,
  CASE WHEN QUESTION_1 IN (NULL, 'DECLINED TO ANSWER') THEN 'NOT ANSWERED' ELSE 'ANSWERED' END AS QUESTION_1,
  CASE WHEN QUESTION_2 IN (NULL, 'DECLINED TO ANSWER') THEN 'NOT ANSWERED' ELSE 'ANSWERED' END AS QUESTION_2,
  CASE WHEN QUESTION_3 IN (NULL, 'DECLINED TO ANSWER') THEN 'NOT ANSWERED' ELSE 'ANSWERED' END AS QUESTION_3,
  CASE WHEN QUESTION_4 IN (NULL, 'DECLINED TO ANSWER') THEN 'NOT ANSWERED' ELSE 'ANSWERED' END AS QUESTION_4,
  CASE WHEN QUESTION_5 IN (NULL, 'DECLINED TO ANSWER') THEN 'NOT ANSWERED' ELSE 'ANSWERED' END AS QUESTION_5,
  CASE WHEN QUESTION_6 IN (NULL, 'DECLINED TO ANSWER') THEN 'NOT ANSWERED' ELSE 'ANSWERED' END AS QUESTION_6,
  CASE WHEN QUESTION_7 IN (NULL, 'DECLINED TO ANSWER') THEN 'NOT ANSWERED' ELSE 'ANSWERED' END AS QUESTION_7,
  CASE WHEN QUESTION_8 IN (NULL, 'DECLINED TO ANSWER') THEN 'NOT ANSWERED' ELSE 'ANSWERED' END AS QUESTION_8,
  CASE WHEN QUESTION_9 IN (NULL, 'DECLINED TO ANSWER') THEN 'NOT ANSWERED' ELSE 'ANSWERED' END AS QUESTION_9
  FROM(
  SELECT
  CUSTID as CUSTOMERID,
  (CASE WHEN ATTRIBUTE_NM = 'Question 1' THEN ATTRIBUTE_VALUE_TXT ELSE '' END) AS QUESTION_1,
  (CASE WHEN ATTRIBUTE_NM = 'Question 2' THEN ATTRIBUTE_VALUE_TXT ELSE '' END) AS QUESTION_2,
  (CASE WHEN ATTRIBUTE_NM = 'Question 3' THEN ATTRIBUTE_VALUE_TXT ELSE '' END) AS QUESTION_3,
  (CASE WHEN ATTRIBUTE_NM = 'Question 4' THEN ATTRIBUTE_VALUE_TXT ELSE '' END) AS QUESTION_4,
  (CASE WHEN ATTRIBUTE_NM = 'Question 5' THEN ATTRIBUTE_VALUE_TXT ELSE '' END) AS QUESTION_5,
  (CASE WHEN ATTRIBUTE_NM = 'Question 6' THEN ATTRIBUTE_VALUE_TXT ELSE '' END) AS QUESTION_6,
  (CASE WHEN ATTRIBUTE_NM = 'Question 7' THEN ATTRIBUTE_VALUE_TXT ELSE '' END) AS QUESTION_7,
  (CASE WHEN ATTRIBUTE_NM = 'Question 8' THEN ATTRIBUTE_VALUE_TXT ELSE '' END) AS QUESTION_8,
  (CASE WHEN ATTRIBUTE_NM = 'Question 9' THEN ATTRIBUTE_VALUE_TXT ELSE '' END) AS QUESTION_9,
  FROM Questions
  )

I tried the Union and I think there is a way to do it that way but you are right, it wasn't working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM