How to calculate difference between two periods values in Tableau Desktop - tableau-api

In Tableau Desktop, I have 2 different periods as per below:
and I want the result to be 20 --> Value (29) - Value (28) = 20
Thank you,

You should create a calculated field calculated_value and compute using 'Table (down)'.
sum(Value) - lookup(sum([Value]), -1)
Hope this helps!

I had a similar issue I was trying to resolve and managed to use parameters for added flexibility.
The calculation by Prem only gives you the difference based on the previous value.
In case you wanted it to be adjusted between different periods. You can use parameters to help with this.
Make Period a 'Date' datatype and create 2 parameters:
Params (Start Date/End Date): Type 'Data', List and pull values from Field 'Period'
Create a calculated field called 'Numerator': if([Period] = [Start Date]) Then [Value] else 0 end
Create a calculated field called 'Denominator: if([Period] = [End Date]) Then [Value] else 0 end
Create a calculated field called '%Difference': Sum([Denominator])-Sum([Numerator])
This gives flexibility for different periods and allows users to toggle which value they want and to see the difference accordingly.
Alternatively, you can see a link I found helpful as well: https://community.tableau.com/thread/225030

Related

level of detail expressions cannot contain table calculations or the attr function in Tableau

i have this tableau workbook
basically this calculated day different between each user_id and each transaction for each user_id with this calculation
DATEDIFF('day',LOOKUP(MIN([Created At]),-1), MIN([Created At]))
that pull filters its so filter the conditions of users (We can ignore this)
and date_rante filters its for calculated day different between date range on parameter
with this calculated
lookup(min(([Created At])),0) >= [START_DATE] and
lookup(min(([Created At])),0) <= [END_DATE]
so from the frequency i want to find out the Max of different day, with this calculated
MAX({FIXED [User Id]:DATEDIFF('day',LOOKUP(MIN([Created At]),-1), MIN([Created At]))})
but it says
level of detail expressions cannot contain table calculations or the attr function
so i used this solution https://kb.tableau.com/articles/howto/finding-the-dimension-member-with-the-highest-measure-value
and from that solution, i applied with my codes into like this
MAX({FIXED [User Id]:DATEDIFF('day',INT(LOOKUP(MIN([Created At]),-1)), INT(MIN([Created At])))})
but it turns to error datediff being called with string,integer,integer
based on #Anil solution, i tried to create it, and idk why the results was like this
new picture
Presently, as far as my knowledge of tableau is, tableau doesn't allow to calculate LOD calcs or further aggregations on table calcs. To find the transactions where the user took most/max time (in days) in subsequent order- You can do this workaround..
Let's assume your datediff calc field is named as CF1. create another calc field lets say CF2 with following calculation
rank_unique([CF1])
EDIT:
Change table calcs on this field similar to CF1. putting a filter on this field will give you the dates with max(time diff) as shown in screenshot.
table calculation options on first (datediff field)
table calculation options on second field (rank_unique)
I have added third field on colors
(Please note no field used in filters just to highlight)

How do I build a Tableau divide function based on a case statement?

In Tableau I have a fully normalized dataset e.g. I have a field for a dimension (country) a field for metric (profit and sales) and a field for value (fact numbers).
I was wondering if I can write a case statement where I divide profit by sales e.g. in SQL it would be something similar to:
(case when metric = 'profit' then [value] else 0 ) / (case when metric = 'sales' then [value])
I've tried building the case statement using the Tableau calculated field functionality but it returns zero - I suspect because I'm trying to do this in 1 step whilst in SQL I think it might be a 2 step process.
Please note that the simple solution of re-structuring the data isn't really an option as this creates other issues (not outlined in my simplistic example).
Try this:
step 1: calculated field Profit:
if metric='Profit' then value end
step 2: calculated field sales:
if metric='sales' then value end
step 3 : one calculated field to divide Profit/sales
then you use this to get the result.
I assume your dataset has profit and sales in different rows .Which might have been giving the trouble. The good news is that with Tableau 9.0 and later this can be achieved easily using LOD Calculations .
So you can try creating a calculated field as
country_profit : {FIXED country : SUM(CASE WHEN metric = 'PROFIT' then value)}
country_sales : {FIXED country : SUM(CASE WHEN metric = 'Sales' then value)}
result : country_profit / country_sales
Let us know if this works .

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

Default range for date range filter in tableau

I want to set the default range on a date filter to show me the last 10 days - so basically looking at the lastDate (max date) in the data and default filtering only on the last 10 days (maxDate - 10)
How it looks now:
I still would want to the see the entire range bar on the dashboard and give the user the ability to modify the selected range if he wants to. The maxDate changes after every data refresh so it has to be some sort of a condition that is applied to the filter.
How I want it to look (by default after every refresh of data - new dates coming in):
Any suggestions on how this can be done? I know I can use the relative date and show the data for last 10 days but that would modify the filter and create a drop down which I don't want.
Any suggestions are welcome!
One simple approach that does most of what you want is the following:
Create an integer valued parameter with a range from 1 to some max
you choose, say 100. Call it say num_days.
Show the parameter control on your dashboard as a slider, and give
it a nice title like "Number of days to display"
Create a boolean calculated field called Within_Day_Range defined as:
datediff('minute', [My_Date_Field], now()) < [num_days] * 24 * 60
Put Within_Day_Range on the filter shelf and select the value true.
This lets the user easily select how many days in the past to include, and works to the granularity of minutes (i.e. the last two days really means the last 48 hours, not starting at midnight yesterday). Adjust the calculated field if you want different behavior.
The main drawback of this approach as described so far is that it doesn't display the earliest date possible in the database because that is filtered out. Quick filters do an initial query to get the bounds, which has a performance cost -- so using the approach described here can avoid that query and thus load faster.
If you really need that information on your dashboard, you could create a different worksheet to get just the min([My_Date_Field]) and display that near your parameter control.

Crystal Reports and Adding values

I am using Crystal Reports XI and I'm trying to add a particular column if one column or another column is set to one.
Here is the current preview:
Username (GROUP 1)
MONTH (GROUP 2)
DATE SUBJECT TOTAL_TIME
End of group 2
End of group 1
Now I want to add the values in total_time if one of the two hidden fields contain 1 (true).
I tried using sum() function but it didn't work as it added all the times together.
I'm still new to Crystal Reports and I tried searching on google but nothing came up similar to what I need.
Any help would be greatly appreciated.
One alternative that I can suggest, You can use Parameter fields on your report.
Do the calculation on Code behind and set the calculated sum to parameter field on Page_Load event of Crystal Report page.
This parameter field will be used to display the sum on report page.
Please check this link to see
-How to create Parameter Fields:
http://devlibrary.businessobjects.com/BusinessObjectsXIR2/en/en/CrystalReports_dotNET_SDK/crsdk_net_doc/doc/crsdk_net_doc/html/crtsktutorialscrvparametersdiscretecreatingreport.htm
-How to set values in Parameter Fields:
http://msdn.microsoft.com/en-us/library/aa289936(v=vs.71).aspx
Your best option will be use "Running totals" by this way you can control the flow by keeping a condition to sum when the required column is 1.
Did you think about small trick? You wrote: I want to add the values in totaltime if one of the two hidden fields contain 1 (true).
Under these circumstances you can calculate helper field Total_Time_Conditional using formula:
Total_Time_Conditional = IIf(HiddenField1 = 1 or HiddenField2 = 1, 1, 0) * Total_Time.
This will multiply Total_Time
by 1, if any of hidden fields is 1
by 0, if both hidden fields are 0
Calculating Sum(Total_Time_Conditional) will give you expected result.