SSAS custom group by query - ssas

My fact table looks like this
yesterday a
yesterday a
yesterday a
yesterday b
yesterday b
yesterday c
today a
today a
today b
today b
tommorow a
tommorow a
tommorow c
tommorow d
In the end I need an Excel report like this
repetition count
1 2 3
yesterday 1 1 1
today 0 2 0
tomorow 2 1 0
How to create a "repetion count" dimension in SSAS 2k5 ? Please keep in mind that my fact table is a liitle bit more complicated and I have more other dimension there.
My idea is to create a named query in DSV but I have some doubts if filtering will work correctly.

I think you would need to do a view with a GROUP BY and a count so that you load data like the following into your cube
yesterday a 3
yesterday b 2
yesterday c 1
today a 2
today b 2
tomorrow a 2
tomorrow c 1
tomorrow d 1
Then you could use the count column as the key for a repetition count dimension and you would create a measure based on the row count aggregate type.

Related

Retrieve field from table B based on date in table A

I have table A which contains tasks and completion dates and table B that contains the Fiscal Year, and production cycles. This is the structure of table B:
FY
Cycle
StartDate
EndDate
2021
2
2021-03-31
2021-06-30
2021
3
2021-07-01
2021-31-12
2022
1
2022-01-01
2022-03-31
2022
2
2022-03-31
2022-06-30
What I want to do is to retrieve Cycle based on whether my date in Table A falls between a StartDate and EndDate.
My resultset would be my date and then the cycle. Eg.
Task with date 2022-02-22 08:43:44.002000 falls between 2022-01-01 and 2022-03-31 so I want to retrieve cycle 1 in FY 2022.
Since I cannot join this table directly and check it with a BETWEEN I tried to create a CTE containing the entire table of B but then I kind of got stuck with the next steps. There must be a better approach than the code below (which doesn't work).
WITH cte AS (
SELECT * FROM B
)
CASE WHEN task_date >= (SELECT StartDate FROM cte) AND task_date < (SELECT EndDate FROM CTE)
Could i perhaps point you to a question i've asked in the past where i was trying to link data from 2 tables in separate databases where there was no link between them. The answer from Squirrel was able to give me exactly what i needed:
How To Insert Data Into Temp Table From Different Databases
It basically involves adding a Row Number to each table and joining on that Row Number. You might be able to use that logic to get what you need.

Display target value for week when targets are in 4 week increments

I have a targets table which goes up in increments of 4 weeks like so
Week
Target
1
12345
5
67890
9
12345
I then have a calendar table from which I would display all 52 weeks of a year. I would like to list the target for the higher week where the week number is between 2 week numbers. E.g. Week 4 will use the target for week 5 as it is between 1 & 5.
I have tried CTEs and left joins but this will not bring back every number in the calendar table, only those found in the target table. I have also tried cross apply and a number of ways to join. I am stumped at this point
You can just join with arithmetic:
select . . .
from calendar c join
targets t
on ceiling((c.week - 1) / 4.0) * 4 + 1 = t.week

How to make a (MDX) calculation in a SSAS cube to get the difference between a value from the current row and a value from the previous row?

I would like to make a calculation to get the difference between the departDate from my current row and the arriveDateNextStop from my previous row. I have a fact table which has multiple columns. The three most important columns are: id, departDate, arriveDateNextStop.
If I have for example these two rows in my fact table:
id departDate arriveDateNextStop
1 01-01-2019 03-01-2019
1 04-01-2019 07-01-2019
Explanation: On 1 January 2019 I depart to the next destination and I arrive there on 3 January 2019. On 4 January 2019 I again depart to the next destination and I arrive there on 7 January 2019.
Now I would like to know how many days the idle time was (the amount of days between the arrival and the next depart). So with this example the idle time would be 1, because between 3 January 2019 and 4 January 2019 is one day.
First, I made this 'calculation' in Management Studio as a SQL query. See query below:
SELECT s.Id, s.departDate as Depart_current_place, s.arriveDateNextStop as Arrival_next_stop, LAG(arriveDateNextStop) OVER (ORDER BY arriveDateNextStop) AS Arrival_current_stop, DATEDIFF(DAY, LAG(arriveDateNextStop) OVER (ORDER BY arriveDateNextStop), departDate) AS Amount_of_days
FROM MyTable s
WHERE Id = 9
GROUP BY s.departDate, s.Id, s.arriveDateNextStop
ORDER BY s.departDate
This query works fine, but how can I do this in my cube as a calculation in MDX?
I don't have the same example, but the similar cube structure with Completed/Received date:
with
member departDate as [Received].[Year Qtr Month].CurrentMember.Member_Key
member arriveDate as [Completed].[Year Qtr Month].CurrentMember.Member_Key
member arriveDateNextStop as [Completed].[Year Qtr Month].CurrentMember.Lead(1).Member_Key
member idleDays as departDate-arriveDateNextStop
SELECT NON EMPTY { departDate,arriveDate,arriveDateNextStop,idleDays } ON 0
, NON EMPTY
{ ([Completed].[Year Qtr Month].[Date].ALLMEMBERS
* [Received].[Year Qtr Month].[Date].ALLMEMBERS ) } ON 1
FROM ( SELECT ( { [Completed].[Year Qtr Month].[Date].&[6213] } ) ON COLUMNS
FROM [MyCube])
I also have integer key for a date dimension (CurrentMember.Member_Key). 1 = 1998-01-01, 2 = 1998-01-02 etc. till today. You need to create a property in a Date dimension if your Date key is classic YYYYMMDD (which you cannot subtract to get days difference, I can do that in my example). And use it like CurrentMember.Properties("property name") instead of Member_Key.
Main formula part: Lag/Lead function to get prev. or next member.
Please update in case of questions.

Calculating Weekly Returns from Daily Time Series of Prices v2

This is a slightly adjusted version of the question here: Calculating Weekly Returns from Daily Time Series of Prices which was answered by #Scott Craner:
I want to calculate weekly returns of a mutual fund from a time series of daily prices. My data looks like this:
A B C D E
DATE WEEK W.DAY MF.PRICE WEEKLY RETURN
02/01/12 1 1 2,7587 -0,0108
03/01/12 1 2 2,7667
04/01/12 1 3 2,7892
05/01/12 1 4 2,7666
06/01/12 1 5 2,7391
09/01/12 2 1 2,7288 0,0067
10/01/12 2 2 2,6707
11/01/12 2 3 2,7044
12/01/12 2 4 2,7183
13/01/12 2 5 2,7619
16/01/12 3 1 2,7470 0,0511
17/01/12 3 2 2,7878
18/01/12 3 3 2,8156
19/01/12 3 4 2,8310
20/01/12 3 5 2,8760
23/01/12 4 1 2,8875
The date is (dd/mm/yy) format and "," is decimal separator. This would be done by using this formula: (Price for first weekday of next week - Price for first weekday of current week)/(Price for first weekday of current week). For example the return for the first week is (2,7288 - 2,7587)/2,7587 = -0,0108 and for the second is (2,7470 - 2,7288)/2,7288 = 0,0067.
The problem is that the list goes on for a year, and some weeks have less than five working days due to holidays or other reasons. Some weeks start with weekday 2, some end with weekday 3. So I can't simply copy and paste the formula above. I added the extra two columns for week number and week day using WEEKNUM and WEEKDAY functions, thought it might help. I want to automate this with a formula and hope to get a table like this:
WEEK RETURN
1 -0,0108
2 0,0067
3 0,0511
.
.
.
I'm looking for a way to tell excel to "find the prices that correspond to the min weekdays of two consecutive weeks and apply the formula "(Price for first weekday of next week - Price for first weekday of current week)/(Price for first weekday of current week)".
I would appreciate any help! (I have 5 separate worksheets for consecutive years, each with daily prices of 20 mutual funds)
It seems to me that you can generate your column E with this formula in E2 :
=IF(B2=B1, "", (VLOOKUP(1+B2, B3:D9, 3, FALSE) - D2)/D2)
It's a VLookup limited on the next 7 rows from each row that declares a new week.
Copying into all cells will give the result indicated in your first tableau. To transform this result into to the list (Week, Return) is a matter of a filter that hides blanks from E.
Notice that a problem could occur if the WeekNum restarts from one when a new year is reached, but since you say that each of your sheets is for one (calendar) year, it shouldn't happen.

Transforming a table by the condition in one row

I have a table like this
Day Operation Times Condition
Monday addition 3 done
Monday subtraction 2 done
Monday subtraction 4 uncomplete
Tuesday multiplication 3 done
Tuesday addition 1 done
Tuesday addition 8 uncomplete
Wednesday subtraction 1 uncomplete
And I want to transform the Condition column in two distinct columns something like
Day Operation Done Uncomplete Total
Monday addition 3 0 3
Monday subtraction 2 4 6
Tuesday multiplication 3 0 3
Tuesday addition 1 8 9
Wednesday subtraction 0 1 1
Fusioning the Done and Uncomplete Operations in a single row
Is there a way to do it in SQL?
So far I have tried something like this Creating multiple columns from one column using case. I think than I may need to group it first by day, and then by Operation.
Considering the fact that you do this on Microsoft Access please refer to the following tutorial: https://www.youtube.com/watch?v=ZgerpTHzQes
One method is conditional aggregation:
select day, operation,
sum(case when condition = 'done' then times else 0 end) as dones,
sum(case when condition = 'uncomplete' then times else 0 end) as uncompletes,
sum(times) as total
from t
group by day, operation;
You need to tag your question with the database you are using. The equivalent in MS Access is:
select day, operation,
sum(iif(condition = "done", times, 0)) as dones,
sum(iif(condition = "uncomplete", times, 0)) as uncompletes,
sum(times) as total
from t
group by day, operation;