MDX IN Or SubQuery - sql

I am making a sales cube for a bank, I need to do a query with this Dimensions DimTiempo, DimCliente, DimCuenta
I need to find the number of customers who are in DimCuenta attribute [DimCuenta].[Planes].[Cue Nombre Plan] with value 'APORTES SOCIALES' And besides having another record in the DimCuenta with the attribute [DimCuenta].[Cue Codigo Aplicacion] with value 'L' also need to include in the result hierarchy [Ano Mes] of DimTiempo, I don't know to do this because I Don't know how to do an In or a subquery in MDX,

The best advise would be to take some time and read about MDX technology. There are a few principles that are different and need to be known.
I'd advise reading some introductions in MDX as this one. If you're more a reader there are some books on MDX. Sorry, long time I haven't read one can't advise.

Related

Using sql subquery in Tableau

I trying to calculate a new field in Tableau similar to a sub query in SQL. However, my numbers not matching up when I try to do this. I am stuck at this point and I am trying to see what others have done.
To provide reference, below is the subquery that I am trying to duplicate in Tableau.
select
((sum(n.non_influenced_sales*n.calls)/sum(n.calls))-(sum(n.average_sales*calls)/sum(calls))))/
(sum(n.non_influenced_sales*n.calls)/sum(n.calls)) as impact
from (select
count(d.id) as calls
,avg(d.sale) as average_sales
,avg(case when non_influenced=1 then d.sale else null end) as non_influenced_sales
from data d
group by skill) n
When I try to build the same calculation in Tableau, I am able to get the same results as long as I comment out the group by skill. However, when I try to group by skill, my attempts to match the number have not been working.
The closest I have come is when I try to fix the level of detail expression by using include. Tableau code:
(include [skill]:([non_influenced_sales]-[average_sales])/[non_influenced_sales]}
However, doing this or using fixed has not worked and I can't match the numbers I am getting from SQL.
FYI, Impact is an aggregated measure. I built the sub-query part in tableau by just creating separate fields for the calculation I needed. So for example
Non Influenced Sales calculated in Tableau:
avg(if [non_influenced]=1 then [non_influenced_sales] end)
However, I am not sure if this matters or not.
I have also tried creating custom sql. I am able to get a rolled up version using all of the dates correct. But when I want to get down to different dates/use other filters, things get messy real quick. I am trying to build relationships on a date level, but that hasn't worked either.
Is there an easier way to do this?

Syntax for OLAP calculated measure based on a dimension

Ok, so first off I apologize if this has been answered, but I've searched and after a few hours have given up. Also, I'm not a developer but rather a curious analyst who's trying to learn. That said, the short of what I'm trying to do is factor in a dimension filter when creating a calculated measure using OLAP extensions.
So I have a dimension called Call_Workgroup and within that dimension there are different workgroups, such as Sales, SalesSpanish, etc. What I'm trying to do is calculate Spanish Conversion and have the result in the same table as Total Conversion (with the y-axis as date and the columns would be Total Conversion, Spanish Conversion, etc). So for Total Conversion, I have the calculation ([Measures].[Orders] / [Measures].[Calls]) and I'd like to add the calculation for Spanish Conversion, ie Spanish Sales / Spanish Calls. For what it's worth, I'm using OLAP extensions rather than writing MDX queries. I'm decent with SQL queries, but haven't written any MDX queries yet.
I've tried ([Measures].[Orders],[Call_Workgroup].[CallWorkfroup].&[7] / [Measures].[Calls],[Call_Workgroup].[CallWorkgroup].&[7]) (where 7 is the ID of the Spanish workgroup), but keep getting a syntax error saying "the syntax for "," is incorrect."
So, any guidance? Thanks in advance.
Here is a quick introduction to MDX and calcultated members. I believe your issue is not using the tuple notation (i.e., parenthesis around the members compositing the tuples) :
( [Measures].[Orders], [Call_Workgroup].[CallWorkfroup].&[7] )
/ ( [Measures].[Calls], [Call_Workgroup].[CallWorkgroup].&[7] )
Hope that helps.

Using DateDiff in SSAS MDX

I would like to write a KPI in SSAS which gives me back the average of all the employee's age. The employee's Birth Date is in Dim_Employee. I read 3 books full of MDX date and time handling recommendations already, but neither one worked. With hours of desperate trial and error I tried countless combination to the solution without success.
The Birth Date is datetime(null) in the source database. The solution I tried is the following:
VBA!DateDiff("yyyy", now(), [Employee].[BirthDate].CurrentMember.Member_Value)
Of course I should use [Date].[Date].CurrentMember instead of now(), but for simplicity I used this.
In the Employee dimension, I created a ValueColumn with Date datatype. When I try to execute it in Management Studio, it gives me back the following error:
"The Axis0 function expects a tuple set expression for the argument. A
string or numeric expression was used."
When I do not use Member_Value, it gives back null, and DateDiff gives back -2010.
Because I'm not responsible for the cube's structure where I would like to write this KPI I search for a solution which does not require new Measures, Dimensions at all. (however if there is no solution without adding new elements to the cube then I will of course propose a change request in the given cube)
What is the solution in this case? Is it possible to write this KPI without using additional Measures?
Answering my own question.
It looks like this cannot be solved as I tried. Finally I added a new column under T-SQL to the Fact_Headcount which now uses INNER JOINs to both Dim_Employee and Dim_Date and I use T-SQL's DateDiff to calculate ages for every employee with every given datetime. Now I added Age as a Measure to this HeadCount MeasureGroup and now I can manage to do this KPI calculation.
Which means that I have to make modifications to the underlying model to solve the case.
Try using the CDATE function:
VBA!DateDiff("yyyy", now(), CDate([Employee].[BirthDate].CurrentMember.Member_Value))
I accept with your answer, We could also do this like
Datediff("yyyy",Now(),[Delivery Date].[Date].CurrentMember.Name)
-- Here the format of the Now() and the member is making issue. When I did at [Adventure Works] Cube with correct foramt acctually I am getting datediff in years

Passing MDX parameters to SQL query

I have several reports that are from SSAS 2008, but one of them has to drill through to a SQL Server report simply because the data is far to granular for a cube.
Any tips on passing parameters? Of course they are passed in as MDX, and I can't figure out a way to get just the "Key" with the source MDX. Surprisingly I can't find a lot of pointers on this.
Let me know if this is too vague...
Are you getting the MDX passed in as a string which you can examine and fiddle with? If you can extract the dimension and member name, you could look up the name in the SQL database to find the ID required, which could then be passed on?
I assume you are getting strings like this:
[Location].[All location].[South].[Surrey].[Guildford]
If you look up 'Guildford' in your SQL db you'll be able to find out it is row 2134 and that's what you need?
I'm assuming that you want to call this report from a report action. If your attribute has a single column key you can reference it using something like:
[<dimension>].[<hierarchy>].CurrentMember.Properties("MEMBER_KEY")
If your attribute has multiple key columns you would use KEY0, KEY1, etc to reference the individual parts instead of MEMBER_KEY

MDX query issue

In analysis services, I have cube that is based on hospitalization data. For each hospitalization there are potentially 9 icd codes and these are each stored in their own field in the view on which the cube is based. These are stored in a child table in the relational database on which the SSAS database is based.
I would like to query the cube to return all rows that have a certain ICD code in any one or more of the 9 icd code fields. It seems as if it should be simple to have this sort of "OR" in the WHERE or the Filter clause, but I'm not finding the correct method.
Thanks in advance,
Jeremy Schrader
As far as i understand, you are an SQL guy and new to MDX, so that's why you have difficulties for the query.
it would be better if you tell us what are the measures you want to select with ICD codes but i am going to try to show you an mdx query sample as simple as possible. Your query should like below;
select {Measure1,Measure2,...} on columns
ICDCodeDimension.Children on rows
//{ICDCodeDimension.ICDCode1,ICDCodeDimension.ICDCode5,...} on rows
from Cube
MDX is highly advanced query language and there are many more concept you should know/learn to use it effectively.
Hope this help.
I am guessing that you'd have a dimension called [ICD Codes] with a single level called [Codes] and 9 members called [Code A] and [Code B] or whatever. Perhaps even a member for [No code] too?
In that case your query would be able to tell you the total number of hospitalisation cases for each code, for a certain time period, across all hospitals:
SELECT {[ICD Codes].[Codes].members} ON ROWS,
{[Measures].[Number of Cases]} ON COLUMNS
FROM [CubeName]
WHERE ([Time].[2010].[Quarter 1])
Thanks for both of your feedback. After I researched further (particularly an article here: http://sqlblog.com/blogs/mosha/default.aspx that uses the SUBCUBE method to give some "OR" functionality but with very poor performance) I realized that the OR construct that I was looking for requires record-level information and so doesn't work after the aggregation that SSAS performs. Thus, I need to create a field on the fact table that has the result of the SQL "OR" statement that I need.
In this case I will just create a flag for any record that has a certain range of ICD codes in any of the 9 ICD code fields. Then, I'll create a measure that gives a count of these. Luckily, the requirements of my app are that only a limited number of diagnoses need to be looked at in this way(i.e. any hospitalization that is diabetes-related,tobacco-related,etc.). I'm still curious how one would approach this if you needed to allow the user to choose any ICD code. My understanding at this point is that you would then need to revert back to plain SQL.
Jeremy