简体   繁体   中英

Last 7 days data calculation in Tableau

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM