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
Related
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?
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
I have this:
SELECT
{[Case_Type].[Case_Type].AllMembers} ON COLUMNS,
{[Geography].[country].AllMembers} ON ROWS
FROM [Cases_Cube]
where ([Create_Date_Key].[Create_Date_Key].&[20150201] : [Create_Date_Key].[Create_Date_Key].&[20150208])
and it works fine - IF there is a 20150201 and 20150208 respectively.
However, I need to be able to send in ANY date range. When I use a date that is not there... the query fails.
I have done a lot of research and can't seem to get a good, simple, answer.
This is the troublesome set?
[Create_Date_Key].[Create_Date_Key].&[20150201] : [Create_Date_Key].[Create_Date_Key].&[20150208]
If 20150201 does not exist in the cube the processor will interpret the above as the following:
null : [Create_Date_Key].[Create_Date_Key].&[20150208]
Which returns all dates up to 20150208
If 20150208 does not exist in the cube the processor will interpret the above as the following:
[Create_Date_Key].[Create_Date_Key].&[20150201] : null
Which returns all dates after to 20150201.
Hopefully this explains the behaviour you are experiencing.
Can you limit the user's choice to dates that exist in your cube?
I have what I thought was a simple task: create a Measure that returns the distinct count of a field for all records that meet a specific filter condition, in this case a date filter. I came up with the following:
=CALCULATE(DISTINCTCOUNT(ExitFilter[Dim_Client_ID]),ExitFilter[ExitDate]<1/1/2014)
and the result was (blank) so I tried:
=CALCULATE(DISTINCTCOUNT(ExitFilter[Dim_Client_ID]),FILTER(ExitFilter,ExitFilter[ExitDate]<1/1/2014))
and the result was still (blank) so I tried:
=CALCULATE(COUNTROWS(DISTINCT(ExitFilter[Dim_Client_ID])),FILTER(ExitFilter,ExitFilter[ExitDate]<1/1/2014))
and the result was still (blank). What am I missing? And what does (blank) signify?
If it's not clear 'ExitFilter' is a table, [Dim_Client_Id] is a column, and [ExitDate] is a date column.
Tom, comparing dates is from my experience best done with simple Date function.
So if you update your formula to something like this:
=CALCULATE([Your Measure], Table[DateColumn]>DATE(2014,1,1))
You will get what you need with easy-to-read code. Also, for any advanced time-based calculations, the best way to go is creating dates table and using Time Intelligence module of PowerPivot.
Tom, think your second attempt is 'right' but you need to be careful about how you are expressing the date.
Later versions PowerPivot are a little bit funny about this and while "01/01/2014" should work I don't think it will.
Instead try replacing 01/01/2014 with 41640 - this is is the underlying date value, its hacky but then so is any kind of hard coded date :-)
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.