Tableau percentage calculating - tableau-api

I'm quite new in tableau environment.
I have one column with reps. Second column contains values 'Yes' and 'No'. Third with customers names.
I want to count this: 'Yes' Clients/ All clients for each rep
I created calculating field: COUNT(IF [C2]='Yes' THEN [C2] ELSE NULL END]/COUNT [C2]
but it doesn't work, to each rep I have a 1 value.
How to fix it?

Well, apparently Tableau counts NULLs as well. Try this instead:
SUM(IF [C2]='Yes' THEN 1 ELSE 0 END)/COUNT([C2])
This way you count only the 'Yes'

Related

Fetch adjacent column word?

I want condition like "if max(col1) then col2 end "in tableau calculated feild, output will be "e".
thanks in advance
Simply use
If [col1] = {max([col1])} then col2 end
Drop this in view and Hide nulls, you'll get desired value.
I've tried it with these steps and it looks to be working :)
# first find the max. Using fixed lod calculation to do it across the whole dataset. The 1 is the same value across all so its doing a group max, but the group is the full data
MaxCol1: {fixed 1: MAX(Col1)}
# pick out rows where max is found in Col1
Col1Match: Col1=[MaxCol1]
# find Col2 where there's a match and fill rest with NULL's
Col2Value: IF [Col1Match] then Col2 else NULL end
# This will be a bunch of NULL's and "e"
# Finally, do the same as in the first calculation to get the result
Output: {fixed 1 : max([Col2Value])}
You can try combining some of these steps together now to clean up the space a bit. Does this suffice or is your real data more complicated?
Best,
Jonny

How to sum a calculated a field on top of another calculated field?

The current issue I have maybe a bit difficult to describe but I will do my best.
Currently, in my workbook, I am experiencing duplicates at the product level so I created a calculated field to work around that. I wanted to know which individual products have not been quoted the past year so the answer should be 1 for not quoted and 0 for quoted, I worked around that by doing an if statement with % products not quoted the formula looks as such:
IF [% Not Quoted] > 0
then 1
else 0
end
which I named Prod Not Quoted. That worked great for me,
however now I want do the count or sum(?) of products not quoted at the vendor level which would mean my products not quoted needs to be grouped by the vendor name. To be specific my objective is to provide a table of all products not quoted by vendor name without duplicates at the product level. What I tried to do is create a new calculated field using the previously which I calculated as the following:
IF [Prod Not Quoted] = 1
then sum(1)
else 0
end
The latter calculation, however, gave me the sum of all products quoted and not quoted along with the duplicates I am trying to avoid. Why would IF [prod Not Quoted] = 1 then sum(1) else 0 end not work?
Someone on tableau forum with the following suggestion
SUM (IF [Prod Not Quoted] = 1 THEN 1 ELSE 0 END)
however I got the error cannot sum something that is not already aggregated.
Is there any kind of work around to the issue I am having?
If you have duplicates at the dimension level you can use the FIXED calculation to select just a single row at the dimension level. For example:
{FIXED [Product]: MAX(quoted)}
What this is doing is saying "for each Product, what is the max quoted value?" So if you have duplicates, it'll return only one value
Now we are solving the duplicates problem we can wrap it in SUM:
SUM({FIXED [Product]: MAX(quoted)})
To answer your question about why the sum isn't working, it's because you're trying to do a sum of a sum. If you still wanted to take the approach that was offered to you then you could change it to:
IF [Prod Not Quoted] = 1
then {FIXED [Product]: MAX(quoted)}
end
(I called the aggregation field quoted, but it'll be the measure you're working with)

Using COUNT in Tableau to count observations by group

Thanks in advance for any advice you can offer! I'm building a Tableau dashboard to explore housing affordability and school quality in different neighborhoods in my area. A user will select their occupation and see a graph of neighborhoods plotted based on school quality and housing affordability. To explore housing affordability, I'm using county level assessor data with the valuation of every property matched to neighborhoods.
The goal is to display the percentage of homes in an area that are affordable given the median occupational wages for the job a user selected. Right now, I'm trying to use a calculated field with COUNT([Parcels]<[Occupation])/COUNT([Parcels]), but I need to find a way to count the number of properties in each specific neighborhood below the cut off value.
Does anyone know of a way to count elements of a particular group in this way in Tableau?
I'm on a Mac, using Tableau Desktop, and doing the back end analysis work in R. Thank you!
You seem to misunderstand what the function COUNT() does. You are certainly not alone. Count() behaves in Tableau almost identically to how it does with SQL.
Count([some field]) returns the number of data rows where the value for [some field] is not null. It does not not return the number of rows where [some field] evaluates to true, or a positive number, or anything else.
If [some field] always has a non-null value, then Count([some field]) is the same as SUM([Number of Records]). If [some field] is always null, then Count([some field]) is zero. Count() is not like Excel's CountIf function.
If you want to count data rows that meet a condition, you could try COUNT(if [condition] then 1 end) Since the missing ELSE case defaults to null values, that expression will count rows where [condition] is true.
So one way to get the percentage of affordable homes is count(if [affordable] then 1 end) / count(1) assumes each Data row represents a home. Then format your field to display as a percentage. Another option is to learn to use quick table calcs
If you want to display the number of rows in a given visualized table you could also use SIZE()
Source, official docs:
https://help.tableau.com/current/pro/desktop/en-us/functions_functions_tablecalculation.htm#size

postgresql one column 2 opposite criteria

In my data set, I am looking for value that have both positive and negative result under the amount category. For example, one entity can be bank account, and there are money coming in (positive number) and money going out (negative number).
SELECT description, account_subtype_id, subcategory_id, (case when amount > 0 then 1 end) AS amount_p, (case when amount < 0 then 0 end) AS amount_n
FROM mx.transactions
LIMIT 100
;
This approach doesn't help much because now my data looks like:
bank_A 1 null
bank_A null 0
But I really want to get something like:
bank_A 1 0
because this will be really helpful for my analysis.
Actually. If there is a way to do this, it would be even better:
For example, an entity has
Bank_A $500 -$300 -- (these two results both are from the amount column)
If you want just one row per description, you need to group by description and use aggregate functions. A clean way to check whether there's any positive or negative amount would be to check whether min(amount) and max(amount) are less/greater than 0:
SELECT
description,
min(amount) < 0 AS amount_n,
max(amount) > 0 AS amount_p
FROM mx.transactions
GROUP BY description
These tests will give you true and false values, but you can use them in your CASE/IF statements if you want something else. Or to get the actual values rather than testing against 0, just use min and max directly.
It looks like you've got multiple columns potentially acting as your bank_A identifier. If that's the case, you can GROUP BY all of them.
SELECT
description,
account_subtype_id,
subcategory_id,
min(amount),
max(amount)
FROM mx.transactions
GROUP BY description, account_subtype_id, subcategory_id

How to calculate current-1 year in tableau

I am using Tableau 8.2,I want to show calculations for current as well as previous year,how to apply this dynamically? it is getting possible if the year is written manually but it will be only temporary solution, how to do that dynamically?
i.e on applying filter Year=2013 then sales and orders value of current-1 year should be displayed
I have sales and orders On the Columns.
A quick solution to this would be to create a new calculated field for previous year sales by using the Lookup function.
For example:
LOOKUP(SUM([SALES]),-1)
You can simply introduce two calculated fields which will give you the current and previous year. These can be used on the rows and columns shelves or any where else depending on whether your aiming for chart, text table or any other viz.
Calculated field 1 = Current Filter
if datediff('year',[Date],TODAY() )=0 then 1
ELSE 0
END
Calculated field 2 = Previous Filter
if datediff('year',[Date],TODAY() )=1 then 1
ELSE 0
END
The formulas above can be used if the required measure is just a count otherwise you could replace: then 1 with then [Amount] or whatever measure this applies to.
Create a parameter and use IF Year = 'Parameter Value'THEN Sales ELSE 0 END
or use LOD to know the Max Year if you want it to be more dynamic: IF Year = {FIXED: MAX([Year])} THEN Sales ELSE 0 END