Difference between two values - same Dimension, same Measure - tableau-api

As you can see from the picture above I am trying to add new column and to calculate the difference between =2014-2017.
Is there any way to make this because Tableau's option "Table Calculation" doesn't play role for me.

Working out the difference between the first and last periods with table calculations:
First you need to get minimum year's values (i'm calling the field "Min Year Select"):
IF DATETRUNC('year',[Order Date]) =
{FIXED: MIN(DATETRUNC('year',[Order Date]))}
THEN 1 END
The above field named Min Year Select is saying that it should return a 1 if the year of the order date is the minimum year in your date range
Now we are flagging the smallest years, we can create a field to get the values (i'll call this "Min Year Segment"):
IF SUM([Min Year Select]) >= 1 THEN [Sales] END
Here we're saying that if the year is flagged as the smallest (as classified by the previous calc field we made), then get the value
But before we can compare the two values, you have to work out the number of time periods between the min and current year so that the difference calculation lookup field is comparing the right values (i'll call this "Number Years in Range"):
{FIXED [Segment]: COUNTD(DATETRUNC('year',[Order Date]))}
What we're doing is fixing the query at the category level (segment), think of this as removing the date pill from your report, then performing a calculation. Here it's COUNT DISTINCT years. So if a segment has data for 2011,2012,2013; then the query returns 3
We can now get the difference between your latest and your minimum Segments (called: "Difference from First Last Segment"):
[Segment] -
LOOKUP([Min Year Segment],
-1*(Number Years in Range)-1)
Firstly we get the first year's sales for each segment (Min Year Segment will be null for all years that aren't the first, so we need to lookup the first by going backwards by the number of years in our range:
We do -1 * because we want the lookup to lookup backwards, then we add in ("Number Years in Range" - 1) because we want to lookup to the period that had the earliest data. We do minus one so we're excluding the current year/latest year in your dataset
This is a lot to digest, I think it's easier to present as a picture too:
Here we calculate the difference between the first and last month, with the value in the last month
If this helped or you have any more questions, please vote on my answer/let me know

Related

How do we calculate difference and percent difference between two columns in tableau

]Difference and percent Difference must be calculated.
I cannot do Apr20-MAy20 because it is not always the same. I need to show the current month and previous month
So I did a relative filter to just show the current month and previous month.
So the difference of two columns should automatically change when the month changes.
Now how do I get the same month of prior year, how do I filter ?
I also need to calculate the difference of current year same month and previous year same month.
Thank you in advance for any help!
When I do table across difference, the difference value is overwriting the existing May and Apr month values as the below screen shot, how to show the difference in another column
Currently:
Below is Expected:
Sounds like you should create a custom filter for the dates. You want:
This month this year
This month last year
Last month this year
There are a number of ways you could do this. I'll give one example and will assume there aren't any future dates in your data set.
[DateFilter]: DATETRUNC('month',[YourDateField])>=DATETRUNC('month',DATEADD('month',-1,TODAY())) OR DATETRUNC('month',[YourDateField])=DATETRUNC('month',DATEADD('year',-1,TODAY()))
Put the to the filters shelf, set to True, and it should keep the months you want.
Then you can just use the standard table calculations to calculate Difference and Percent Difference.
Note, the formula isn't tested, just typed directly into here, let me know if it doesn't work
Based on your comments look at creating separate calculations for to YoY / MoM / etc calculation. That also means creating calculated fields to isolate the Current Month, Previous Month, etc.
For example, the current month:
[isCM]: DATETRUNC('month',[YourDateField]) = DATETRUNC('month',TODAY())
The previous month:
[isPM]: DATETRUNC('month',[YourDateField]) = DATETRUNC('month',DATEADD('month',-1,TODAY()))
Then month on month, something like:
[MoM]: (SUM([Measure])*INT([isCM]))/(SUM([Measure])*INT([isPM]))
To make your table check this article about using the placeholder technique to create tables in Tableau

how to get number of days from relative dates filter?

I want to get number of months user selected using Relative Dates Filter?
If user entered "Last 3 Months" then I wants to get number of days for 3 months to use these days in another calculated field.
Calculated field which is using this is as follows.
([Total Sale within Entered Date]/[Number of Days])*365
which returns me predicted sales for the whole year.
Here, [Number of Days] is the filter which I am using. Picture added for Relative Dates filter I am using.
Screenshot of Relative Date Filter:
You need to create a calculated field that works out the number of days in the data.
Something like datediff('day',min([dates]),max([dates])) if data is present for all the days in the time period will work. This calculation will dynamically give you the number of days between the first and last date in the [dates] field for whatever data is selected by the filter (so won't be precise if not all the possible dates are represented in the dataset).

Extract highest date per month from a list of dates

I have a date column which I am trying to query to return only the largest date per month.
What I currently have, albeit very simple, returns 99% of what I am looking for. For example, If I list the column in ascending order the first entry is 2016-10-17 and ranges up to 2017-10-06.
A point to note is that the last day of every month may not be present in the data, so I'm really just looking to pull back whatever is the "largest" date present for any existing month.
The query I'm running at the moment looks like
SELECT MAX(date_col)
FROM schema_name.table_name
WHERE <condition1>
AND <condition2>
GROUP BY EXTRACT (MONTH FROM date_col)
ORDER BY max;
This does actually return most of what I'm looking for - what I'm actually getting back is
"2016-11-30"
"2016-12-30"
"2017-01-31"
"2017-02-28"
"2017-03-31"
"2017-04-28"
"2017-05-31"
"2017-06-30"
"2017-07-31"
"2017-08-31"
"2017-09-29"
"2017-10-06"
which are indeed the maximal values present for every month in the column. However, the result set doesn't seem to include the maximum date value from October 2016 (The first months worth of data in the column). There are multiple values in the column for that month, ranging up to 2016-10-31.
If anyone could point out why the max value for this month isn't being returned, I'd much appreciate it.
You are grouping by month (1 to 12) rather than by month and year. Since 2017-10-06 is greater than any day in October 2016, that's what you get for the "October" group.
You should
GROUP BY date_trunc('month', date_col)

Show values of measure names from previous week and make difference?

My sheet has measure names divided by ops managers and managers filtered to show values only for current week. What I need is to make new column for each of the measures to show values for previous week and then make %diff. I have seen solutions by putting week in row but is it possible this way somehow?
What I tried:
LOOKUP(sum([missed hours]),-1) (shows values from previous row)
{ FIXED DATEPART('week',[metric_date]),[ops_login_name],[supervisor_login_name]:sum([missed hours])} does nothing, same values as normal
I even tried separate calc field DATEPART('week',[metric_date])-1 that shows previous week number and then { FIXED [previous_week],[ops_login_name],[supervisor_login_name]:sum([missed hours])}
You can do this by filtering for the desired week in the Measure Value and not in the filter shelf. Create two boolean expressions that isolate your week.
Is this week? : datetrunc('week',{max([metric_date])}) = datetrunc('week',([metric_date]))
Is last week? : dateadd('week',-1,datetrunc('week',{max([metric_date])})) = datetrunc('week',([metric_date]))
Then create week specific measure values with sum(if [Is this week?] then [total_miss_cnt] end)
Repeat for previous week and then you can do a % diff. Make sure to remove and date filtering from the filter shelf.

How to return the last day of each month

I am creating a view where i have multiple records showing up for each month.
Example: January is showing 20 records and February is showing 30 records. I only want the last record of EACH MONTH to show up. So i want to see 1 record for each month.
I have already tried the first() and last() table calculation, but it does not filter by month, but by column.
If you just apply first and last on the dataaset then you won't get the correct output, If you need for every month then you need to divide the partition to year and month and then apply the max on the specific partition.
Try this way:
Place the order date in Exact date format and change the property to discrete
Now extract the year and month in separate calculated fields and place in detail.
Year:
year(Date)
Month:
Month(date)
Now create one more calculated field and write below code:
WINDOW_MAX(MAX([Order Date]),FIRST(),LAST())
Try This
1 Calculated Field - DateMonth
Datetrunc('month',Date)
2 Calculative field - LastDate
{ FIXED DateMonth: max(Date)}
3 Calculative Field- Filter
if lastDate=Date then 'Yes' else 'No' end