Calculate median sales price with using 3 variables Tableau 10 - tableau-api

I would like calculate the median sales price and the median rental price for an apartment in NYC in each of the 5 boroughs, Brooklyn, Bronx Manhattan, Queens and Staten Island. In Tableau the sales and and rentals are groups of ListPrice -- Variables ListPrice is NUMBER(decimal) Type (includes Sales & Rentals, Borough
Any help is appreciated
I tried using Tableau's table calculation feature but that did not work, I tried
WINDOW_MEDIAN(SUM([ListPrice])-1, -1)
ERROR: WINDOW_MEDIAN is being called with (float, integer), did mean
(float,integer,integer)
Data
Type Borough ListPrice
RentalType1 Manhattan $5,000
RentalType2 Bronx $3,000
RentalType2 Brooklyn $3,000
SalesType2 Manhattan $900,000
SalesType1 Brooklyn $100,000
SalesType1 Bronx $500,000
SalesType2 Queens $800,000
SalesType2 Staten Island $400,000

Table calculations takes 3 arguments, Expression, First row of the partition and last row of the partition. In your formula you haven't given last row of the partition.
Run the function for type in each Borough and calculate for each Borough.
So your formula would be:
WINDOW_MEDIAN(SUM(INT([List Price])),FIRST(),LAST())
are you looking to get values below:
Here calculation2 is median value

Related

High Value at Each Granularity

I have an table below which shows some data and I am trying to get the high value from each row.
Below is the data I have:
Table A
Once calculated it should looks like below in tableau
Table B
Notice that for Total, its not summing up from pers and bus instead its getting it highest value from total from table A and same concept is for Grand Total. Numbers which you see are balance.
I am able to get high value up to Total but its the grand total where I am struggling with. Below is my Calculation which I am using.
if
countd([Category]) = 1 then
sum({ FIXED [Group], [Category]: max(
{ FIXED [Group], [Category], [Date]: SUM([Balance])})})
ELSE
sum({ FIXED [Group]: max(
{ FIXED [Group], [Date]: SUM([Balance])})}
)
END
Step-1: For max of categories, use this calculation desired maximums
MAX({Fixed [MVRA Group], [Product Category], [Date Display]: ([Primary Measure])})
STEP-2: For max of subtotal, use desired sub-total maximum
MAX({Fixed [MVRA Group], [Date Display]: ([Primary Measure])})
https://drive.google.com/file/d/1zyjCDdG_QECrkFgY-UFm7WM2yN1xaHmZ/view?usp=sharing

PostgreSQL product table and sales table

I have the following problem.
I need all the products in a table and at the same time put the sum of the quantities sold of each product.
I need to see each product in the product table with the total sum of sales, according to the date range.
If there is no sales record in that range, it must be zero.
My query is as follows, but it doesn't work.
Product Table : sto_producto
Product sales movement table: sto_movdet
SELECT
sto_producto.pro_codprod AS cod_product,
sto_producto.pro_desc AS name_product,
sum(sto_movdet.mvd_cant) AS total_sale,
AVG(sto_movdet.mvd_costo) AS cost_product_sale
FROM sto_producto
INNER JOIN sto_movdet ON (sto_producto.pro_codprod = sto_movdet.mvd_codprod)
WHERE mvd_fecha BETWEEN '2020301' and '20200716'
GROUP BY pro_codprod, pro_desc
I expect a result similar to
cod_product name_product total_sale cost_product_sale
0004 mousered 45 $ 2.355
0071 pc laptop 0 $ 1.000

Iterate over current row values in kdb query

Consider the table:
q)trade
stock price amt time
-----------------------------
ibm 121.3 1000 09:03:06.000
bac 5.76 500 09:03:23.000
usb 8.19 800 09:04:01.000
and the list:
q)x: 10000 20000
The following query:
q)select from trade where price < x[first where (x - price) > 100f]
'length
fails as above. How can I pass the current row value of price in each iteration of the search query?
While price[0] in the square brackets above works, that's obviously not what I want. I even tried price[i] but that gives the same error.

MDX How to add bands to grouped fact table

I'm trying to create income bands on my fact table.
The fact table that consists of
Date, TranType, AdvKey, Amount
01/01/2015 TRAN1 ID01 5000
01/02/2015 TRAN1 ID02 13000
01/03/2015 TRAN2 ID01 500
The AdvKey links back to DimIFADetails, this table has a ParentKey, a Parent Child hierarchy. There are two levels, Adviser and Adviser Group
ADVKEY PARENTKEY
----------------
ID01 ID03
ID02 ID03
ID03 NULL
I want to be able to group on AdvKey or AdvGrpKey and have the summed amounts (income) allocated to income bands.
So when I group on Adviser, each adviser will be assigned an income band, ie 0-10k, 10-50k, 50-100k, same applies when the grouping is changed to the Group Level.
In the above
ID01 5300 0-10000
ID02 13000 10000-20000
or if by Adviser Group
ID03 18300 10000-20000
In SQL, I could group by the key necessary, then add a column and case statement to allocate each aggregated amount to a band.
The facttable has about 2 million rows.
The income is how much is generated from the advisers and adviser group, so this will change over time.
I can't think of any way to do it with a dimension, as there is no way to link back to the fact table seeing as these are groupings done at run time.
I now have something that works
with
MEMBER Measures.[Calc Sum] as
IIF( Sum(EXISTING [Dim IFA Details].[Parent Key].[Adviser Group].Members,
Measures.[Amount] * -1
) <= 10000 , '0-10000', IIF( Sum(EXISTING [Dim IFA Details].[Parent Key].[Adviser Group].Members,
Measures.[Amount] * -1
) <= 20000 , '10001 - 20000' , '>20000' ))
SELECT { [Measures].[Amount] , measures.[Calc Sum] }
ON COLUMNS,
NONEMPTY( [Dim IFA Details].[Parent Key].[Adviser Group].Members , [Measures].[Amount])
on rows
FROM [Income and Emails Cube]
Its going to have a rather bulky IIF when I add more bands.
Is it possible to create a dimension with the categories and add the keys on the fly like this?
or would there be no benefit.

Rank values in a dataset row

I have a dataset of stock prices called 'stocks'. Each column is a different stock. Each row is the date of the stock prices.
How can I rank the stock price of a given date?
I tried
tiedrank(stocks.yhoo)
And it successfully ranked the prices of YHOO stock. However, I would like to rank by row, not column.
Also, when I tried
tiedrank(stocks(1,:))
or to delete the date column in column 1
tiedrank(stocks(1,2:3))
I got the error message: Dataset array subscripts must be two-dimensional.
Am I doing something wrong? Or am I better off using matrices?
If I understand correctly, you want to rank the stocks according to price at a given date, where dates are rows, and stocks are columns. To use tiedrank across a row, you need to convert that part of the dataset to double, and then use the output index list to sort:
%# create index for sorting
idx = tiedrank( double( stocks(1,:) ));
%# reorder columns with index
sortedStocks = stocks(:,idx);