Syntax for OLAP calculated measure based on a dimension - ssas

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.

Related

MDX-How to return multiple measures

I'm trying to optimize a 2M row SSAS query into Power BI by using MDX prior to the Power Query. I have experience in T-SQL and found a website to help translate T-SQL experience into MDX, which was successful for some queries (basic rows/column selects, crossjoins, non empty, order by, filter, where). So now I want to get in my sales data which contains three dimensions and four measures but I get the following error:
Executing the query ...
Query (3, 1) The 'Measures' hierarchy appears more than once in the tuple.
Run complete
I attempted a few variations related to crossjoining the measures and the dimensions, only selecting one measure (which still took too long), and specifying members vs children.
'''
select
([Date].[OrderDate].children, [Customer].[CustID].children, [ProdLevel].[ProdNumber].children) on rows,
([Measures].[Revenue], [Measures].[Units], [Measures].[ASP], [Measures].[Profit]) on columns
from [RepProdDB]
where [ProdLevel].[Prod Description].[MyBusinessUnit]
'''
Looking up the error: "The 'Measures' hierarchy appears more than once in the tuple." is a bit vague to me as I have slight but probably incomplete understanding of tuples.
My hope is to have something that I can easily get in PivotTable OLAP, Power Pivot, and Power Query but using the actual MDX code. Thoughts?
So you need to understand the diffrence between tuples and sets.
select
non empty
(
[Date].[OrderDate].children,
[Customer].[CustID].children,
[ProdLevel].[ProdNumber].children
)
on rows,
{
[Measures].[Revenue],
[Measures].[Units],
[Measures].[ASP],
[Measures].[Profit]
}
on columns
from [RepProdDB]
where
[ProdLevel].[Prod Description].[MyBusinessUnit]

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?

No QUARTER() in DAX? Really?

While building a Calendar table with PowerQuery for a PowerPivot model in Excel 2013 I use its Date.QuarterOfYear function to get the number of the quarter.
Building the same thing in SSAS Tabular requires some workarounds. There's no equivalent DAX function to get number of the quarter from a DATE. Strangely, DAX has YEAR() and MONTH(), but no QUARTER().
Are nested IF or SWITCH statements really the only way in DAX to get the quarter number?
What is the reason for the absence such a simple and useful function? Am I overlooking the supreme wisdom of this decision?
I found an answer in this great book!
One should use =ROUNDUP(MONTH([Date])/3, 0) to get quarter number.
No QUARTER() in DAX?
Yes, that is correct.
Really?
Yes, it's crazy and doesn't make any sense. Fortunately the workaround is just dividing the month by 3.
Solution:
VAR quarterNumber = CEILING(MONTH([Date])/3,1)
Alternate Solutions:
Since DAX has multiple ways to round numbers, these will also work:
VAR quarterNumber = ISO.CEILING(MONTH([Date])/3,1)
VAR quarterNumber = ROUNDUP(MONTH([Date])/3,0)
Which Solution is Best:
For the values used in my examples, the results will be identical.
For other examples there can be small and subtle differences in the result depending on standards or the type of CPU being used.
ROUNDUP is probably more intuitive to Excel people.
CEILING is probably more intuitive to math people.
ISO.CEILING is ugly to look at in code, personal opinion.
It's not documented but this works:
INT(FORMAT([Date], "q"))
DAX now has quarters! This is some date data:
And these are how you get the quarters and quarter numbers:
The results of these are below:
I think they are assuming you'd create a date dimension in which your Quarter is pre-defined, including the Financial Year. I live in Australia where the Financial Year ends in June, and I've always pre defined the quarters as an added column to the table. IF you're using Power BI/Power Query you can add a query in the M code level (at the import stage).
You can also use this command:
=FORMAT(Date[Date],"q")
And combined them to create things like this
="Q." & FORMAT(Date[Date],"q") & " - "&Date[Year] = e.g. Q.1 - 2021
Add column for get quarter :
Quarter = summary_bu_USD[Start Period].[Quarter]
Add column again and group year in column:
QuarterYear = YEAR(summary_bu_USD[Start Period])&" "&"Q"&RIGHT(summary_bu_USD[Quarter],1)
enter image description here

MDX IN Or SubQuery

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.

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