Last 7 days data calculation in Tableau - tableau-api

I am having an issue in calculating last week data in Tableau. Below is my scenario:
In my dashboard, I have a slider which selects the date. In my table, I have a list of users where I will be showing each of them's call records. One column will have last week records and one will have total records.
For Total records, there is not an issue. But for finding last week's count, I need to have a calculated field, that needs to subtract 7 days from the date selected and then give out the number of records for each user.
Say I have selected date as 25-04-2017, then my table should show all the records until 25-04-2017 in one column and other should show data from 18-04-2017 till 25-04-2017.

You can filter it with Relative days. When adding your dimension (date type) to the Filters list the picture below will appear.
Now you can click on the relative date and to choose the best option for you. You can see it in the picture below.

Create a Date parameter for your user to select. Using the Superstore data set, I created a calc field for last 7 days sales:
if datediff('day',[Order Date],[date]) <= 7 and datediff('day',[Order Date],[date]) >= 0 then [Sales] end
And sales up to date:
if datediff('day',[Order Date],[date]) >= 0
then [Sales] end
See attached example: https://www.dropbox.com/s/nqdp9zj74jay72d/170427%20stack%20question.twbx?dl=0

I was able to find the solution for my issue.
I created a boolean field as Max7Days with the formula as below:
DATEDIFF('day', [Date] , {MAX([Date])} ) <= 7
And created another that would count the number of records for the last 7 days if the condition was true as per the below formula:
CASE [Max7Days]
WHEN TRUE
THEN
[Number of Records]
END

Related

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).

Difference between two values - same Dimension, same Measure

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

tableau to show both last and previous 12 month

In one worksheet, to display the measure value (Sales) of Last 12 Months(LTM) and Previous 12 Months(PTM) from the selected month.
Current Month is 2018-June , By default selection
PTM date range for the sales period as (2016-June to 2017-May)
LTM date range for the sales period as (2017-June to 2018-May)
If user selects different month from the drop down, let's select (Apr 2018)
PTM date range as (2016-Apr to 2017-Mar)
LTM date range as (2017-Apr to 2018-Mar)
This can be done using a parameter. Here are the steps involved:
Create a date parameter
Create a calculated field as below(You might need to fine tune it to match your exact requirements)
Add the calculated field to filter card to show only 'SHOW'

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

Grails Model Count Number of occurence group by Date month, hour

I have a Grails Domain that contain two date feild
Date updated
Date created
I want to count number of rows based on updated time with respect to month or hour or year. How can I do that. The methods showed on Internet does not work. I am using grails version 2.4.4.
Update:
Example
I have a domain that contain four feids
Date Created
Date updated
Long id
Long idx
I want to find how many idx is there in every group when i group by hour. Like there can be 5 in 12h and 6 in 13th hour... so on.. How can i find those.... My comaparison is not fixed. It can be changed to mont or year... In year it will show the number of idx in each year. in month it will show the number of idx in each month..
How can I do this with grails...
You can use where detached query:
DomainClass.where { year( updated ) == 2014 }.count()
Similarly, hour(), minute() etc.
Refer WHERE queries for details.