SSAS -> AdventureWorks Example -> Using the browser to splice a measure by week, shows results that have two of the same week records? - sql-server

I have been working on a cube and noticed that when I am browsing measures in my cube by weeks, I am getting an unexpected result, but first let me display my current scenario. I am looking at counts of a fact load by weeks. When I do so I am getting results like these. :
Weeks | Fact Internet Sales Count
2001-07-01 00:00:00.000 | 28
2001-07-08 00:00:00.000 | 29
....and so on as you would expect.
Further down I noticed this. :
2001-09-30 00:00:00.000 | 10
2001-09-30 00:00:00.000 | 24
As you can see, it shows the week twice with different counts, when you add these counts together it is the correct number of counts for this week (i.e. 34).
I am just confused why it is showing two weeks, when I look at the data in sql I can see that the difference in data between these two is strictly the month in which these dates fell (10 in the earliest month and 24 and the later month in any example).
I initially saw this in my original cube that I created on my own, in turn, I pulled up trusty adventureWorks practice cube and found that it was present in that cube also.

This is due to the fact that within this date hierarchy, the lowest attribute in the hierarchy was date not week. Therefore, there was always a split for weeks by date. This can be alleviated by making a date hierarchy with week as the lowest portion of a date hierarchy.

Related

How can I sort time dimensions in SSAS cubes? Poor sort is causing issues with ParallelPeriod()

I have an old cube built in SSAS. We break revenue down into a time dimension like so:
fiscal week
Date
Revenue
Prior Year Revenue
53
2020-12-31
$5000
????
49
2020-12-03
$1000
$200 (should be $100)
50
2020-12-10
$2000
$300
51
2020-12-17
$3000
$400
52
2020-12-24
$4000
$500
As you can see, the 53rd fiscal week is not displaying in the proper sort order.
This is becoming a problem due for a dimension we use, "Prior Year, Week". This attempts to grab the revenue from the same fiscal week instead of the previous year. It accomplishes this by calling ParallelPeriod([FiscalWeek], 52), [Measures].[Revenue]).
What I believe is happening is that because week 53 is at the beginning of the month, ParallelPeriod is counting it as one of the intervals it skips. This results in the Prior Year column showing fiscal week 50 when it should be 51, 51 when it should be 52, etc.
Is there a way to sort those dates? I've tried the obvious KeyColumnstuff and examined the source views, and nothing seems like it should be causing this poor sort.
Thanks in advance -- I don't know much about this stuff and am sort of being tossed into the deep end here.
EDIT:
I've made some progress here but still having issues. It appears that every year with 53 fiscal weeks would have this problem -- week 53 is sorted improperly.
The time dimension breaks down like this:
Year
Quarter
Month
Week Number
Bill Date
The bill dates are sorted in order, but for some reason, the weeks in the Browser look like this:
53
1
2
3
...
It appears that the Week Number attribute is the one that's off. The OrderBy property is set to AttributeKey and the OrderByAttribute property is set to Bill Date attribute. However, viewing just the pubdates reveals that they are properly sorted.
EDIT:
After setting the OrderBy to Key as suggested by the response below, it appears that some of the month hierarchies are out of order. For instance, certain months are containing weeks that don't belong. For example:
* Sep
* 35
* 2020-08-27
* 36
* 2020-09-03
* 37
* 2020-09-10
* 38
* 2020-09-17
* 39
* 2020-09-24
Here's the XML for the month attribute:
<Attribute>
<ID>Month attribute</ID>
<Name>Month attribute</Name>
<Type>Months</Type>
<EstimatedCount>48</EstimatedCount>
<KeyColumns>
<KeyColumn>
<DataType>WChar</DataType>
<Source xsi:type="ColumnBinding">
<TableID>dbo_dim_time</TableID>
<ColumnID>fiscalmonth</ColumnID>
</Source>
</KeyColumn>
</KeyColumns>
<NameColumn>
<DataType>WChar</DataType>
<Source xsi:type="ColumnBinding">
<TableID>dbo_dim_time</TableID>
<ColumnID>fiscalmonth</ColumnID>
</Source>
</NameColumn>
<AttributeRelationships>
<AttributeRelationship>
<AttributeID>Pubdate attribute</AttributeID>
<Name>Pubdate attribute</Name>
</AttributeRelationship>
</AttributeRelationships>
<OrderBy>AttributeKey</OrderBy>
<OrderByAttributeID>Pubdate attribute</OrderByAttributeID>
<MembersWithData>NonLeafDataHidden</MembersWithData>
<MembersWithDataCaption>(* data)</MembersWithDataCaption>
<AttributeHierarchyVisible>false</AttributeHierarchyVisible>
</Attribute>
The OrderBy design you described referencing Bill Date seems overly complex for your requirements.
For the Week Number attribute, I would just set the OrderBy property to Key.

Calculation of Night shift hours

I am stuck with an issue, I have to create an attendance system where I have an employee id and punching time.
There can be multiple punches in a day. So firstly for night shift employees I can't determine his actual in and out time and thus not the duration too.
For example a person came office at 11 pm on 1st April and left at 4 am on 2nd April. In between he also went out for dinner or tea .
I don't have a fixed number of punches. And I need to handle 2 dates as well. So min and max logic wont work.
The major problem is there are multiple punch-ins and row_number wont work as punches are of 2 different dates in case of night shift.
here tktno is employeeID , date is the punching time and sno, is number of punches in a day. I have sent you a sample data of a night shift employee.
I require data in given below format -
But here as you can see the hours calculation is not correct . I want to correct it .

Calculate Facebook likes, comments, and shares for different time zones from saved UTC

I've been struggle with this for a while and hope someone can give me an idea to tackle this.
We have a service that goes out and collects Facebook likes, comments, and shares for each status update multiple times a day. The table that stores this data is something like this:
PostId EngagementTypeId Value CollectedDate
100 1(for likes) 10 1/1/2013 1:00
100 2 (comments) 2 1/1/2013 1:00
100 3 0. 1/1/2013 1:00
100. 1. 12 1/1/2013 3:00
100. 2. 3. 1/1/2013 3:00
100. 3 5. 1/1/2013 3:00
Value holds the total for each engagement type at the time of collection.
I got a requirement to create a report that shows new value per day at different time zones.
Currently,I'm doing the calculation in a stored procedure that takes in a time zone offset and based on that I calculate the delta for each day. If this is for someone in California, the report will show 12 likes, 3 comments, and 5 shares for 12/31/2012. But if someone with the time zone offset of -1, he will see 10 likes on 12/31/2012 and 2 likes on 1/1/2013.
The problem I'm having is doing the calculation on the fly can be slow if we have a lot of data and a big date range. We're talking about having the delta pre-calculated for each day and stored in a table and I can just query from that ( we're considering SSAS but that's for the next phase). But doing this, I would need to have the data for each day for 24 time zones. Am I correct (and if so, this is not ideal) or is there a better way to approach this?
I'm using SQL 2012.
Thank you!
You need to convert UTC DateTime stored in your column to Date based on users UTC time. This way you don't have to worry about any table that has to be populated with data. To get users date from your UTC column you will use something like this
SELECT CONVERT(DATE,(DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), '01/29/2014 04:00')))
AS MyLocalDate
The select statement above figures out Local date based on the difference of UTC date and local Date. You will need to replace GETDATE() with users DATETIME that is passed in to your procedure and replace '01/29/2014 04:00' with your column. This way when you select any date from your table it will be according to what that date was at users local time. Than you can calculate other fields accordingly.

Cumulative Sum - Choosing Portions of Hierarchy

I have a bit of an interesting problem.
I required the cumulative sum on a set that is created by pieces of a Time dimension. The time dimension is based on hours and minutes. This dimension begins at the 0 hour and minute and ends at the 23 hour and 59 minute.
What I need to do is slice out portions from say 09:30 AM - 04:00 PM or 4:30PM - 09:30 AM. And I need these values in order to perform my cumulative sums. I'm hoping that someone could suggest a means of doing this with standard MDX. If not is my only alternative to write my own stored procedure which forms my Periods to date set extraction using the logic described above?
Thanks in advance!
You can create a secondary hiearchy in your time dimension with only the hour and filter the query with it.
[Time].[Calendar] -> the hierarchy with year, months, day and hours level
[Time].[Hour] -> the 'new' hierarchy with only hours level (e.g.) 09:30 AM.
The you can make a query in mdx adding your criteria as filter :
SELECT
my axis...
WHERE ( SELECT { [Time].[Hour].[09:30 AM]:[Time].[Hour].[04:00 PM] } on 0 FROM [MyCube] )
You can also create a new dimension instead of a hierarchy, the different is in the autoexists behaviour and the performance.

Grouping by week and daily breakdowns in SSRS

I have a query concerning SSRS and displaying data. My report will consist of a weekly breakdown of week numbers and sum of hours. Showing something like this
12/06/10 351
19/06/10 342
26/06/10 399
And then on clicking/toggling the week data cell(s) a breakdown by day will be displayed. I have completed the daily breakdown and written a function to return all weeks in 2010 but my difficulty lies in grouping by the week.
In the layout tab for SSRS, I have created a group on the week value and date value, and have the week value appearing in the group cell, but it creates an additional blank line below each day breakdown.
Could anyone help me?
Thanks very much

Resources