MDX for age range - range

I have a following MDX query:
SELECT {[Measures].[PARTICIPANT ID]} ON columns,
{[GENDER].[Female Gender]} ON rows
FROM [Dystonia DS]
I have a dimension called AGE IN YEARS and I want to filter PARTICIPANT ID using age range i.e PARTICIPANT IDs between AGE 20 to 54 etc.
I got solution for date range on this forum but unable to make it for age range by referring date range MDX.
Any help is greatly appreciated.

If it is really another dimension, and you don't want to display it, can't you just add it to the WHERE clause?
SELECT {[Measures].[PARTICIPANT ID]} ON columns,
{[GENDER].[Female Gender]} ON rows
FROM [Dystonia DS]
WHERE {[Age Range].&[20]:[Age Range].&[54]}
And if you need to see it, add it to the tuple in the ROWS dimension.
SELECT {[Measures].[PARTICIPANT ID]} ON columns,
([GENDER].[Female Gender],
{[Age Range].&[20]:[Age Range].&[54]}) ON rows
FROM [Dystonia DS]

Related

sum by groups in KDB

I have a PnL table with 3 columns. date, region, product.
I'm trying to group all PnL rows by region and product. One way that i've tried is to sum by region and product as following
select PnL : sum(PnL) by region, product from table where date within (d1;d2)
The issue I have is unexpected results. For a given date range (d1;d2) I'm getting the results I'm expecting. However for date range (d1;d2+1) I'm getting 0 everywhere.
I checked the data availability on the d2+1 and data is already available on that day.
Please note that the server is stateless and it is not possible to use intermediate results in variables.
What is the best way to achieve a grouping sum in KDB?

Changing a functional qSQL query to involve multiple columns in calculation KDB+/Q

I have a ? exec query like so:
t:([]Quantity: 1 2 3;Price 4 5 6;date:2020.01.01 2020.01.02 2020.01.03);
?[t;enlist(within;`date;(2020.01.01,2020.01.02));0b;(enlist `Quantity)!enlist (sum;(`Quantity))]
to get me the sum of the Quantity in the given date range. I want to adjust this to get me the sum of the Notional in the date range; Quantity*Price. So the result should be (1x4)+(2x5)=14.
I tried things like the following
?[t;enlist(within;`date;(2020.01.01,2020.01.02));0b;(enlist `Quantity)!enlist (sum;(`Price*`Quantity))]
but couldn't get it to work. Any advice would be greatly appreciated!
I would advise in such a scenario to think about the qSql style query that you are looking for and then work from there.
So in this case you are looking, I believe, to do something like:
select sum Quantity*Price from t where date within 2020.01.01 2020.01.02
You can then run parse on this to break it into its function form i.e the ? exec query you refer to.
q)parse"select sum Quantity*Price from t where date within 2020.01.01 2020.01.02"
?
`t
,,(within;`date;2020.01.01 2020.01.02)
0b
(,`Quantity)!,(sum;(*;`Quantity;`Price))
This is your functional form that you need; table, where clause, by and aggregation.
You can see your quantity here is just the sum of the multiplication of the two columns.
q)?[t;enlist(within;`date;(2020.01.01;2020.01.02));0b;enlist[`Quantity]!enlist(sum;(*;`Quantity;`Price))]
Quantity
--------
14
You could also extend this to change the column as necessary and create a function for it too, if you so wish:
q)calcNtnl:{[sd;ed] ?[t;enlist(within;`date;(sd;ed));0b;enlist[`Quantity]!enlist(sum;(*;`Quantity;`Price))]}
q)calcNtnl[2020.01.01;2020.01.02]
Quantity
--------
14

Tableau measure count items if between dates

What I am trying to achieve is to get a count of people employed in a particular period.
I have 3 variables:
Employee ID (integer)
Hire date (date)
Termination date (date or null)
Example
the formula I am looking for is something like
if termination_date is null
then
count employee_ID in
dates between Hire_date and max of either hire_date or termination_date
else
count employee ID in
dates between hire_date and termination_date
This aims to show the dynamic of staff level over the time.
I am new to Tableau, not sure how to even start with it. Any suggestions welcome.
This problem will be simpler if you reshape your data to have the following three columns
Employee ID
Date
Action. (where action takes on the values of ‘Hire’ or ‘Terminate’).
Each data row represents one change in status for an employees. If an employee had a termination date, they will have two records in this new format, otherwise just one record showing the hiring date.
You can reshape your data by hand, or leave the original and use Tableau Prep or the Tableau data source page to reshape using a self Union and a few simple calculated fields.
Define a calculated field called Staffing_Change as
if Action=‘Hire’ then 1 else -1 end
Now you can plot the change in staff level over time by putting exact date on columns and sum(Staffing_Change) on Rows. You can use a quick Table calc, Running Sum, to see the net staffing level. For line mark types, I’d use a step style by pressing on the path button on the Marks card. Otherwise, the chart can give the impression of fractional number of employees.

Group by month using cell above filtered cells

I have a spreadsheet that looks like this:
https://docs.google.com/spreadsheets/d/1b29gyEgCDwor_KJ6ACP2rxdvauOzacDI9FL2K-jgg5E/edit?usp=sharing
I have two columns I'm interested in, Date and Count. Every few dates, there will be a "TOTAL" line where all the Counts corresponding to that TOTAL will be summed.
I want an output that looks like the cells to the right, where all the TOTAL counts are summed according to month. The problem lies in that Column A has only the date or TOTAL, in separate rows, and this layout can't be changed, leaving me thinking I need to reference the cell directly above TOTAL in column A, which has the correct month I want to group that TOTAL by.
The reason why I can't just filter column A by date range is because of inconsistent use, where sometimes the count data is only entered in the TOTAL row.
I've scoured the internet exploring FILTER, INDIRECT, QUERY, SUMIFS, etc... but can't find exactly how to do this.
I can easily filter column B where A:A="TOTAL", but what I think I am needing to do after that is use each cell above where A:A="TOTAL" as a range for the month criteria, somehow using what I found here: https://exceljet.net/formula/sum-by-month, expressed by ">="&D3 and "<="&EOMONTH(D3,0).
Any help or alternatives would be appreciated. Thank you.
or a different (offset) approach:
=QUERY(FILTER({EOMONTH(INDIRECT("A1:A"&ROWS(B2:B)), 0), B2:B}, A2:A="total"),
"select Col1,sum(Col2)
group by Col1
label sum(Col2)''
format Col1'mmmm'", 0)
Query formula is great for these kind of situations but looking at it by month will introduce issues if you plan on looking at multi-year data:
=arrayformula(QUERY(QUERY({row(A:A),TEXT(A:A,"MMMM"),B:B},"SELECT max(Col1),Col2,sum(Col3) where Col3 is not null group by Col2 order by max(Col1) label Col2 'Month', sum(Col3) 'Count'"),"SELECT Col2,Col3"))
try:
=ARRAYFORMULA(QUERY({IF((B2:B="")*(A2:A=""),,VLOOKUP(ROW(A2:A),
IF(A2:A<>"total", {ROW(A2:A), DATEVALUE("01/"&MONTH(A2:A)&"/2000")}), 2, 1)),
IF(A2:A= "total", A2:A, ), B2:B},
"select Col1,sum(Col3)
where lower(Col2) = 'total'
group by Col1
label sum(Col3)''
format Col1'mmmm'", 0))

Creating a calculated field in Tableau using 3 columns

Date1 Date2 Line Item Total
May10,2009 May9,2009 10 40
May9,2009 May10,2009 20 10
May9,2009 May8,2009 20 30
May8,2009 May11,2009 30 0
This is my Table in Tableau. I want to create a calculated field for last column. If Date2 value has a match in Date1 Column, the new value is equal to the sum of all values corresponding to that that in Line Item column. Is this possible in tableau using calculation or in some way? example: 'May9,2009' in Date2 COLUMN has two occurrences in Date1 Column. hence the two values in Line Item column (20+20) is added and new calculated field shows 40.
Someone please help.
Maybe you can try doing a Join with the same table. Creating new rows matching Date2 with Date1 from a duplicated of the same table. Something like this (Hoja and Hoja12 are the duplicated sheets, my excel is in spanish):
And then calculate de SUM of each row aggregated by Date2. Then you can get a table like this one:
*For [Line Item] itself I had to use average because the data exists in multiple rows due to the join made, but in everyone is the same value. If you want a cleaner way of that maybe you can try using LODs.
Hope this helps.
(Perhaps another approach for the whole problem could be using the LODs, but that means a better comprehension of that kind of expressions and this method was the first one I thought about)