Tableau count average from averages - tableau-api

There is some data:
A, B, C (where C = A/B)
10, 20, 0.5
10, 100, 0.1
There are two ways of counting the average C:
First is sum(A)/sum(B), that gives C equal 20/120=0.1666
This is Tableau: sum([A])/sum([B])
Second is avg(C), that gives C equal (0.5+0.1)/2=0.3
This is in Tableau: No Idea...
Both have different usages. I need the second one, but can't find a way to calculate it. Anything I try, I always get the first type.

You are correct. In your first method, tableau is calculating the calculated field C by summing values according to the dimensions you are partitioning on in your view.
In your second option, you need to tell tableau to calculate column C (or define a different column D) for every row of the data, no sums involved. Simply: [A]/[B].
Then when you AVG column C (or D) you will get the average of all A/B values, instead of SUM(A)/SUM(B). You can get the average of column D by using the built in tableau AVG function instead of doing it manually

Related

Percentage of total changes on applying filter

I have a data table with three dimensions and one measure. For each row, I am trying to calculate the percentage of total (calculated by taking the sum of rows) using a calculated field.
As seen in the screenshot attached, For the column titles 'Dec-19' I want the values to be a percentage of current value / grand total (calculated at the bottom as 122,187)
Screenshot of DataTable:
So e.g. for the Column B value of 2000, the Dec-19 column should be (97/122,187) * 100 = 0.079.
I have achieved this by creating a calculated field with the formula: SUM (sales) / MAX ({EXCLUDE (Column B): Sum (sales}), where sales is the measure used in the datatable.
However, upon application of filter on column B, the percentage value changes. e.g. if I select the value 2000 in my filter for column B, I get the percentage as 100%. It seems as if the percentage is being calculated based on only the rows in the filter.
Since you haven't included any sample data, I created some sample data like this, hope this resemble yours.
Thereafter, I built a cross-tab view in tableau, somewhat like yours
If that is the scenario, use a calculated field, say CF like this, instead of yours
([Sales])/
{FIXED [Col1], DATETRUNC('month', [Col3]) : sum([Sales])}
Dragging it in the view
and thus, filtering won't affect your calculation

TSQL Summing to a value

I am looking to take a table like the one below. Code is a part that exists on a job, and Value are the different cut lengths of that part needed. Ultimately, I want to sum as many of these cuts that I can get out of a full bar which would equal 1.
Code Value
X 0.10000000
X 0.40000000
X 0.64000000
X 0.30000000
X 0.52000000
Expected End Results is
Code Value
X .8
X .64
X .52
I am not sure the best way of saying it but I want to sum all rows grouping by the Part Code and return as many results of the Sum that do not exceed my predetermined value of 1...

Tableau filter values instead of aggregate

I have the following sample data
Origin A, A, A, A, A
Destination C, D, E, F, G
Distances 5, 6, 7, 8, 9
Revenue 20, 25, 40, 55, 60
What I want to do is filter out all destinations with distances 7 or greater and display the aggregated results in one row.
What I can do so far is get this table:
Origin: A, A
Destination: C, D
Revenue: 20, 25
What I want is to display the following table with the Origin and the Sum of the Revenue:
Origin: A, Revenue: 45
When I apply the filter on Distance, I set it to be At Most 6, which works when I have 'Destination' in the Columns field, but when I take 'Destination' out, then the filter applies itself to all the distances aggregated (35), which would then filter out everything.
How can I get the filter to filter on individual values instead of the aggregate?
Thanks for your help!
Right click the field (on the filter shelf), choose dimension (rather than measure). This will consider individual values of the field, rather than the aggregation (probably sum by default).
Not sure if this will solve your problem, as I didn't understand what you are trying to do. But this is one way to filter individual values rather than the aggregation.
There are several variations possible on the filter shelf that cause different effects, depending on whether the field is set to dimension or measure, and what options are selected on the various tabs of the filter panel, not to mention context filters.
In your specific case, you should simply be able to place the Distance field on the filter shelf to get a row level filter to on include data rows where the Distance value is <= 6

Taking average of one column with w.r.to other column

I have two columns in .std file. I want average of the second column values corresponding to all values ranging from some value (eg. 1.0- 1.9) in first column how can I program in Matlab?
Say, a is the name of your two column matrix. If you want to find all of the values in the first column in the range of 1.0 - 1.9 and then use those entries to find the mean in the second column you can do this:
f = find(a(:,1)>=1 & a(:,1)<=1.9)
m = mean(a(f,2))
find will find the values that lie within this range and return the index, and a(f,2) accesses those indices in the in the second column and takes the mean. You can also do it with one line like so:
m = mean(a((a(:,1)>=1 & a(:,1)<=1.9),2))

Assigning the different row to another matrix after comparing two matrices

i have two matrices
r=10,000x2
q=10,000x2
i have to find out those rows of q which are one value or both values(as it is a two column matrix) different then r and allocate them in another matrix, right now i am trying this.i cannot use isequal because i want to know those rows
which are not equal this code gives me the individual elements not the complete rows different
can anyone help please
if r(:,:)~=q(:,:)
IN= find(registeredPts(:,:)~=q(:,:))
end
You can probably do this using ismember. Is this what you want? Here you get the values from q in rows that are different from r.
q=[1,2;3,4;5,6]
r=[1,2;3,5;5,6]
x = q(sum(ismember(q,r),2) < 2,:)
x =
3 4
What this do:
ismember creates an array with 1's in the positions where q == r, and 0 in the remaining positions. sum(.., 2) takes the column sum of each of these rows. If the sum is less than 2, that row is included in the new array.
Update
If the values might differ some due to floating point arithmetic, check out ismemberf from the file exchange. I haven't tested it myself, but it looks good.