How to aggregate table calculation in tableau - tableau-api

this is my workbook
on that workbook, i calculate timediff between each transaction for each users. what i build first is the filters PUL with this calculate
{Fixed [User Id]: sum(
if [Created At]<=[END_DATE] then 1 else 0 end)}>=2
AND
{FIXED [User Id]: sum(
IF [Created At]<=[END_DATE] AND
[Created At] >= [START_DATE] THEN 1 ELSE 0 END)}>=1
this formula is to find out the users who match with conditions (do transaction at least 2 before end_date parameter, and atleast doing 1 transaction in between start_date parameter and end_date parameter) after that i add to context this filters to find out the users first.
and i made filters date_range with this calculate
lookup(min(([Created At])),0) >= [START_DATE] and
lookup(min(([Created At])),0) <= [END_DATE]
so it will visualize only transaction on range (start_date as first range, and end_date as last range) and also visualize last date transaction before first range (if any).
after that i make calculate called datediff
DATEDIFF('day',LOOKUP(MIN([Created At]),-1), MIN([Created At])) and put that on label so it will calculate the day different. and also i put the date in the detail and put the date also in rows and make it ATTR.
my question is, how to find out max, min, median, and average value from this calculate
i tried with calculated max
MAX({FIXED [User Id]:DATEDIFF('day',INT(LOOKUP(MIN([Created At]),-1)), INT(MIN([Created At])))})
but it return error datediff being called with string,integer,integer

For Max and Min you can proceed like I presented you a solution on your previous question. (For max create a rank calculation and sort is descending, For Min you create a second rank calculation ordered ascending).
However, as far as my knowledge of table calculations in tableau goes, Tableau doesn't allow to hard-code these table calculated fields and therefore you cannot-
further aggregate these results
perform LOD calculations on these.
For calculation of these like average and median, It is advised that you may please create a hard-coded column/field which give you the time-difference on any order with that customer's previous order. This you can do it in any programming language of your choice like R or python (or other).
Moreover, Tableau integration with R/python is through script-real type functions which are again of table calculation category and above restrictions will apply.
Good Luck.
EDIT as Alex Blakemore has suggested on a different question/answer, you can use window functions with a slight tweak. Let's assume your calculated field name for datediff is [CF], then create four calculated fields with the following calculations.
window_max([CF])
window_min([CF])
window_avg([CF])
window_median([CF])
and name them [CF max], [CF Min], [CF avg], [CF Median] respectively.
Now edit table calculation with nesting in each four these, as follows-
click nested calculations down arrow. CF will be listed there. change its calculation to specific dimensions, at level deepest and restarting at evrey user id. the screenshot is
thereafter click nested calculations down arrow again. select CF_max/min/med/avg (as the case may be) and create table calculations with table down.
You'll get a view like this as desired.

Related

Compute average (with condition) in Tableau

Wanted Result: Average of the turnover value on days that are between the start and end reference period.
Using Tableau Desktop
Lod expression
The first step is i return the turnover value on days that are between the start and end reference period, and return null otherwise.
Daily Turnover in reference period
IF [Date]>= [Start reference date]
and [Date]<= [End reference date]
THEN [Amount] END
Second Step is to calculate the average across this range of values for each product.
Average Turnover in reference period
{FIXED [Product]: AVG(Daily Turnover in reference period)}
Here a screen shot
The average must be 2331 and not 24.
Really i need HELP.
Thanks.
There are multiple possible approaches, here is one.
Define an LOD calc as Daily_Amount_Per_Product
{ FIXED Product, Date : SUM([Amount]) }
Put Date on the filter shelf and select the range of dates you want to analyze. Put Product on the Rows shelf and put Daily_Amount_Per_Product on the Columns shelf.
At this point you are almost, but not quite done. Since your LOD calc as at a deeper level of detail (has more dimensions in play) than your view, Tableau will perform aggregation to get a result at the same level as your view - which is why you see the word SUM before your field on the Columns shelf. If you want to see the average instead of the sum, change SUM to AVG and you should have your result.

Execute IF-THEN_ELSE before Execute Calculation - Tableau

I have a graph below.
I would like to calculate lapsed rate which is sum of lapsed value divided by sum of inforce value. I use the formula below in calculated field.
abs(sum(if[Status]='lapsed'then[TotalAmount]end))/abs(sum(if[Status]='inforce'then[TotalAmount]end))
However that formula will also pick the value from Q2 (quarter 2) 2016. What I want to do is to tell tableau to check first if any quarter does not contain both inforce value and lapsed value then skip that quarter. In this case I need to calculate lapsed rate which does not include Q2 2016. How do i do this?
I'm using Tableau v.10.
Thanks.
This is just a quick approach and may not be the most efficient but it seems to work.
You need to use a combination of a row level calculation and a level of detail calculation.
The level of detail calculation can be used to flag the quarters which have both a lapsed and inforced status. Once these quarters are flagged you can calculate the lapsed rate at a row level which can then be rolled up using a sum.
Create a calculated field as follows:
Let me know if you have any questions or need any tweaks.
if
avg(
// Calculate the number of Inforce/Lapsed occurences per Quarter
IF
[Status] = 'Inforce'
or
[Status] = 'Lapsed'
then
{ FIXED
DATEPART('quarter', [Date]):
countd([Status])
}
else
0
end)
//
= 2
then
// Calculate the Lapsed Rate as both statuses exist in the quarter
sum((if
[Status] = 'Lapsed'
then [Total Amount]
END))
/
sum([Total Amount])
END

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

Can't Sum Formula in Crystal Reports

I am trying to sum two different formula fields in Crystal. It will not let me select them from the Sum. The first formula is
if Sum ({tblPostedLine.pli_QUANTITY_SHIPPED}) >= 1
then {tblPostedLine.pli_NET_PRICE}
else ({tblPostedLine.pli_NET_PRICE} * -1)
I am trying to take a price and make it a negative value if the quantity is a negative value. I then want to sum the amounts to get a net amount that was shipped.
The other formula is
If PreviousIsNull({RodsvwCatalogAnalysis.pro_PROMOTION_CODE})
or ({RodsvwCatalogAnalysis.pro_PROMOTION_CODE}) <>
Previous({RodsvwCatalogAnalysis.pro_PROMOTION_CODE})
then {RodsvwCatalogAnalysis.pit_AREA_PER_PAGE} else 0
With this formula, I am trying to sum at a group level, not the details level. When I just sum the group level number, it is adding it every time the value is listed in the details as well.
I am open to any suggestions.
Thanks!
For the first scenario,
Create 1 variable formula in detail level to hold with your requirement.
for e.g.
#NetPrice , formula if {tblPostedLine.pli_QUANTITY_SHIPPED} >= 1 then
{tblPostedLine.pli_NET_PRICE} else ({tblPostedLine.pli_NET_PRICE} * -1)
using running total field feature in your Field Explorer to sum up the value and place
your group footer.
For the second scenario, I believe it relate to promotion group and again you could use the running total field feature to evaluate the sum condition and reset the value when condition are meet like field value change.