A ratio measured within one dimension shown across another dimension - tableau-api

For the sake of the exercise, let's assume that I'm monitoring the percentage of domestic or foreign auto sales across the US.
Assume my dataset looks like:
StateOfSale | Origin | Sales
'CA' | 'Foreign' | 1200
'CA' | 'Domestic' | 800
'TX' | 'Foreign' | 800
'TX' | 'Domestic' | 800
How would I show the percentage of Foreign Sales, by State of Sale, but each State is a line/mark/bar in the visual?
So for CA, the Foreign Percentage is 60%. For TX, the Foreign Percentage is 50%.

This is what Tableau was born to do!, and there are a lot of great ways to visualize this type of question.
Use a Quick table calculation called "Percent of Total" and compute that percentage according to each State. In the picture below, "StateofOrigin" is in Columns, and "Sum(Sale)" is in Rows, I compute using Table (Down).
You can also graph the raw sales numbers in addition to displaying the text percentage to gain additional context about the number of sales between states.
Finally, if you've got a lot of states, it can be cool to plot it out on a map. You can do this by creating a calculated field for percentage and then filtering out the domestic sales.
Field Name: Percentage
SUM([Sale])/SUM({FIXED [StateofOrigin]: SUM([Sale])})

Related

Custom aggregation in Tableau when joining two different types of tables

I'm fairly new to Tableau, but otherwise pretty well versed in data and SQL, etc.
I'm working on a dashboard that will display some financial data. The two main categories are "Actual" results and "Plan" (or budget).
The Actual results are at the individual transaction level by cost center and by account. The Plan is at the cost center and account level. For example:
Actual table:
Cost Center | Account | Amount
______________________________
ABC 10023 500
ABC 10023 600
BAC 10023 250
BAC 10023 300
Plan table:
Cost Center | Account | Plan Amount
___________________________________
ABC 10023 2000
BAC 10023 2300
When joining these two tables together in Tableau, the Plan Amount gets repeated at the transaction level (obviously, as it's a straightforward LEFT JOIN), which in this example for Cost Center ABC would aggregate to a total of 4000, and for BAC it would aggregate to a total of 4600.
What I'm hoping to somehow accomplish is for the Plan Amount to only be aggregated at the cost center level, not at the transaction level. In the actual tables I'm using, the Cost Center rolls up through a department hierarchy, i.e. a department can have multiple cost centers, etc, and when I show results at a department or division level it really blows up the Plan Amount numbers, they're vastly overstated.
Is there some way to accomplish this in Tableau? In SQL I'd probably use a window function of some sort to divide the Plan Amount by # of transactions per Cost Center so that it would aggregate correctly (though this would lead to some rounding errors, but not that material). But don't know how to do this in Tableau.
When joining it will be put against the transaction level, as with any normal inner join. Therefore you have options on how to handle this.
You could do it using an aggregation - i.e. instead of SUM you could MIN / MAX / AVG the field, which would always return the same value.
Alternatively you could blend this data, rather than join. If you blend in the visualisation layer and only blend it when showing the Cost Centre level, this will prevent aggregating against all transactions. To blend this would be a completely separate data source. It would "blend join" in Tableau - see Data - Edit Blend Relationships in Tableau.

Using RANK function in Tableau on a Combined column. However, display individual columns as well

I am using RANK function in Tableau and I am displaying the Rank of calculated measure (Eg: 1 to 50)
The calculated Measure I have is Total Amount for Combined Periods.
When there is no Period displayed on the dashboard, the Total Amount is the sum of both periods and this is exactly what I want. I am good in this case.
However, When I want to display the Period in the Rows, the Total Amount changes to "Total Amount for Period 1 and Total Amount for Period 2".
How can I add a different axis to show Individual Periods as well as Rank of Total Amount for Combined Period?
I guess this might come down to Dual axis in Tableau and I believe this is not available yet and users are voting for this in Ideas.

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.

Different Total Types in Tableau

I am trying to use Tableau's row total function but am running into a challenge. In the same widget I have Rows 1 - 4 with Numbers. Row 5 is a percentage.
What I would like to do is have Rows 1 - 4 use a Sum Total and Row 5 use an Average total.
Any suggestions on how I can do this?
Thanks,
I don't believe you can use different total metrics on the same worksheet.
What you can do is to create 2 different worsheets, and bring them side by side on a dashboard. Then use the proper Total metric in each.
But beware on calculation average of percentages, because they might be twisted. Usually weighted average is required to accurately express the "average" of a percentage.
What you can do is to actually calculate the percentage (use a calculated field) via the division of two metrics. That way, when you do Totals you will actually a valid value for the "average" of the percentage.
As an exercise, suppose you have sales (in $) in first row, and # of clients in row 2. Now I create a calculated field called ticket, that is
SUM(sales) / sum([# of clients])
That way I can add that to a third row, and for each column I'll have the right number of ticket, and if I add a Row Grand Total, I'll get the actual average ticket value (that is total sales / total # clients), because Tableau will sum all sales, sum all # clients and them perform the calculation (the division)

How to sample from KDB table to reduce data before querying?

I have a table of tick data representing prices of various financial instruments up to millisecond precision. Problem is, there are over 5 billion entries, and even the most basic queries takes several minutes.
I only need data with a precision of up to 1 second - is there an efficient way to sample the table so that the precision is reduced to roughly 1 second prior to querying? This should dramatically cut the amount of data and hence execution time.
So far, as a quick hack I've added the condition where i mod 2 = 0 to my query, but is there a better way?
The best way to bucket time data is with xbar
q)select last price, sum size by 10 xbar time.minute from trade where sym=`IBM
minute| price size
------| -----------
09:30 | 55.32 90094
09:40 | 54.99 48726
09:50 | 54.93 36511
10:00 | 55.23 35768
...
more info http://code.kx.com/q/ref/arith-integer/#xbar