Summing 2 Statements Tableau - tableau-api

In Tableau, what are the rules when it comes to adding 2 statements if you cannot put everything in 1? In the first part of the statement below, I am trying to pull all products besides 1 of them from a distributor, then in the part I am trying to pull all the units except from 2 of the distributors. However, as you can guess, the cells are blank when I drag the pill over.
Is it best practice to just create 2 calculated fields and add THOSE together?
(IF [Distributor] = "NDC"
AND [Product] <> "PE Single Use"
THEN ['15] END)
+
(IF [Distributor] <> "NDC"
AND [Distributor] <> "M&D"
THEN ['15] END)

Here are a few simple rules about calculations in Tableau (and generally in SQL also)
If you don't specify a value in some case, say by leaving off the else clause in an if statement, the expression evaluates to null in that case. That can be fine or a problem depending on what you want. Consider null to mean no-value, or not-applicable or missing-value depending on your situation.
Nulls propagate through other expressions. A null value + anything yields a null value. You can explicitly test for null if necessary, but it is often better to keep simple expressions that evaluate to null when any part is null.
Aggregation functions like SUM(), MIN(), AVG() etc silently ignore null values. So AVG(Salary) is really the average of all the rows that had a non-null value in the Salary field. You don't get any warning about this, it is understood. If you want to know how many rows have a non-null value for a field, you can use the COUNT() function to check.
In your expression above, the two IF conditions are never both true for the same record. So at least one of the two halves of your expression is null in each case, so the resulting entire expression is always null.
There are other rules to learn when using table calcs and LOD calcs, but these rules apply throughout.

Related

AND/OR Statement in Tableau

Does anyone know how to transcribe this in Tableau:
WHERE DATE(created) = '2021-01-01'
AND (source = 'T' OR (a.promo = 'TK' AND source != 'T'))
Basically, I'm validating if source equals to "T" and grabbing all those results, but sometimes a promo but does not get flagged under the "T" source.
Is there a way to have filter that validates under this nested WHERE clause?
Thanks in advance!
If you're wanting to use a calculated field in the place of SQL then:
IF DATE([created]) = '2021-01-01'
AND ([source] = 'T' OR ([promo] = 'TK' AND [source] <> 'T'))
THEN [measure or dimension] END
That's assuming that the fields in Tableau end up being given those names.
EDIT
The calculated field above works by just ignoring data that doesn’t meet the conditions, which is very flexible — effectively pulling the condition from the WHERE clause into the SELECT clause of the query that Tableau generates.
In some other cases, you really do want to first filter to a set of data and then calculate on the resulting rows — either for performance or logic reasons.
In that case, you can define a boolean calculated field with just the condition, put it on the Filter shelf, and choose to filter to records whether the calculated field evaluates to True.
Both approaches are useful.

Tableau isNull then 0 calculated field

I have my tableau workbook and I'm currently counting by a field called ID - COUNT([Id]) - while this is great, on days with no activity my dashboard doesn't show ANYTHING and I want it to show zero if there was no activity - so I do I change this to count but also replace null with 0 (zero)?
First make sure you understand what Count([ID]) does. It returns the number records in the data source that have a non-null value in the column [ID].
Count() never evaluates to null. But if you have no rows at all in your data after filtering, then you'll get an empty result set -- i.e. view data will not have any summary data to show at all - whether null or zero.
Wrapping COUNT() in a call to ISNULL() or ZN() won't help in that case.
The solution is to make sure you have at least one data row per day, even if all other fields besides the date are null. Aggregation functions ignore nulls so padding your data like this should not disturb your results. The simplest way is to make a calendar table that has one row per day with nulls in most columns. Then use a Union to combine the calendar with your original data source. Then Count(ID) will return zero on days where there are no other records besides the calendar entry.
You can also get similar results using data blending, although with a bit more complexity.

NULL value shown on filter regardless

I have created a calculated field to only show week numbers for the latest four weeks from today. I called it latest_4_weeks with the following definition:
if (([week_number] <= datepart('week',today())) and ([week_number] > datepart('week',today()) -4)) then [week_number]
end
When I add this field to my columns an unwanted NULL value shows up. I still haven't been able to tell the Tableau to ignore all the null values using IFNULL(), etc.
And when I manually filter out the NULL value, the filter no longer works as it should. Since it will now apply the current values and all other values that relate to next week will be ignored.
How can I effectively remove NULL in my calculated field?
The reason the NULL's are being created is because your don't have a else block that assigns value when the condition is not Satisfied. So add a else block
e.g IF(COND) THEN A ELSE B END.
Or. You can also exclude NULL if you dont want to assign an else block in the same filter block by checking the Exclude option on bottom right corner.

Accessing current row value with lag function

I want to calculate the difference between the previous and the current column and make it a new column named increase. For this, I'm using the lag window function. The value of the first column is not defined since no previous column exists. I know that a 3rd parameter specifies the default value. However, it depends. For the first row, I want to use the value of another column e.g. the one of count from that current row. This assumes that 0 is increased to count for the first row which is what I need. Specifying the column name as 3rd argument for the lag function does not work correctly and neither does using 0. How can it be done? I'm getting strange results such as quite a random result or even negative numbers.
SELECT *, mycount - lag(mycount, 1) OVER (ORDER BY id, messtime ASC) AS increase FROM measurements;
Window functions cannot be nested either:
ERROR: window function calls cannot be nested
There is another issue with your query: So far your results are in random order, so you may think you are seeing problems that don't exist.
Add ORDER BY id, messtime to your query to see the rows in order. Now you can compare one row with its predecessor directly. Are there still issues? If so, which exactly?
SELECT *, "count" - lag("count", 1) OVER (ORDER BY id, messtime) AS increase
FROM measurements
ORDER BY id, messtime;
COUNT is a reserved word in SQL. It seems the DBMS thinks you want to nest COUNT and LAG somehow.
Use another column name or use quotes for the column:
SELECT *, "count" - lag("count", 1) OVER

Converting decimal to char Reporting Service 2005

I'm developing a Report Server on SSRS 2005. In this report there are some clause contracts. Just one of this clauses isn't constant, so sometimes it appears and sometimes doesn't. It will appear if a column value on one of my datasets isn't null,
Every clause must be preceeded by a char and a Parentheses. For example, the first clause must be preceeded by a), the second by b), the third by c)...
The problem is, that clause that can be shown or not, if shown, must be the clause a and the other constant clauses must follow it. But if the variable clause doesn't appear, the first constant clause must be the a clause.
I tought the following. On the sql code of my dataset I made a case when test to check the column that indicates if the clause will appear or not, if it's null, the clause will not appear and I return on the select of my dataset the integer value 96, if it is not null, the clause will appear and I return on the select of my dataset the integer value 97.
I want to convert this integer value to a char. So 97 will become an a.
I'm using the report server element TextBox to receive this integer value, so I want to do somenthing like this.
=Convert.ToChar(First(Fields!FIRSTLETTER.Value, "DataSetAditionalInformation"))
I use Convert.ToChar(value) in C#, and I want to know the equivalent command to use on my Report Server.
On the first constant clause on my report I want to do the following:
=Convert.ToChar(First(Fields!FIRSTLETTER.Value + 1, "DataSetAditionalInformation"))
And for each constant clause, I will increase by one the number after the plus signal.
So, if the variable clause appears, the FIRSTLETTER will be 97, and the second clause will be 98, after converting these number will become the letters a and b. If the variable clause doesn't appear, the FIRSTLETTER will be 96, and the first and constant clause will be 97, and after converting: a.
I don't even know if this is possible, so any idea will be appreciated to solve this problem.
Thanks in advance.
UPDATE
A example of how the contract clauses will work:
I have this select:
SELECT CASE WHEN ADDITIONAL_CLAUSE IS NULL THEN 96 ELSE 97 END AS FIRST_LETTER,
ADDITIONAL_CLAUSE
FROM TBLADDITIONALDATA
WHERE (NUMBER_CONTRACT = #NUMBER_CONTRACT)
If the select returns 96 (and this means that there isn't an additional clause), the clauses will be like below:
a) The amount of monthly contemplations will be defined in accord to the disponibility of the credit of the group on the respective month of the Meeting Group realization.
b) The total amount of plots are not constants, because them are calculated in accord with the variation of the object price
If the select returns 97 (and this means that there is an additional clause), the clauses will be like below:
a) The text of this clause isn't constant, it depends of the value stored in the column ADDITIONAL_CLAUSE
b) The amount of monthly contemplations will be defined in accord to the disponibility of the credit of the group on the respective month of the Meeting Group realization.
c) The total amount of plots are not constants, because them are calculated in accord with the variation of the object price
I have a lot of other constant clauses, but I guess they are not necessary to exemplify.
So I need to make the letters that preeceed the clauses be not constant, so every letter will be a text box.
The textbox of the additional clause will just appear if the ADDITIONAL_CLAUSE isn't null, this I already did on the Visibility Expression of the textbox. And this textbox receives the value of FIRST_LETTER.
But the other textboxes of every constant clauses will receive the value of FIRST_LETTER plus 1 for the first constant clause, plus 2 for the second constant clause, plus 3 for the third constant clause and so on. But the value of FIRST_LETTER is an integer, so I need to convert it to char.
Thanks again.