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
Related
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 :-)
I don't have much experience so I apologize in advance for a potentially dumb question. I did not create these tables nor the queries that have been used in the past. With that said --
For the past several months I have been using a date conversion query that was given to me to update columns from an integer to a date. It used to work just fine and I swear everything is the same for my latest data extractions, but at some point the dates started getting wonky. For example, a typical date column might look like:
58222
58158
59076
58103
And the conversion query looks something like this:
IIf([D_posting]<>0,[D_posting]-18261,0)
And returns the above dates as:
05/27/2059
03/24/2059
09/27/2061
01/28/2059
Which obviously is wrong. The situation kind of reminds me of how I remember we generated random numbers in C++ (which was a long time ago), but for the life of me I can't figure out how to reverse engineer the correct subtraction factor without a reference point.
I also tried using the CDate() function instead, and it resulted in a bunch of future dates also, leading me to wonder if there's something else wrong. I work for a small physicians group so it might be something in the Electronic Health Records software, but I'd like suggestion on what I should check to make sure it's nothing that I've done.
You could create a query that uses the 'cdate' function (see below) to return the date. You can modify the code so that it subtracts the offset (maybe 18261?)
In the immediate window of VBA you can tinker with the following:
The 'cdate' will take a number and convert it to a date:
?cdate(41925)
10/13/2014
The 'cdbl' will take a date and convert to a number.
?CDbl(Date())
41926
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.
What is the best way to group dates by day or month in solr queries ?
For example if I have the date in the following format: 20140108180854746 (actually the format doesn't matter all that much here, might as well be in a normal format), is there an easy way to just group by its days or months.
Someone suggested keeping a field just for the days that it will group by and the other solution I saw was something in the lines of
&group=true&group.func=rint(div(ms(date_dt),mul(24,mul(60,mul(60,1000)))))
which isn't exactly user friendly but it will do the trick I guess.
Any general advice about that?
There are in-built faceting methods to accomplish this. You can use the "facet.date.gap" param to achieve this. Take a look at the following example.
q=solr&facet=true
&facet.date=j_createddate
&facet.date.gap=+1DAY,+2DAY,+3DAY,+10DAY
Here, we define date faceting for j_createddate, a date field. Then we create compartments for the gaps 1 days, 2 days, 3 days and 10 days
Refer http://solr.pl/en/2010/08/23/the-scope-of-solr-faceting/ & http://wiki.apache.org/solr/SimpleFacetParameters#facet.date.gap
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