How do I make a calculated field for total cost by quarter and location in Tableau? - tableau-api

I have Tableau 9.1 and have the costs per transaction in a column over the course of 3 years. I also have the dates and locations for all the transactions.
How do i make a calculated field for total cost at a location for a given quarter assigned to each observation?

Generally that depends on how you want to display these values.
An easy way would be via a LOD calculation:
{fixed DATETRUNC('quarter',[Date]), [Location]: SUM([Cost])}
This calculates the SUM([Cost]) for each quarter of [Date] and each [Location]

Related

Why are my values multiplying when I apply Month/Year to my values?

When I apply Month/Year to Cases or Deaths from my data, the values explode. For Cases it goes from approximately 48 million to over 1 billion, and for Deaths it goes from about 700 thousand to over 22 million. However, when I try the same thing with Initial Claims or the Stringency Index, my values remain correct. I'm trying to find the month over month percentage change by the way. And I'm using the Date column. I only select 2020 and 2021 in the filter for Year.
What I'm asking about is Sheet 21.
Link to workbook: https://public.tableau.com/app/profile/nilajah.rivers/viz/CoronaVirusProject_16323687296770/Sheet21
Your problem is that the data points are daily cumulative deaths. If you change the date aggregation to anything other than days, Tableau will default to summing the numbers for all the days in the month. This will give the wrong result, obviously.
If you want to show the correct total deaths or cases regardless of the time aggregation (months, days, weeks etc.) then you could use the New Case or New Death numbers plus a running sum table calculation. This will always give the correct total for the time period.
Table calculations will also allow automatic calculation of the period to period % change from the same data fields.
This is a common problem when working with datasets that offer pre-calculated aggregations. Tableau doesn't need that as it can dynamically calculate the aggregation of a field over any given time period but it is easy to forget which field has pre-aggregated data and which has raw data. Pre-aggregated fields assume a particular time period and can't be used for different time periods without disentangling that assumption (which is unnecessary if you also have the raw data (in this case daily new deaths/cases).

DAX Calculate Monthly Average

I am trying to create a measure to calculate a monthly average from a set of data that was collected every 15 minutes. I am newer to DAX and am just unsure how to intelligently filter by month without hard setting in the month ID #. The formula I am trying is:
Average Monthly Use:=AVERAGEX(VALUES('Lincoln Data'[Month]),[kWh])
Where kWh is a measure of the total usage in a column
Thanks in advance
DVDV
To get the monthly average usage, You need to sum up the total usage per user and divide by the total number of months for that user.
Without knowing what your tables look like, it's hard to give a very good formula, but your measure might look something like this:
= DIVIDE(SUMX(DataTable, [kWh]), DISTINCTCOUNT(DataTable[Year-Month]))

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.

Aggregating data from the US stock market in Tableau, using different time frames

I am a very basic user of tableau and I have not found an answer to my question.
I have a txt file that has historical daily data for 98% of all the stocks in the US, with their daily capitalization. Each stocks has its TICKER, Daily Market Value for every trading day of the year, and its SECTOR.
I did a simple time series that display SUM([Mktval]) (sum of all individual market values) across all stocks, on a daily daily, and where I can see that the total value as of 2016 is about 24 Trillion USD, as in the image below.
When I change the view column from DAY to YEAR, I don't see the right values, but something a lot larger. So I realized that I need to do SUM([Mktval])/252 to get the right value for a year (there are 252 trading days in a year).
If I change the view to MONTH, as in the chart below, the numbers are again wrong because 252 is not the right value to use in the division.
Is there any way that Tableau can adjust the values automatically to reflect the AVG MktVal across different time intervals?
Thanks
Replace SUM(Mktval) on the Rows shelf with the following calculated field
avg({ fixed day(Date1) : sum(Mktval) })
That solution is all in one step. It is perhaps a bit more clear to use 2 steps. First, create a calculated field called total_daily_market_value defined as
{ fixed day(Date1) : sum(Mktval) }
Then make sure that calculated field is a measure. It is an LOD calculation that you can think of as a separate table with one value for each day showing the total market value for that day.
Drag that measure to a shelf, and then change the aggregation function to AVG(), MEDIAN(), MIN(), MAX() or STDEV() as desired. Tableau will aggregate the total_daily_market_value using your chosen aggregation function for whatever values of Date1 are in your view.

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.