NULL value shown on filter regardless - tableau-api

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.

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.

Group data based on different size

I am trying to make a report in which I need to show data based on Width groups. Below is an example of data & the required output. I'm unable to make a group which can give this required output. If someone can help please.
The easiest way to accomplish your grouping needs for this data set would be to create a new Formula Field that evaluates the values of the Width data field for each record to determine which group the record belongs within, then do the grouping on this new formula field.
You formula field will look like this.
Select {WIDTH}
Case 400 to 600 :
"G1"
Case 601 to 849:
"G2"
Case 850 to 1049:
"G3"
Default :
"Default text or error message text goes here"
You will likely need to adjust the integer values I'm used in the Case statements to evaluate the WIDTH field. The text that goes into the Default case is up to you. In fact, if it works logically with your needs, you could eliminate the Default case entirely, as it is not required. However, it is good practice to ensure the Switch statement always returns a value, even if that value is text to indicate that something unexpected occurred. This allows your users to easily recognize a bit of a data that may be out of range for the grouping of the report so the report can be modified or the data can be corrected, whichever is the most appropriate action.
The other 3 columns in your required output appear to just be counting the number of records within each group that have a diameter within a range. To get this output you can use Running Total Fields with a Type of summary of Count and then use the range of values in the Evaluate section. The Reset section will be set to On change of group evaluating the group created by the formula field above. You will want to put a sort order on the diameter field though.

Repeating a result on a line on the rows/lines below untill that result changes?

Image showing problem
I want to know if there is a way of repeating the result of a column downwards until the change of result.
Attached image shows the problem.
I need 9.1stWC column to fill the number(which is actually a string) all the way down until that number changes.
The column is not null where the blank fields are (9.is null, shows this) so I'm going to have to write additional formulas to show I'm guessing.
I've used previous function but that only shows for the next line down then is blank after.
The replicate string function will go across the column not downwards.
I've looked at a stringVar but cant find a way of making this work on this report.
I'm reserved about grouping as I have a lot of other filters and by grouping I could possibly loose the sequence depending on what I group by.
My aim is to show WC which are equal to the current op line by doing a simple formula after I have managed to populate that column
I will assume your column is a field named {Result.WC}.
Try this:
Create a formula. Let's call it #LastWC.
Use this code in the formula:
shared stringvar sharedLastWC;
if not isnull({Result.WC}) and {Result.WC} <> "" then sharedLastWC := {Result.WC};
sharedLastWC; //returns
Replace the field by the formula.
The idea behind this is to set the shared variable when the field is not blank and not null. So, the variable will hold the last "filled" value.
I can't make a test with the purposed solution right now, but give it a try and make adjusts if you need. Tell me if it does not work.
If the first row can be empty, you may need to create an aditional formula do start the value of the shared variable in the header of the report.

Summing 2 Statements Tableau

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.

Setting a condition so that no results are displayed if the condition is not met

I'm trying to have a chart be blank rather than showing results if the size of the data that populates the chart is too small. I have a variable ID in which each case has a unique value. In a case where I don't want any data to show if there are less than 10 cases, I figured I'd need to set up a calculated field that would do something like the following:
IF COUNTD([ID]) < 10 THEN ....
But I can't figure out what command to call so that it shows no data. Would anyone have a pointer?
You can create a calculated filed with calculation "COUNTD([ID])<10" .It would be a Boolean filter.Now drag and drop the calculation to the filter shelf and set it to either "TRUE" or "Exclude False".