head & tail function in Tableau - tableau-api

What is the most effective procedure or equivalent for "head" & "tail" in Tableau if you are try to isolate the last or first items of an vector?
The known procedure e.g. in R is easy if you are looking for an average of the last 10 numbers:
mean(head(x, 10)) or mean(tail(x, 10)).
I try to find a solution in Tableau with LOD "max" & "min" but "show me the first 10 min items"?! No chance!

If you are talking about table calculations, then you can use the WINDOW_XXX() functions -- such as window_sum(sum(x), 1, 10) or window_avg(sum(x), size()-9, size())
Table calcs operate on the aggregated query results returned from the data source, hence the aggregation function sum() around the field x.
It might be simpler to learn to use Top N filters instead table calcs, depending on what problem you are trying to solve.

Related

How to divide an Aggregate and Sum function

I am working in Tableau and trying to create a formula that will return me the value of each customer that walks into a store by dividing Net Sales / Traffic. When I try to combine the two separate formulas, it gives me the following error: Cannot mix aggregate and non-aggregate arguments with this function. The two functions I created that I'm trying to divide are:
SOT = (SUM([Sales Net])-SUM([Sales Gcard Net]))/SUM([Traffic Perday]) and SOT Goal
When I look at it in Tableau, it's stating that SOT is an aggregate function. How do I work around this to be able to get
SOT / SOT Goal
Aggregate variables are values that are calculated in the view, and depend on the level of aggregation in Tableau. e.g. sum(Sales) will show different values in Tableau if it’s next to a Region dimension, or if it’s next to a Category dimension.
In order to avoid the errors you can use many solutions. My favorite is indeed LOD expressions. In your view, though I do not have required sample data and therefore, I cannot try my hands on different possibilities here, I suggest that this should work-
SOT = ({SUM([Sales Net])}-{SUM([Sales Gcard Net])})/{SUM([Traffic Perday])}
Do remember that this solution will over-ride your filters and if you are using filters you have to add all those to Context.
EDIT
While trying different possibilities remember these things...
{SUM([Sales])} will sum the sales over entire data and {} i.e. curly braces wrapped around the sum function will cause to return the value as non-aggregate. In other words, this will work as LOD and if you'll add this field to view, the sum of entire sales will be shown against each row.
{FIXED [DIMENSION NAME] : sum([Sales])} will sum sales separately for each Dimension value. Fixed statement (LOD) again returns the value as non-aggregate value. if you'll add this field to view, the sum of entire sales for that dimension will be shown against each dimension.

How to calculate within a factor in Tableau

Apologies if this question is trivially easy, I'm still learning Tableau.
I have data where the variables Set and Subset are arranged by week (W1 to W52) and by Source (A or B). So if I put Week into Rows and create the calculated fields
SUM(Set)
SUM(Subset)
Rate = {INCLUDE Source: SUM(Subset) / SUM(Set)}
I get data that look like this:
Week SUM(Set) SUM(Subset) Rate
A B A B A B
W1 1234 123 567 56 45.95% 45.53%
So far, so good. But what I really want is the percentage difference between Rate(A) and Rate(B) by week:
Diff = (Rate.A - Rate.B) / Rate.B
I could do this in a second if I were using Excel or R, but I can't seem to figure out how Tableau does it. Help?
There's a built in table calculation "Percent Difference" , you can deploy it using compute using Table across and relative to previous. For that you need to have continuous measures.
Something like this will be the calculation-:
(ZN(SUM([Quantity])) - LOOKUP(ZN(SUM([Quantity])), -1)) / ABS(LOOKUP(ZN(SUM([Quantity])), -1))
Create two different for "Set" & "Subset"

Tableau, how to calculate Weighted Standard Deviation

I've a problem to calculate weighted standard deviation. Here's the formula I used:
sum([Weight]*(([Variable]-[Mean Score - Variable])^2))
/
SUM([Weight])
But there's a error pop up message "Cannot mix aggregrate and non-aggregrate"
I wonder what's wrong with my formula?
Thanks
I am assuming Variable and Weight are explicit fields in your dataset, while [Mean Score] is a calculated field you defined in Tableau.
[Mean Score] is an aggregate calculation; Variable is not. You can check this by dragging [Mean Score] to any shelf in Tableau, and note that it is show within the prefix AGG(). Note that you can't select the form of aggregation (SUM, MIN, AVG) to apply in that case, because the aggregation function is defined within that calculation.
You can't mix aggregate and record level calculations directly. Record level calculations are evaluated once for each individual data row. Aggregate calculations are evaluated once for each block of data rows.
The dimensions used in your worksheet determine which data rows are grouped together into blocks (partitioning the data). Analogous to the fields that follow the keyword GROUP BY in SQL select statement. As with SQL, the other fields referenced must be aggregated somehow such as via a SUM(), MIN(), MAX() or other call. Tableau calls those fields measures.
The most straightforward solution is to revise your definition of [Mean Score] to make it a Level Of Detail (LOD) calc instead of an aggregate calc.
That will allow you to essentially first compute the mean score separately, and then reference that result in your record level calculation. You will have to decide among 3 different ways for determining the dimensions for your LOD calc. See the online help for more info on LOD calcs.
For example, try replacing [Mean Score] with { include : [Mean Score] }

Get 2 max date in Tableau

My requirement is to get the second max date available in report and filter the data set on this.
I tried something like this:
datediff('day',dt,max(dt))=1
Referred to this link
any help?
You're going to need Tableau 9.0 for this. Basically because any calculation you do on Tableau depends on the level of detail you have on the worksheet (the dimensions you put in there). So datediff('day',dt,max(dt))=1 won't work. First because you're mixing aggregated fields (max(dt)) with non-aggreagated (dt). Second, because the aggregation depends on the dimensions in the workfield.
But Tableau 9.0 has a new awesome feature, called Level of Detail calculations. It allows you to perform calculations in the level of detail you choose, depending not on the dimensions on the sheet. It is also calculated BEFORE any calculation on the worksheet (just after context filters).
Now to the answer. First I'll figure out what is the max(dt). Let's call it max_dt
{ FIXED : MAX(dt) }
This will calculated the maximum of dt in all your database
Now to get the second max, you can go like this:
{ FIXED : MAX(IF dt != max_dt
THEN dt
END)
}
This will calculated the maximum of dt, ignoring those who are equal to max_dt (that is the true max(dt)). Therefore, the second max.
Take a look on those LOD calculations. They were just released, I'm having tons of fun with them right now
If the view has date dimension
The easy way to do this,is to create a calculated Last()=1
then filter off the records that evaluate to TRUE

Weighted Average Fields

I'm totally new to doing calculations in T-SQL. I guess I'm wondering what is a weighted average and how do you do it in T-SQL for a field?
First off as far as I know a weighted average is simply just multiplying 2 columns then average it by dividing by something.
Here's an example of a calculated field I have in my view, after calling one of our UDFs. Now this field in my view needs to also be a weighted average....no idea where to start to turn this into a weighted average.
So ultimately this UDF returns the AverageCostG. I call the UDF from my view and so here's the guts of the UDF:
Set #AverageCostG = ((#AvgFullYear_Rent * #Months) +
(#PrevYearRent * #PrevYearMonths))
/ #Term
so in my view I'm calling the UDF above to get back that #AverageCostG
CREATE View MyTestView
AS
select v.*, --TODO: get rid of *, that's just for testing, select actual field names
CalculateAvgRentG(d.GrossNet, d.BaseMonthlyRent, d.ILI, d.Increase, d.Term) as AverageRent_G,
....
from SomeOtherView v
Now I need to make this AverageRent_G calc field in my view also a weighted average somehow...
Do I need to know WHAT they want weighted or is it assumed that hey, it's obvious.. I do not know what I need to know in order to do the weighted average for these guys...like what specs I need if any from them other than this calculation I've created based off the UDF call. I mean do I have to do some crazy select join or something in addition to multiplying 2 fields and dividing by something to average it? How do I know what fields they are to be used int he weighted average and from where? I will openly admit I'm totally new to BI T-SQL development as I'm an ASP.NET MVC C#/Architect dev...and lost to this calculation stuff in T-SQL.
I have tried to research this but just need some basic hand holding the first time through this, my head hurts right now caue I don't know what info I need to obtain from them and then what to do exactly to make that calc field weighted.
They'll have to tell you what the weighting factor is. This is my guess.
SUM([weight] * CalculateAvgRentG(...)) / SUM([weight])