Tableau - weekly average from daily data - tableau-api

Using Tableau 8.1.
Daily data like this:
Date Sales
1-1-14 $5
1-2-14 $2
...
6-15-14 $15
Which can be aggregated to weeks of course
Week 1 $15
Week 2 $12
Week 3 $10
Week 4 $13
etc.
I would like to get the weekly average of each month
So like this:
Weekly Average
Month 1 (weekly average of the weekly total sales) ($15+$12+$10+13)/4 = 12.5
Month 2
Month 3
Does that make sense? If ou simply put "average" in the tableau table, it gives the average daily value PER WEEK (or month) < - I don't want that. I want the average of the weekly totals per month.

Quite simple, actually.
Create these calculations
Unique Weeks = COUNTD(DATEPART('week',[Date]))
Weekly Average = sum([Sales])/[Unique Weeks]
Then simply report Weekly Average x Month (or Quarter or Year, etc.)
(Note: Some weeks have < 7 days).

This can be done very simply with one calculated field.
WINDOW_AVG(SUM([Sales]))
Then drag your Date pill onto the rows shelf twice, one aggregated to day, the other to week. Make sure pills are set to discrete (blue) and drag your Sales measure onto the shelf, also as discrete.

Related

Trying to Average number of accounts by hour, day of week, and month

I'm in healthcare and we're trying to assess the number of discharges we have per hour of day, but we'd also like to be able to filter them down by day of week, or specific month, or even a particular day of week in a particular month (e.g. " what is the average number of discharges per hour on Mondays in January?")
I'm confident that Tableau can do this, but haven't been able to make the averages show up in my line graph... every time that I convert it from COUNT to AVG, the line simply goes straight. I got close when I did a table calculation to find the Average (dividing the count per hour by the number of days captured in the report), but when I add a filter for either the month or day of week, selecting one of the options of the filter reduces the total number that is being counted, rather than re-averaging the non-filtered items. (i.e. if the average of the 7 days of the week is "10" for a particular hour, and I deselect the first three days of the week, it's now saying that my average for that hour is roughly 6, despite the fact that all of the days are very close to 10 at that hour.)
Currently, my data table has the following columns:
Account#/MonthYear/HourOfDay/DayOfWeek
ex.12345678/ Jan-17 / 12 /Sunday
I would just create a few calculated fields to differentiate the parts of the calendar you might want to filter/aggregate on. Mixing the month and day of the week with filtering is pretty straight forward with the calculated fields. Then do standard summing to get what you are looking for because an average count of records is always one unless you are throwing some other calculation into the mix. I threw a quick example up on Tableau Public for you to get the idea.

Average Volume Every Weekday In a Month Tableau

I am trying to calculate an expression in Tableau 10.2 for a contact center use case.
I have calls at every hour all days of the week.
I need to calculate the average call per hour per day. For instance i need to calculate the average call every Sunday at 9 pm and use that in tool tip.
By this i am measuring how many average calls we get on a given Sunday or any other day of the week at a particular hour.
I had written an expression sadly that is not resulting in accurate number
AVG({ FIXED [Weekday Flagging],[Hour]:
AVG([Volume])})
Do suggest.
Very Recently I had a same requirement to see avg visitors on the hours of all the days of a week. My requirement was (Total No. Of Visitors on Sundays/No.Of Sundays) and then (Total No of Visitors #hour/No. Of Sundays).
1)create a calculated field : sum(volume)/countd(date) and name it 'Avg Volume'
2)drag date to 'Columns'. From the drop down click 'More' -> 'Weekday'
3)drag the calculated field to rows. This will give avg volume on each days of a week.
To get Avg Volume of hours
1)do the same procedure as above and instead of 'Weekday', drag Hour of the day to the column.
Link these 2 sheets using an action filter, so that if u click on sunday in the first sheet, it will show the avg volume of each hour of sunday in the second sheet.

Use Number of Values selected in a Filter inside a Calculation Field

In Tableau, I have a Date Filter with 31 days (days in a month).
I have a calculation,
Sum(sales)/(No of days)
Based on no of days selected in the Date Filter, my calculation should change.
Eg : If 12 days are selected in filter
The Calculation should be Sum(sales)/12
If 20 days then Sum(sales)/20.
Regards
Try this formula
sum(Sales) / datediff('day', min(Date), max(Date))
Just realize that if you have missing data at the start or end of your period, say no entries at all for the first few days of the month, it will only start counting days at the very first day of data, i.e. min(Date)
For those reasons, it is often useful to pad your data to have at least one row per day, even if it most of the fields are null in the padded data row. You can use a union or left join to do that without disturbing your original data.

Tableau: Calculate Monthly and Yearly Averages from Days

I have a few years of data by day that looks like this:
Date Count
1/1/2015 1000
1/2/2015 1010
1/3/2015 1040
I would like to create a calculation that allows me to switch between Day, Month and Year using Tableau's date formats without having to calculate the average for each individual format. For instance, the monthly average for Jan 2015 would be the total of Count for all 31 days in January 2015 divided by 31.
Since you have no missing data (days) and no duplicate data, the solution is extremely easy.
Place the Count field on any shelf (except the filter shelf). For example, place it on the Rows shelf. Click on the Count field on the Rows shelf and change the aggregation to AVG(). You should see the AVG(Count) of all your data rows at this point.
Place the Date field on any shelf (except the filter shelf). For example, place it on the Columns shelf. Click on the Date field pill on the Columns shelf, and choose the level of granularity you wish from the SECOND batch of choices. (The first batch returns date parts, not dates) For example, choose Month. You'll see the Avg(Count) per month.
Place any other dimensions on shelves to either filter to specific dimension values or to show the average counts broken down by your other dimensions.
Users can drill up or down the granularity of your dates by clicking the + or - icons by the date axis, or by clicking on the Date pill. You can choose to display the dates as a continuous or discrete value.
If you don't like the +/- UI, then you can create a string valued parameter with values: year, quarter, month, day etc, and then create a calculated field using the date_trunc() function to allow, say, a dashboard user to to choose the date granularity from a different type of UI control.

Dynamic weekly average per category

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.
Add a new dimension, which is for the weeks
You can then create a variable which calculates the average amount for a specific week as follows:
{FIXED [Date (Week numbers)], [Category]: avg([Amount]) }
Then when you want to average you can average the above formula
AVG({FIXED [Date (Week numbers)], [Category]: avg([Amount]) })
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.
Based on Mark Andersen's solution I found the following:
create a calculated field WeekNumber:
DATETRUNC('week', [Date])
create a calculated field WeekTotal:
{FIXED [WeekNumber], [Main Category]: SUM([Amount Person]) }
create a calculated field WeekDiff:
DATEDIFF('week',#2015-08-01#,TODAY())
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.