I am working on creating some calculated fields in Tableau 8.2.
The data for the "test2" field is imported from Access. They can be numbers or "Null" in text.
I also have fields "test1" and "minimum" and "maximum". "test1", "minimum" and "maximum" are only numbers.
I would like to do an calculated field with an if statement.
The name of the calculated field is "answer".
I would like to do the following:
1) If "test2" is not "Null" and "test2" < "minimum" then calculate "minimum" - "test2".
(I was using the syntax IIF and != for not equal, but it did not like it because "Null" is a string value)
2) else if "test2" is not "Null" and "test1" < "minimum" then calculate "minimum" - "test1"
How would I go about doing this? Please advise.
IF (NOT ISNULL([test2])) AND [test2] < [minimum]
THEN [minimum] - [test2]
ELSEIF (NOT ISNULL([test2])) AND [test1] < [minimum]
THEN [minimum] - [test1]
END
Not so hard if you know ISNULL function
Testing for null is redundant (unnecessary) in this case.
An if condition that references a field with a null value evaluates to false. This is different than, say, Java programming. You don't have to test fields for null values before referencing them in most cases. Helpfully, aggregation functions like min(), max(), sum(), count() etc ignore null values altogether.
Unnecessary null tests make formulas hard to read and can easily hide typos. Assuming you meant the test1 instead of test2 in your second test above, then your calculated field need only state:
if test2 < minimum then
minimum - test2
elseif test1 < minimum then
minimum - test1
end
Inox is correct that when you do need to explicitly test for null values, the function to use is isnull(), or in some cases ifnull() or zn().
Related
I am trying to find the timestamp of the first occurrence of max value(the cursor point in below image)
I wrote query
min(timestamp(max(jmeter_count_total{label="GET - Company Updates - ua_users_company-updates"})))
But it's returning the max timestamp of the max value
I am not able to grab the value highlighted by cursor in below image(minimum value). Instead I am getting highest value when I use above query.
I've played with this for a bit and I think this may work (take it with a grain of salt, due to limited testing).
TLDR - the query (using only foo for brevity):
min_over_time((timestamp(foo) and (foo == scalar(max_over_time(foo[2h]))))[1h:])
This portion of the query:
foo == scalar(max_over_time(foo[2h]))
returns only values where foo matches the max value of foo in the last 2h interval. For retrieving the timestamp of those cases we use the timestamp function and use these previous clause as a conditional:
timestamp(foo) and (foo == scalar(max_over_time(foo[2h])))
Finally we only want to get the first/lowest timestamp value over the time window, which is what the outer min_over_time with the nested subquery should do.
I fiddled with the online Prometheus demo using one of the metrics present there. You can check the queries here.
i m searching sum of class of each student using countif formula, but any student have unique username like A*di (in the image) and so the calculation is false. And any other student using username like </John>, and 'Angel. and make calculation false
Formula: =COUNTIF('Data Asli'!$A:$A,$A$2)
Use SUMPRODUCT(--EXACT(..)) to run an exact, case-sensitive comparison that ignores wildcards:
=SUMPRODUCT(--EXACT('Data Asli'!$A:$A,$A2))
How it works:
EXACT(Value1, Value2) will return TRUE or FALSE, depending on whether the 2 values exactly match (same capitals, no wildcards, et cetera)
-- will convert TRUE/FALSE into 1/0
SUMPRODUCT(Array1[,Array2]) will run down the arrays, multiply the numbers together, then add them. It also forces many functions to both treat a Range as an array, and output an array.
So, as an example, the steps run like this:
=SUMPRODUCT(--EXACT(A1:A5, A2))
=SUMPRODUCT(--EXACT({Value1,Value2,Value3,Value4,Value2}, Value2))
a.k.a.
=SUMPRODUCT(--{EXACT(Value1,Value2),EXACT(Value2,Value2),EXACT(Value3,Value2),EXACT(Value4,Value2),EXACT(Value2,Value2)})
=SUMPRODUCT(--{FALSE,TRUE,FALSE,FALSE,TRUE})
=SUMPRODUCT({0,1,0,0,1})
=2
I'd like to know how to turn different variables with specific values into a new category variable to extend the specific values? I tried it with compute a variable to get a new one but it didn't work:
this is what I tried but it didn't work:
DO IF ((Q103_01=>3) AND (Q103_05=>3) AND (Q201_06=<2) AND (Q201_07=<2) AND (Q201_08=<2) AND (Q301_06=<2) AND (Q301_07=<2) AND (Q301_08=<2) AND (Q305_04=2) AND (Q305_06=2) AND (Q409_05=1) AND (Q407_07=<2) AND (Q408_04=1) AND (Q408_05=1) AND (Q411_02=<2) AND (Q203_04=<2) AND (Q203_06=<2) AND (Q203_07=<2) AND (Q203_09=<2) AND (Q203_10=<2)).
COMPUTE NewVariable = 1.
END IF.
EXECUTE.
Thanks for help in advance!
When using "Less than or equal to" or "Greater than or equal to" the sequence should be <= (or LE), >= (or GE).
The error message came from using => instead of >=.
Otherwise your syntax should work fine.
But it can be written more efficiently this way:
if cond1 and cond2 and cond3 newvar=1.
without using do if.
I have a table that looks like the following:
ID City Code
"1005AE" "Oakland" "Value1"
"1006BR" "St.Louis" "Value2"
"102AC" "Miami" "Value1"
"103AE" "Denver" "Value3"
And I want to transpose/pivot the Code examples/values into column attributes like this:
ID City Value1 Value2 Value3
"1005" "Oakland" 1 0 0
"1006" "St.Louis" 0 1 0
"1012" "Miami" 1 0 0
"1030" "Denver" 0 0 1
Note that the ID field is numeric values encoded as strings because Rapidminer had trouble importing bigint datatypes. So that is a separate issue I need to fix--but my focus here is the pivoting or transposing of the data.
I read through a few different Stackoverflow posts listed below. They suggested the Pivot or Transpose operations. I tried both of these, but for some reason I am getting either a huge table which creates City as a dummy variable as well, or just some subset of attribute columns.
How can I set the rows to be the attributes and columns the samples in rapidminer?
Rapidminer data transpose equivalent to melt in R
Any suggestions would be appreciated.
In pivoting, the group attribute parameter dictates how many rows there will be and the index attribute parameter dictates what the last part of the name of new attributes will be. The first part of the name of each new attribute is driven by any other regular attributes that are neither group nor index and the value within the cell is the value found in the original example set.
This means you have to create a new attribute with a constant value of 1; use Generate Attributes for this. Set the role of the ID attribute to be ID so that it is no longer a regular attribute; use Set Role for this. In the Pivot operator, set the group attribute to be City and the index attribute to be Code. The end result is close to what you want. The final steps are, firstly to set missing values to be 0; use Replace Missing Values for this and, secondly to rename the attributes to match what you want; use Rename for this.
You will have to join the result back to the original since the pivot operation loses the ID.
You can find a worked example here http://rapidminernotes.blogspot.co.uk/2011/05/worked-example-using-pivot-operator.html
I have two columns in a query.
I have 2 fields (Field 1 and Field 2). What I want to do is:
if Field 1 is blank then use Field 2, but
if it is not blank then I want it to populate it with Field 1.
I am sure it can be done in a computed column but have been unsuccessful with the logic (so far).
"Blank" can mean a lot of things. You can try checking for the most common ones.
In a results set, this will check a few things.
if( Field_1 == null || Field_1.replace(/\s/gi,'') == '' || Field_1 == NaN )
{
Field_2;
}
else
{
Field_1;
}
First is whether your field truly is blank, and contains no data.
Hyperion results-set computed items like "null" to be all-lowercase.
Second is whether you do have data, but it's invisible. Usually,
that's empty strings, but it can also be other whitespace, so we'll
see if replacing all whitespace (not changing the data; it's just
for comparison-sake) with empty strings would yield an empty string.
/\s/gi is regex ... it means "whitespace" global (ie: every match),
case-insensitive.
Lastly (and, I've never had this be the case personally, but I've been told it can happen), check to see if there's a Not-a-Number (NaN) response. This is sort of an error that can appear in data, counts as value, but which might not display on your screen.
If you're doing this in the Query section, you'll have to write it in SQL, and have to match the syntax to your data source (Oracle, DB2, Sybase, etc), but the concept is the same.