Is it possible to create a dynamically calculated field? - ssas

How can I create a dynamically calculated field to determine the age in days based on a date/time stamp?
For example say I have a field that has a date time stamp. What I want to be able to see when browsing the cube is a column showing the number of days since the time stamp. So if the date time stamp is '8/21/13 12:00PM' and I browse the cube on '8/22/13 12:00PM', the calculated column "Number of Days Since" would show 1.
Is this even possible with SSAS?

Yes, you can:
with member Measures.[date string] as
Left(Right([Date].[Date].CurrentMember.UniqueName, 9), 8)
member Measures.[date formatted] as
Left(Measures.[date string], 4) + "-" +
Mid(Measures.[date string], 5, 2) + "-" +
Right(Measures.[date string], 2)
member Measures.[date date] as
CDate( Measures.[date formatted])
member Measures.[to today] as
now() - Measures.[date date]
, format_string = 'yy" years, "m" months, "d" days"'
select {
Measures.[date string],
Measures.[date formatted],
Measures.[date date],
Measures.[to today]
}
on columns,
[Date].[Date].[Date].Members
on rows
from [Adventure Works]
gives what you want. You do of course not need all the intermediate measures, they just show step by step what is calculated.
One remark, however: I formatted the to today measure with a date format using yy. A four digit year would show as "1912 years" etc, as SSAS is internally using the date coding like Excel of number of days since Jan, 1, 1900. I mis-used the date formatting here to format a duration, which is a number and not a real date. This formatting approach is only giving correct results for positive durations less than 100 years. But this is only a formatting issue and the main duration calculation is correct nevertheless. It just shows the duration in days, and the time within the day as fractions.

Related

Calculating a parameter dynamically based on the input in SQL Server stored procedure

I need to write a SQL Server stored procedure where based on the input date given by the user, data needs to be calculated dynamically.
To explain in detail, I need to find the sum of weights recorded from start of a month till the input date in the database.
The data will be recorded in DB during each shift for each day.
A day consists of 3 shifts. So number of shifts will be 3*30=90 (for a 30 days month).
Here is where it gets tricky. My query needs to be such that, the number of shifts should be calculated dynamically based on the input date.
For example, if the input date is 20, then no.of shifts will be 3*20=60 (from 1st day of the month to input day).
This number of shifts will be used to calculate the total average weight recorded for that month.
[Sum of weights from 1st day till input day]
/ [no. of shifts from first day of month till input day]
I am providing part of my stored procedure that is relevant to this question. I can find the number of days from start date to input date, but I am unable to calculate the number of shifts based on the input date.
#Reportdate DATETIME,
SELECT
PartName,
(SUM((COALESCE(weightActualPerCent, 0))) / 'total shifts') AS 'OverWeight%'
FROM
table1
WHERE
Process = 'Cooling'
AND Day >= DATEADD(MONTH, DATEDIFF(MONTH, 0, #Reportdate), 0)
AND Day < #Reportdate;
In the above code, the denominator 'total shifts' should be calculated dynamically based on the number of days between start date and input date. Any suggestions/pointers will be much appreciated.
Apologies if the question was too lengthy or confusing.

Sum values on date table where one column equals a selected value from another

I have a DimDate table that has a Billable Day Portion field that can be between 0 and 1. For each day that's in the current Bonus Period I want to multiple that Day Portion by 10, and then return the total sum.
To find out what Bonus Period we're in, I return ContinuousBonusPeriod where the date equals today:
Current Continuous Bonus Period:= CALCULATE(MAX(DimDate[ContinuousBonusPeriod]), FILTER(DimDate, DimDate[DateKey] = TODAY()))
I can see in the measure display this is correctly coming back as Bonus Period 1. However, when I then use ContinuousBonusPeriod in the measure to determine the number of days in the current period, it only returns 10, 1 day multiplied by the static 10.
Billable Hours This Period:= CALCULATE(SUMX(DimDate, DimDate[Billable Day Portion] * 10), FILTER(DimDate, DimDate[ContinuousBonusPeriod] = [Current Continuous Bonus Period]))
It appears to be only counting today's DimDate record instead of all the records whereContinuousBonusPeriod = 'Bonus Period 1' as I'd expect.
I needed to make sure no existing filter was applied to the DimDate table when calculating the Current Continuous Bonus Period:
Current Continuous Bonus Period:= CALCULATE(MAX(DimDate[ContinuousBonusPeriod]), FILTER(ALL(DimDate), DimDate[DateKey] = TODAY()))
(Notice the ALL() statement)

Ms ACCESS: calculating past annual averages over varying date ranges

In a form on Ms ACCESS, a user can select a commodity (such as copper, nickel, etc.) from a list and a commodity price date from a list. A trailing 12 month average commodity price should then be calculated.
For example: the user selects Copper as commodity and February 1st 2010, 02/01/2010. I then want the average price to be calculated over the time period: [02/01/2009 - 02/01/2010].
I'm not sure how to write this in query form. This is the current incomplete code;
SELECT Avg(CommPrices.Price) AS Expr1,
FROM CommPrices
WHERE (((CommPrices.Commodity)=[Forms]![Tool Should Cost]![List243]))
AND CommPrices.DateComm = [Forms]![Tool Should Cost]![List55];
List243 is the list of commodities the user can select from, list55 is the list of dates the user can select. All data is obtained from the table CommPrices.
Note: the earliest dates in the column DateComm is 01/01/2008. So if the user selects a date for example 02/01/2008, then calculating the average over the past 12 months before 02/01/2008 won't be possible. I do want the code to still calculate the average using the dates available. (in the example it would just be the average over the past month)
Second Note: the column DateComm only has monthly dates for the first day of every month (e.g 01/01/2008, 02/01/2008, 03/01/2008). The dates listed in list55 can refer to different days in the month (e.g 03/16/2009), in that case I want the code to still calculate the past 12 month average using the closest commodity dates possible. So if the user selects date 03/16/2009, I want the code to calculate the 12 month average for 03/01/2008 - 03/01/2009.
For "integer" months it would be:
SELECT
Avg(CommPrices.Price) AS AveragePrice,
FROM
CommPrices
WHERE
CommPrices.Commodity=[Forms]![Tool Should Cost]![List243]
AND
CommPrices.DateComm = BETWEEN
DateSerial(Year([Forms]![Tool Should Cost]![List55]) - 1, Month([Forms]![Tool Should Cost]![List55]), 1)
AND
DateSerial(Year([Forms]![Tool Should Cost]![List55]), Month([Forms]![Tool Should Cost]![List55]), 1)

Excel MDX - same period last month - totals

I have the following MDX expression which helps me to display the data for the 'same period last month' from the Cube PER DAY.
Filter(
[Date].[Date].[Date],
[Date].[Date].CurrentMember.Member_Value >=
DateSerial(Year(DateAdd('m', -1, VBA![Date]())),
Month(DateAdd('m', -1, VBA![Date]())),
1
)
AND [Date].[Date].CurrentMember.Member_Value < DateAdd('m', -1, VBA![Date]())
)
Now I would like to redo the code to display just the total for this period without breaking it down per day. The reason for this is, that if for example I want to see the total number of active users for the same period last month I can't simply sum up the numbers of daily users as this will be higher than the real number of total users in this period (user may be active on more than one day and I just want to count him once for this period).
I tried chaning the code but nothing works so far. Any ideas?
Many thanks in advance,
Maciej

How to filter DATESBETWEEN based on column value (to calculate number of business days in a month)

Working on an SSAS Tabular project in Visual Studio 2010;
I'm trying to create a measure that calculates the total number of business days in a month:
I have Month Start Date and Month End Date measures, and Date and Is Business Day columns.
I can create a Total Business Days measure with COUNTROWS(FILTER(Dates,Dates[Is Business Day]=TRUE())). That gives me the number of business days in the context, but I want the number of business days for the current month.
I've tried various combinations of FILTER, COUNT, COUNTX, COUNTROWS, DATESBETWEEN, and CALCULATE without success.
What I want is a count of days between two dates, where the column [Is Business Day] is true, but I can't seem to get the right combination of filtering.
I would guess I filter the Dates table the way I do for the Total Business Days measure, but FILTER returns a table and COUNTROWS expects a single column - is there a way to get single column from a FILTER result?
Here's one thought...
First, create a calculated column called MonthKey (if you don't have it already):
=YEAR([Date]) * 100 + MONTH([Date])
Then create another calculated column called IsCurrentMonth
=IF(YEAR(TODAY()) * 100 + MONTH(TODAY()) = [MonthKey], 1, 0)
Then you can create your calculated measure as
COUNTROWS(FILTER(Dates,Dates[IsCurrentMonth] = 1))
Would that do what you need?