Rolling 7 Day Periods in Tableau

Viewed 1999

I’m trying to figure out how to create rolling 7 day periods. I have horizontal bar charts that are currently grouped by week, but I actually want them to be grouped by 7 day periods instead.

I used the Superstore data as an example. The top row in this image should be the last 7 days, with the latest date being in the top right, and so forth. Is this possible?

Text

1 Answers

You can create a field to use as a dimension for this assuming you choose some anchor date to specify the beginning of a 7-day period. For this example, I’ll choose a literal date of July 4, 1776 but you could use the Today() function or the min or max transaction date in your data or whatever you like.

Then the DIV() and DATEDIFF() functions are all you need to calculate the number of 7 day periods Between the anchor date and the period containing [Order Date].

DIV(DATEDIFF('day', #7/4/1776#, [Order Date]), 7)

You’ll typically want to treat this field as a discrete dimension.

In the example below, I called the field [Period] and also displayed the earliest and latest dates found in the data for each Period.

enter image description here

In a more production oriented example, you may want to hide the header for the Period field and instead calculate the start/end dates for periods rather than draw them from the data, so that they show correctly if there were period boundaries without any transactions.

The formula to compute the [Period Start] is DATE(DATEADD('day', [Period] * 7, #7/4/1776#)) and to compute [Period End] is DATE(DATEADD('day', [Period] * 7 + 6, #7/4/1776#)) If you are going to use the anchor date in many formulas, it would be convenient to define it in one place with a parameter or calculated field.

Related