Create calculated fields in crosstab - tableau-api

I have a crosstab between supplier and order acceptance status, containing maxvalue of a number.
I need to create a formula like :
IF ACCEPTED > MISSING THEN "GOOD" ELSE "BAD"
Can you help with the syntax?
This is what I get using formula suggested and this is what I should get
ORDER | ACCEP | MISSING | SHOULDBE | NOW |
-------------------------------------------------------------------------------
61010 | 6 | 0 | GOOD | GOOD | FORMULAOK
61011 | 3 | 12 | BAD | BAD | FORMULAOK
63239 | 9 | 11 | BAD | BAD | FORMULAOK
66749 | 0 | | BAD | GOOD | FORMULAnotOK
76824 | 2 | 1 | GOOD | BAD | FORMULAnotOK

Use a SUM() and INT() function to do Conditional Checks ,convert Boolean as Numbers and add the numbers to get total counts. Then its just a comparison.
IF SUM(INT([ACCEPTANCESTATUS]="ACCEPTED")) > SUM(INT([ACCEPTANCESTATUS]="MISSING"))
THEN "GOOD" ELSE "BAD" END

You're data is already pivoted, you can just write a calculated field exactly has you framed your pseudo code:
IF SUM([ACCEP]) > SUM([MISSING]=)
THEN "GOOD"
ELSE "BAD"
END
You can remove the sum elements if you want the value calculated for every row (and not aggregated).

Related

Tableau - Show multiple discrete string (dropdown) dimensions side-by-side in a single table

I have a list of survey results that looks similar to the following:
| Email | Question 1 | Question 2 |
| ----------------- | ---------- | ---------- |
| test#example.com | Always | Sometimes |
| test2#example.com | Always | Always |
| test3#example.com | Sometimes | Never |
Question 1 and Question 2 (and a few others) have the same discrete set of values (from a dropdown list on the survey).
I want to show the data in the following format in Tableau (a table is fine, but a heatmap or highlight table would be best):
| | Always | Sometimes | Never |
| ---------- | ------ | --------- | ----- |
| Question 1 | 2 | 1 | 0 |
| Question 2 | 1 | 1 | 1 |
How can I achieve this? I've tried various combinations of rows and columns and I just can't seem to get close to this layout. Do I need to use a calculated value?
As far as I know - it is not natively possible with Tableau, because what you have is kind of a pivot table.
What you can do is unpivot the whole table as explained here https://stackoverflow.com/a/20543651/5130012, then you can load the data into Tableau and create the table you want.
I did some dummy data and tried it.
That's my "unpivoted" table:
Row,Column,Value
test,q1,always
test,q2,sometimes
test1,q1,sometimes
test1,q2,never
test10,q1,always
test10,q2,always
test11,q1,sometimes
test11,q2,never
And that's how it looks in Tableau:

PostgreSQL simple count query

Trying to scale this down so the answer is simple. I can probably extrapolate the answers here to apply to a bigger data set.
Given the following table:
+------+-----+
| name | age |
+------+-----+
| a | 5 |
| b | 7 |
| c | 8 |
| d | 8 |
| e | 10 |
+------+-----+
I want to make a table that shows the count of people where their age is equal to or greater than x. For instance, the table about would produce:
+--------------+-------+
| at least age | count |
+--------------+-------+
| 5 | 5 |
| 6 | 4 |
| 7 | 4 |
| 8 | 3 |
| 9 | 1 |
| 10 | 1 |
+--------------+-------+
Is there a single query that can accomplish this task? Obviously, it is easy to write a simple function for it, but I'm hoping to be able to do this quickly with one query.
Thanks!
Yes, what you're looking for is a window function.
with cte_age_count as (
select age,
count(*) c_star
from people
group by age)
select age,
sum(c_star) over (order by age
range between unbounded preceding
and current row)
from cte_age_count
Not syntax checked ... let me know if it works!

Crosstabs Crystal Reports Null value vs zero

I'm creating a crosstab report showing the survey history for gopher tortoises (if you must know what that is) monitoring stations. Not all stations are monitored for a given survey and sometimes when we monitor we don't find any and thus record a 0 which is a valid result.
In the crosstab when the station isn't used I would like it to say "N/A" or some other equivalent, but when it's a zero I want it to stay as zero.
I've found so much on how to change a null to a zero, but nothing when you want to keep the zero and somehow note the null.
Below is what the crosstab should look like. You'll see that the 0 in Station4 on 1/1/2004 is "real" (meaning we didn't find any) but all of the N/A's are when we didn't use the station.
Survey Dates
| | 1/1/2000 | 1/1/2002 | 1/1/2004 | 1/1/2006 |
|----------|----------|----------|----------|----------|
| Station1 | 9 | 5 | N/A | N/A |
| Station2 | 5 | 7 | 2 | 6 |
| Station3 | N/A | N/A | 6 | 9 |
| Station4 | 10 | 9 | 0 | 11 |
This is what the Oracle table look like for the 1/1/2000 survey as an example
| SurveyID | StationID | Number |
|----------|-----------|--------|
| 1 | 1 | 9 |
| 1 | 2 | 5 |
| 1 | 4 | 6 |
So, basically how to I keep the zero's and put some text in the nulls in a CR crosstab?
Thanks!
Because CR doesn't differentiate between nulls and actual zeros in the crosstab, you can try replacing actual zero values with a placeholder so you can tell the difference. Note that this solution will only work if you are trying to display the values and not do any aggregate calculations.
First, create a formula that will replace the zeros with a placeholder value. In this case, I'm using -1 since that number should never appear in the database.
//{#Survey Num}
local numbervar totalSurvey;
totalSurvey:={Table.ActiveBurrows} + {Table.InactiveBurrows};
if totalSurvey=0 then -1 else totalSurvey
Use this formula to create your crosstab. Now you need to set a display string so that everything appears correctly. Right-click one of your crosstab cells → hit "Format Field" → select the "Common" tab → then create a "Display String" formula. That formula should be something like:
if currentfieldvalue=-1 then "0" else if currentfieldvalue=0 then "N/A" else totext(currentfieldvalue,0,'')
Now you're basically just printing the real values over top of the placeholders.

Sphinx calculation issue with large numbers

I have connected mysql client with sphinx server
when I issue this query
select 20130919.0+(15/4),15/4 from [INDEX] limit 1;
I get the following result
+------+--------+-------------------+----------+
| id | weight | 20130919.0+(15/4) | 15/4 |
+------+--------+-------------------+----------+
| 7414 | 1 | 20130924.000000 | 3.750000 |
+------+--------+-------------------+----------+
Note that 15/4 returns 3.75 but when it is added to 20130919.0 it returns wrong result.
in another case when i write the following query
select 2222+15/4,15/4 from [INDEX] limit 1;
It returns correct result.
+------+--------+-------------+----------+
| id | weight | 2222+15/4 | 15/4 |
+------+--------+-------------+----------+
| 7414 | 1 | 2225.750000 | 3.750000 |
+------+--------+-------------+----------+
similarly in the previous case third column should have the value 20130922.75. I thought the problem was that sphinx return rounded off number but in that case it should have been 20130923.000 not 20130924.000.
What I want is that it should return a correct floating point number but it is acting strangely. Hope someone here has any explanation for this behaviour.
Sphinx mostly does single precision float maths
http://en.wikipedia.org/wiki/Single-precision_floating-point_format
which only uses 8 bits for the exponent. The amount of decimal digits that can be stored precisely is approximately 7 - you have 8.
There is a double() function, but I havent tested it.
Edit: Actully no, double() wont help.
sphinxQL>select double(20130919.0)+(15/4),15/4 from sample2 limit 1;
+---------------------------+----------+
| double(20130919.0)+(15/4) | 15/4 |
+---------------------------+----------+
| 20130924.000000 | 3.750000 |
+---------------------------+----------+
1 row in set (0.03 sec)
sphinxQL>select double(20130919.0+(15/4)),15/4 from sample2 limit 1;
+---------------------------+----------+
| double(20130919.0+(15/4)) | 15/4 |
+---------------------------+----------+
| 20130924.000000 | 3.750000 |
+---------------------------+----------+
1 row in set (0.02 sec)

Finding the last seven days in a time series

I have a spreadsheet with column A which holds a timestamp and updates daily. Column B holds a value. Like the following:
+--------------------+---------+
| 11/24/2012 1:14:21 | $487.20 |
| 11/25/2012 1:14:03 | $487.20 |
| 11/26/2012 1:14:14 | $487.20 |
| 11/27/2012 1:14:05 | $487.20 |
| 11/28/2012 1:13:56 | $487.20 |
| 11/29/2012 1:13:57 | $487.20 |
| 11/30/2012 1:13:53 | $487.20 |
| 12/1/2012 1:13:54 | $492.60 |
+--------------------+---------+
What I am trying to do is get the average of the last 7, 14, 30 days.
I been playing with GoogleClock() function in order to filter the dates in column A but I can't seem to find the way to subtract TODAY - 7 days. I suspect FILTER will also help, but I am a little bit lost.
There are a few ways to go about this; one way is to return an array of values with a QUERY function (this assumes a header row in row 1, and you want the last 7 dates):
=QUERY(A2:B;"select B order by A desc limit 7";0)
and you can wrap this in whatever aggregation function you like:
=AVERAGE(QUERY(A2:B;"select B order by A desc limit 7";0))