Tableau - Operations between two tables from different sources and different granularity - tableau-api

I'm using tableau Desktop V9.1.13 with two tables from different data sources (one comes from Bigquery and the other from a postgresql DB) and I want to perform a multiplication between two columns of these tables. The first table has one row for a single transaction and the other has one row for each currency available in the database. I've tried blending and create a calculated field but the last always throws an error. The first table looks like this:
+----------------+--------+----------+
| transaction-id | Value | Currency |
+----------------+--------+----------+
| 123-abc | 120 | BRL |
+----------------+--------+----------+
| 556-fds | 100 | PEN |
+----------------+--------+----------+
| 456-cde | 120000 | COP |
+----------------+--------+----------+
| 789-fgr | 100 | MXN |
+----------------+--------+----------+
And the other table
+----------+--------------+
| Currency | Value in USD |
+----------+--------------+
| COP | 0.0003 |
+----------+--------------+
| BRL | 0.3169 |
+----------+--------------+
| PEN | 0.2958 |
+----------+--------------+
| MXN | 0.0539 |
+----------+--------------+
Now I want to generate a new column called Value_USD that is the product of the transaction amount with the value of its currency.
+----------------+--------+----------+-----------+
| transaction-id | Value | Currency | Value_USD |
+----------------+--------+----------+-----------+
| 123-abc | 120 | BRL | 38 |
+----------------+--------+----------+-----------+
| 556-fds | 100 | PEN | 29.58|
+----------------+--------+----------+-----------+
| 456-cde | 120000 | COP | 40 |
+----------------+--------+----------+-----------+
| 789-fgr | 100 | MXN | 5.39 |
+----------------+--------+----------+-----------+

Related

how to show absolute and percentage of total (pane) in text table format in tableau

I'm new in Tableau, I have data like this
month | gender | sales | quantity |
Jan-2022 | male/female | integer | integer |
my goal is to create a table shown below in tableau.
|----------------------------------------------------------------------------|
| | Jan-2022 |
| | male | female |
|metrics | absolute | composition | absolute | composition |
|-----------------------------------------------------------------------------
|sales | 1000 | 33% | 2000 | 66% |
|quantity | 50 | 20% | 200 | 80% |
|----------------------------------------------------------------------------|
to create the composition for each metrics i use the percentage of total then choose compute using -> pane (across). but i have no idea how to put the percentage side by side in the table.

How to get non-aggregated measures?

I calculate my metrics with SQL and publish the resulting table to Tableau Server. Afterward, use this data source to create charts and dashboards.
For one analysis, I already calculated the measures per day with SQL. When I use the resulting table in Tableau, it aggregates these measures to SUM by default. However, I don't want to have SUM or AVG of the average or SUM of the Percentiles.
What I want is the result when I don't select date dimension and not GROUP BY date in SQL as attached below.
Here is the query:
SELECT
-- date,
COUNT(DISTINCT id) AS count_of_id,
AVG(timediff_in_sec) AS avg_timediff,
PERCENTILE_CONT(0.25) WITHIN GROUP(ORDER BY timediff_in_sec) AS percentile_25,
PERCENTILE_CONT(0.50) WITHIN GROUP(ORDER BY timediff_in_sec) AS percentile_50
FROM
(
--subquery
) AS t1
-- GROUP BY date
Here are the first 10 rows of the resulting table:
+------------+--------------+-------------+---------------+---------------+
| date | avg_timediff | count_of_id | percentile_25 | percentile_50 |
+------------+--------------+-------------+---------------+---------------+
| 10/06/2020 | 61,65186364 | 22 | 8,5765 | 13,3015 |
| 11/06/2020 | 127,2913333 | 3 | 15,6045 | 17,494 |
| 12/06/2020 | 306,0348214 | 28 | 12,2565 | 17,629 |
| 13/06/2020 | 13,2664 | 5 | 11,944 | 13,862 |
| 14/06/2020 | 16,728 | 7 | 14,021 | 17,187 |
| 15/06/2020 | 398,6424595 | 37 | 11,893 | 19,271 |
| 16/06/2020 | 293,6925152 | 33 | 12,527 | 17,134 |
| 17/06/2020 | 155,6554286 | 21 | 13,452 | 16,715 |
| 18/06/2020 | 383,8101429 | 7 | 266,048 | 493,722 |
+------------+--------------+-------------+---------------+---------------+
How can I achieve the desired output above?
Drag them all into the dimensions list, then they will be static dimensions. For your use you could also just drag the Date field to Rows. Aggregating 1 value, which you have for each date, returns the same value whatever the aggregation type.

How to use Tableau TOTAL() on WINDOWS_SUM()

In my tableau workbook, I have a calculated field - "Rolling 12 Month Sales" having the below formula and its working fine.
WINDOWS_SUM(SUM(Sales),-11,0)
Now , I am trying to achieve a Rolling 12 Month Sales % measure.
For this % measure, I am referring an existing calculation - SUM(Sales)/TOTAL(SUM(Sales)) - When this is cut on various segments, I get the percent distribution.
I am trying to get the exact same thing, on the Rolling 12 Month Sales % calculation. I tried the following but its not allowing to use a table calculation inside TOTAL()
WINDOWS_SUM(SUM(Sales),-11,0)/TOTAL(WINDOWS_SUM(SUM(Sales),-11,0))
Original Data
+--------+----------+----------+
| Month | Hardware | Software |
+--------+----------+----------+
| Jan-20 | 5000 | 7500 |
| Feb-20 | 6500 | 10000 |
| Mar-20 | 8000 | 10500 |
| Apr-20 | 11000 | 15000 |
| May-20 | 13500 | 21000 |
+--------+----------+----------+
Rolling 2 Months Sum Sales (This is working fine)
+--------+----------+----------+
| Month | Hardware | Software |
+--------+----------+----------+
| Jan-20 | 5000 | 7500 |
| Feb-20 | 11500 | 17500 |
| Mar-20 | 19500 | 28000 |
| Apr-20 | 25500 | 35500 |
| May-20 | 32500 | 46500 |
+--------+----------+----------+
Rolling 2 Months Sum Sales % - Below are the nos I am trying to achieve.
+--------+----------+----------+
| Month | Hardware | Software |
+--------+----------+----------+
| Jan-20 | 40.00% | 60.00% |
| Feb-20 | 39.66% | 60.34% |
| Mar-20 | 41.05% | 58.95% |
| Apr-20 | 41.80% | 58.20% |
| May-20 | 41.14% | 58.86% |
+--------+----------+----------+
Running out of options!
Best Regards
There should be no need to TOTAL a WINDOW_SUM. I suspect this could be solved with different Compute Using. But first I don't fully understand why you're taking the approach you are attempting. Any chance you could show some sample data with the results you expect along the way? For simplicity in the example it would be easier to do a rolling 2 periods rather than 12.

Create Calculated Pivot from Several Query Results in PostgreSQL

I have question regarding how to make a calculated pivot table from several query results on PostgreSQL. I've managed to make three queries results but don't have any idea how to combine and calculate all the data into a single table. I've tried to google it but found out that most of the question is about how to make a pivot table from a single table, which I'm able to do using sum, case, and group by. Well, Here's the simplified version of my query results
Query from query 1 which contains gross value
| city | code | gross |
|-------|------|--------|
| city1 | 21 | 194793 |
| city1 | 25 | 139241 |
| city1 | 28 | 231365 |
| city2 | 21 | 282025 |
| city2 | 25 | 334458 |
| city2 | 28 | 410852 |
| city3 | 21 | 109237 |
Result from query 2 which contains positive adjustments
| city | code | adj_pos |
|-------|------|---------|
| city1 | 21 | 16259 |
| city1 | 25 | 13634 |
| city1 | 28 | 45854 |
| city2 | 25 | 18060 |
| city2 | 28 | 18220 |
Result from query 3 which contains negative adjustments
| city | code | adj_neg |
|-------|------|---------|
| city1 | 25 | 23364 |
| city2 | 21 | 27478 |
| city2 | 25 | 23474 |
And what I want to to is to create something like this
| city | 21_gross | 25_gross | 28_gross | 21_pos | 25_pos | 28_pos | 21_neg | 25_neg | 28_neg |
|-------|----------|----------|----------|--------|--------|--------|--------|--------|--------|
| city1 | 194793 | 139241 | 231365 | 16259 | 13634 | 45854 | | 23364 | |
| city2 | 282025 | 334458 | 410852 | | 18060 | 18220 | 27478 | 23474 | |
| city3 | 109237 | | | | | | | | |
or probably final calculation which come from gross + positive adjustment -
negative adjustment from each city on each code like this
| city | 21_nett | 25_nett | 28_nett |
|-------|---------|---------|---------|
| city1 | 211052 | 129511 | 277219 |
| city2 | 254547 | 329044 | 429072 |
| city3 | 109237 | 0 | 0 |
Any suggestion will be appreciated. Thank you!
I think the best you can achieve is to get the pivoting part as JSON - http://sqlfiddle.com/#!17/b7d64/23:
select
city,
json_object_agg(
code,
coalesce(gross,0) + coalesce(adj_pos,0) - coalesce(adj_neg,0)
) as js
from q1
left join q2 using (city,code)
left join q3 using (city,code)
group by city

Tableau - Calculated field for difference between date and maximum date in table

I have the following table that I have loaded in Tableau (It has only one column CreatedOnDate)
+-----------------+
| CreatedOnDate |
+-----------------+
| 1/1/2016 |
| 1/2/2016 |
| 1/3/2016 |
| 1/4/2016 |
| 1/5/2016 |
| 1/6/2016 |
| 1/7/2016 |
| 1/8/2016 |
| 1/9/2016 |
| 1/10/2016 |
| 1/11/2016 |
| 1/12/2016 |
| 1/13/2016 |
| 1/14/2016 |
+-----------------+
I want to be able to find the maximum date in the table, compare it with every date in the table and get the difference in days. For the above table, the maximum date in table is 1/14/2016. Every date is compared to 1/14/2016 to find the difference.
Expected Output
+-----------------+------------+
| CreatedOnDate | Difference |
+-----------------+------------+
| 1/1/2016 | 13 |
| 1/2/2016 | 12 |
| 1/3/2016 | 11 |
| 1/4/2016 | 10 |
| 1/5/2016 | 9 |
| 1/6/2016 | 8 |
| 1/7/2016 | 7 |
| 1/8/2016 | 6 |
| 1/9/2016 | 5 |
| 1/10/2016 | 4 |
| 1/11/2016 | 3 |
| 1/12/2016 | 2 |
| 1/13/2016 | 1 |
| 1/14/2016 | 0 |
+-----------------+------------+
My goal is to create this Difference calculated field. I am struggling to find a way to do this using DATEDIFF.
And help would be appreciated!!
woodhead92, this approach would work, but means you have to use table calculations. Much more flexible approach (available since v8) is Level of Details expressions:
First, define a MAX date for the whole dataset with this calculated field called MaxDate LOD:
{FIXED : MAX(CreatedOnDate) }
This will always calculate the maximum date on table (will overwrite filters as well, if you need to reflect them, make sure you add them to context.
Then you can use pretty much the same calculated field, but no need for ATTR or Table Calculations:
DATEDIFF('day', [CreatedOnDate], [MaxDate LOD])
Hope this helps!