0
votes

I have a dataset as follows:

DATE       | AMOUNT | CATEGORY
20.12.2015 | 100.00 | Drinks
22.12.2015 | 50.00  | Food
20.12.2015 | 70.00  | Transport
07.12.2015 | 50.00  | Transport
...

There are several records with amounts spent per week and day.

I would like to have a bar chart with the categories on the left and the length of the bars indicating the weekly average, ie. what is spent on average per week during a filtered time frame

If I user the normal AVG([AMOUNT]) it calculates the daily average, rather than the weekly one.

I found this question: Tableau - weekly average from daily data

However one of the answers is not dynamically, the other lists averages for consecutive weeks, rather than per category and I can't think of a way to apply the same technique for mmy problem.

3
sample output would be useful to test.vhadalgi

3 Answers

2
votes
  1. Add a new dimension, which is for the weeks
  2. You can then create a variable which calculates the average amount for a specific week as follows: {FIXED [Date (Week numbers)], [Category]: avg([Amount]) }
  3. Then when you want to average you can average the above formula AVG({FIXED [Date (Week numbers)], [Category]: avg([Amount]) })
0
votes

First make sure the data type for the field named DATE is type date instead of string. If not, change the data type from the right mouse menu or worst case use the date parse function in a calculated field.

Second, after you place the DATE field onto a shelf, set the date level of granularity to Week. Again ], use the right mouse context menu. Choose from the second batch of choices to truncate dates to the week level. The first batch of options on that menu are date parts, not dates. You may want to then change the field to discrete depending on your intended view.

0
votes

Based on Mark Andersen's solution I found the following:

  1. create a calculated field WeekNumber:

    DATETRUNC('week', [Date])

  2. create a calculated field WeekTotal:

    {FIXED [WeekNumber], [Main Category]: SUM([Amount Person]) }

  3. create a calculated field WeekDiff:

    DATEDIFF('week',#2015-08-01#,TODAY())

  4. create a calculated field WeekAvg:

    [WeekTotal] / [WeekDiff] Use WeekAvg as the meassure for the bars and it's done.

A few remarks for that: Mark's solution went int he right direction. I had to replace avg([Amount]) with sum([Amount]) since I want to have the total per week and average it afterwards. However it didn't exactly calculate what I wanted since Tableau only calculates averages based on the weeks that have a spending. If I have

40$ in week 1  
20$ in week 2 
30$ in week 4

then it calculates (40+20+30)/3 = 30 while I would like to have (40+20+30)/4 = 20.25 In my use case my solution works because I have a fixed time frame until TODAY(), however it would be conviniant if that would be calculated automatically if I use a filter between two arbitrary dates.