Tableau: How to calculate number of days - tableau-api

I have custom SQL query in tableau that displays the data in the following format:
Start Date | App
6/21/16 app1
6/22/16 app2
6/23/16 app3
In this case, the end date would be '6/23/16'. So, app1 has been "live" for 2 days, app2 for 1 and so on.
I am trying to find the the number of days an app has been live. I can try using the DateDiff function but I would need to hardcode the values in that case and I want it to be dynamic.
The challenge is to have a calculated field that would find the max date in the entire column and subtract it from the individual app's date. This would give me the 'number of live days' for an app.
I am new to tableau and do not know how to proceed. Any help is appreciated.

Here is one solution.
datediff('day', [Start Date], { fixed : max([Start Date]) } )
Note the expression in Curley braces. That is a level of Detail (LOD) calculation -- basically a separate subquery at a potential different level of detail. So you can compare values for each row with values computed based on the whole table.
Depending on how and where you want to use this calculation, you might want to alter that LOD calculation to be fixed for certain dimensions or include or exclude certain dimensions. The online help should explain.

Just use"DATEDIFF('day',[Order Date], [Ship Date])",
order date and ship date are example dimensions from superstore data.xlxs

Related

How can I calculate my win rate on one column based on the date on another?

I created a rudimentary Google Form to track my win rate at Starcraft. The first column on the resulting Google Sheet is Timestamp created by the form.
I have another column that has my win-loss, and I am able to calculate my percentage for the entire sheet (all games). However, I want to be able to see my daily win rate, and I can't figure out the correct way to go about it.
I tried COUNTIF, COUNTIFS, with TODAY() and I was able to count the games for a certain day, but I don't know how to use it to tie in with my win-loss column. What I currently do is adjust my Daily formula to specify today's date before playing. I was hoping I won't need to do this.
Please see Win-Loss Stats Sheet
Solution:
You can extend your formula to compare against the date in column A:
=(COUNTIFS(D2:D, "Win", ARRAYFORMULA(INT(A2:A)),TODAY())/((COUNTIFS(D2:D, "Win",ARRAYFORMULA(INT(A2:A)),TODAY()))+(COUNTIFS(D2:D, "Loss",ARRAYFORMULA(INT(A2:A)),TODAY()))))
The additional condition would be ARRAYFORMULA(INT(A2:A)),TODAY(), which converts the timestamps into dates and compares them to today's date.
Sample Output:

How can I set a non-continuous date filter?

I need to be able to filter my data to comparable weeks/months across various years. And I need to be able to update those choices on the fly. Is there a way in tableau to set a non-continuous date filter?
because the base data is saved on the tableau server, I'm unable to join additional tables to it, so my initial idea of making a table of year / start date / end date and joining that to my data with a simple t/f filter isn't panning out.
I've considered making series of parameters, but it seems like it might get a bit overwhelming to make so many
Other things I've considered is writing an extended rule like
IF [year] = 2015 THEN [date]BETWEEN(date A, date B)
or the tableau equivalent to BETWEEN
ELSEIF [year] = 2016 THEN [date]BETWEEN(date C, Date D)
ELSE IF //and so forth
Does anyone else have experience with this? Any strategies you might recommend on the parameters vs. a long formula?
Any ideas are greatly appreciated!
Standard tableau filters on dates allow filtering by both continuous date ranges or filtering by parts of dates.
For example, if you wanted to compare January sales cross multiple years, you can drag your date field to the filters shelf and then right-click the date field in the shelf and choose "Discrete" rather than "Continuous" or choose "Month" in the upper set of options about which date component you want (the upper set selects date components, the lower set selects date ranges with in filters and axes).
So, if you put a date field on the filters shelf and select "month" as a discrete you will get a filter that allows you to filter for every January in the dataset.

OBIEE YTD Issues

I have a fact table housing different granularity (date grain)
Monthly
Daily
The month data can be accessed by filtering by end of month date or using YYYYMM date format. In OBIEE RPD repo, the fact is set to LAST Aggregation.
I want to perform Year to Date analysis. And I want to sum only month end dates.
Using function TODATE(Measure), it tends to sum up all the data through out the month e.grain
Date Amount YTD TODate(Amount)
31/01/2106 100 100
28/02/2016 200 300
14/03/2016 50 350*
31/03/2016 100 450
I want YTD to ignore 50 and return 400, so also any other dates that falls within any month. And if if I Select 14/03/2016 I want 350 to return.
Thanks.
Alter the table to add a flag, something that flags Y if the record is at the specified monthly grain, and N if the record is not at the specified monthly grain.
In the logical layer, create two distinct LTSs with the first filtering on the flag for Y. This will be where you will calculate and source all your to date measures. The second LTS can either be filtered to N, or can be left to all the data depending on what you want to do with it.
The performance increases should come from the fact that any month measures you build off that monthly LTS will only hit records flagged as month, and will bypass all that other data that is not relevant. So if a user runs a report only asking for monthly measures, the query will automatically filter to that specific data.
What will happen is if a user selects your to date measure and a specific date measure on the same report, OBIEE should fire off two separate queries to get the data and stitch together based on common dimensions.
Could someone create this in the front end? Probably. You would have to do some sort of PERIODROLLING function, and tell it to aggregate at the month level, but I am afraid it may still roll those days up into a larger than desired number. A TODATE function will not work here.

Creating Bins in Tableau for Days in a Week

Could someone please explain me creating BINS based on Weekdays in Tableau? I tried creating different Calculation Fields but it won't work
You're working too hard.
Tableau already knows how to bin values by dates at many levels of granualarity: such as year, month, day, weekday, hour etc. So you don't need to create a new field to bin dates by the day of the week. (creating bins is not difficult, it's just already available in this case)
Just put a discrete (blue) date or datetime field on a shelf. You'll see the date level of granularity displayed like, say, YEAR(MyDateField) with a leading plus sign.
You can either
click on the plus sign to drill down by adding a second level, say MONTH(MyDateField)
or
right click on the field to select the date level of granularity you want
Alex's Answer is exactly correct, Tableau will perform the operation automatically. What is great about is that you can select various formats (Full day name, number, 1 letter or 3 letter day etc.).
However if you absolutely need to you can used this formula:
datepart('weekday',[Date])
to give you the 1 (Sunday) to 7 (Saturday) value if you need it for something other reason, say another calculation.

Filter data by different time intervals

I need to filter my query with different time intervals like that:
...
where
date >= '2011-07-01' and date <='2011-09-30'
and date >='2012-07-01' and date >='2012-09-30'
I suppose such code is not good, because these dates conflicts with each other. But how to filter only these two intervals, skipping everything else? Is it even possible? Because if I query like this, I don't get any results.I tried to use BETWEEN, but it does same thing.
I bypassed this by extracting quarters from years and calculating only third quarter. But then other quarters sum is showed as zero and I can't ignore these rows that have sum column with zero value. I tried to filter where price > 0 (column where sum goes), but it says that column do not exist. So I put it whole FROM under '('')' brackets to make it calculate sum before where clause, but it still does give me error that such column do not exist.
Also if you need to see query I have now, I can post it, just tell me if it is needed.
I want to do this, because I need to compare third quarter of two different years (maybe I should use another approach).
You're not going to get any results because you can't have a date that's both within 7/1/2011 through 9/30/11 and after 7/1/2012 and after 9/30/12.
You can have a date that is either between 7/1/20122 and 9/30/2011 or between 7/1/2012 and 9/30/2012.
SELECT col1 FROM table1
WHERE date BETWEEN '7/1/2011' AND '9/30/2011' OR date BETWEEN '7/1/2012' AND '9/30/2012';