Exclude the last fact row of a sum measure - ssas

I have a fact table that have 2 columns, 'timeInEventA', 'timeInEventB'. These columns store the difference in seconds between the actual ticket and the next ticket.
Ex: If I have a 'eventA' at 2020/01/04 05:00:00, and the next ticket is 2020/01/04 05:01:10 the column 'timeInEventA' in the first ticket will have the value '70'.
There is the possibility of the ticket have neither eventA, neither eventB, so the two values in the fact table row will be 0.
This difference is calculated in the ETL and stored in the Fact Table.
Problem: The client will filter the period by day. So he will choose 'between 2020/01/03 and 2020/01/05 give me the sum of timeInEventA and timeInEventB'. But was decided that the last ticket of the filter will be excluded because the next event is outside the filter range. So what I can do to exclude the last row register of the sum?
My fact table have these 2 measure columns, a surrogate key to date dimension (ex: 20200103 ), a surrogate key for time with minute granularity( Ex: event occurred 05:03:22 will result in a 0503 surrogate key ), and a surrogate key to the customer dimension.
PS: In the past, I had this problem for another situation and was suggested me to have a sum measure pointing to the measure column, a lastvalue measure pointing to the same column, and a derived calculation subtracts the lastvalue( MDX: Exclude a member that share same dimension property of a measure ).
But for this situation, this approach don't resolve. If my filter is 20200102 to 20200108, this approach will exclude all 20200108 values in the calculation.
Best Regards,
Luis

You need to define another measure with usage of last non empty value(This is done in the SSAS project, Cube structure tab->Define new measure). Then substract its value from your measure.

Related

DAX formula calculate dates between first transaction and (first transaction + 6 months)

Background: I have a column in a Customer dimension with a static date(e.g '2013-01-01').
This column is the result of a calculation that gets the first transaction date ever made by that certain customer. This customer dimension is linked to a fact table containing reportdate as a date column linked to a date dimension.
Goal: I want to make a calculated measure that, based on a sum of amount measure, calculates the result based on a given period between start_date(First transaction date), end_date (first transaction date + 6 months).
All i get is "cannot be determined in the current context" warnings and cannot get my head around to fix it.
All help is welcome!
Thanks in advance,
/Blixter
SOLVED I replicated the logic from the calculated measure found in the Customer table.
=CALCULATE([SumAmount];DATESBETWEEN(DimDate[Date];FIRSTDATE(‌​FactTable[Reportdate‌​]);DATEADD(FIRSTDATE‌​(FactTable[Reportdat‌​e]);5;MONTH)))

Create MDX to Divide two measures for each month and then sum the all of the months

I have a multidimensional cube that needs a custom measure that I'm not sure how to build.
That data looks like this:
MemberID-----Date-------EventType
1--------------1/1/2016-------1
2--------------1/1/2016-------2
3--------------2/1/2016-------1
2--------------2/1/2016-------2
4--------------2/1/2016-------2
There is a count measure in the cube, but others can be added if needed. I need to create a measure that will use whatever filters the user applies and then count the EventType (1 and 2 only) by month, divide the resulting counts for EventType 1 into the count for EventType 2 (for each month individually), and finally sum the monthly results. For example 1/1/2016 would be 1/1=1 (count of EventType 1 and count of EventType 2) and 2/1/2016 would be 1/2=0.5 so the resulting measure value for the two months would be 1+0.5=1.5. Any help is greatly appreciated.
Let's assume you have a Date dimension with an attribute called Month. And let's assume you have an EventType dimension. And let's assume you have a count measure in your measure group called Cnt. Here's what else you need to do.
First, go to the DSV and add a new calculated column to the fact table which is called NullInt and is the following expression:
cast(null as int)
Then create a new Sum measure in your measure group off that column and call the measure My Rollup. Under the Source property, change NullHandling to Preserve so that it will start off null.
To explain why we're doing this, a scoped assignment to a physical measure will aggregate up. (If you assign a value to a physical measure at the grain of each month, then it will rollup to the grand total.) But a scoped assignment to a calculated measure doesn't roll up.
Then in your MDX script add the following calculations:
scope([Date].[Month].[Month].Members); //calculate at the month level then rollup
[Measures].[My Rollup] = DIVIDE(
([Event Type].[Event Type].&[1],[Measures].[Cnt]),
([Event Type].[Event Type].&[2],[Measures].[Cnt])
);
end scope;
Note that your version of SSAS probably has the DIVIDE function if it's AS2012 with the latest service pack or newer. But if it doesn't, you can always do division the old fashioned way as IIF(denom=0,null,num/denom).

Creating a calculated column (not aggregate) that changes value based on context SSAS tabular DAX

Data: I have a single row that represents an annual subscription to a product, it has an overall startDate and endDate, there is also third date which is startdate + 1 month called endDateNew. I also have a non-related date table (called table X).
Output I'm looking for: I need a new column called Categorisation that will return 'New' if the date selected in table X is between startDate and endDateNew and 'Existing' if the date is between startDate and endDate.
Problem: The column seems to evaluate immediately without taking in to account the date context from the non-related date table - I kinda expected this to happen in visual studio (where it assumes the context is all records?) but when previewing in Excel it carries through this same value through.
The bit that is working:I have an aggregate (an active subscriber count) that correctly counts the subscription as active over the months selected in Table X.
The SQL equivalent on an individual date:
case
when '2015-10-01' between startDate and endDateNew then 'New'
when '2015-10-01' < endDate then 'Existing'
end as Category
where the value would be calculated for each date in table X
Thanks!
Ross
Calculated columns are only evaluated at model refresh/process time. This is by design. There is no way to make a calculated column change based on run-time changes in filter context from a pivot table.
Ross,
Calculated columns work differently than Excel. Optimally the value is known when the record is first added to the model.
Your example is kinda similar to a slowly changing dimension .
There are several possible solutions. Here are two and a half:
Full process on the last 32 days of data every time you process the subscriptions table (which may be unacceptably inefficient).
OR
Create a new table 'Subscription scd' with the primary key from the subscriptions table and your single calculated column of 'Subscription Age in Days'. Like an outrigger. This table could be reprocessed more efficiently than reprocessing the subscriptions table, so process the subscriptions table as incrementals only and do a full process on this table for the data within the last 32 days instead.
OR
Decide which measures are interesting within the 'new/existing' context and write explicit measures for them using a dynamic filter on the date column in the measures
eg. Define
'Sum of Sales - New Subscriptions',
'Sum of Sales - Existing Subscriptions',
'Distinct Count of New Subscriptions - Last 28 Days', etc

How to handle monthly and yearly values

I have a Fact table that holds what are more or less, sales goals. The ETL process that populates it, generates 12 "weighted" values into seperate rows, one per month. Each row however, also includes a field that holds the yearly value. I do this with unpivot. This all works. Now Im trying to get at this data in the cube with an SSRS report. The problem seems to be that I can query and see the results that include either the yearly goal values or the monthly, weighted values, but not both in the same set.
[update for fact table details]
My Fact table looks something like this:
FK_Account
FK_User
Target
Projected
GoalYear
FK_DateKey
FK_Dept
MonthlyWeightedTarget
MonthlyWeightedProjected
When I load this fact table via the ETL, I get the date key associated with each monthly value (MonthlyWeightedTarget). That will be 12 seperate records, but each one will have the same yearly value. Im not including next years value as a seperate column, because there are seperate records already associated with that year.
Basically, the users define a set of goals associated with a given year. Then I am applying a "weighting" to generate 12 seperate "monthly" records, which total up to the yearly target goal. Hope this makes sense.
What I need to see is something like this result:
Account Name
YTDgoal
YearGoal
NextYrGoal
I created a calculated member for the NextYrGoal, but now Im not sure I even need it.
What would be a good approach for handling the above (getting the ytd, yearly and next year values) ?
If I was getting at these values with TSQL, I would sum on the monthly values, and just include the associated yearly and next years values, grouping by account, year-goal, next-year-goal

OLAP dimension for boolean, time, selective count

I have just started tinkering with MS SQL Analysis Services. For a start, I'm creating one cube from sales detail table. For the dimensions I have created ProductDim from product master table, LocationDim from location tables, and a CalendarDim.
However I'm stuck when trying to provide these data:
boolean: how do I let user filter active/inactive transactions? Should I create a dimension containing 2 values, TRUE and FALSE?
time: should I create a dimension containing 00:00:00 to 23:59:59 or should I merge time into my calendar dimension?
transaction count: one transaction can have many line items, there's line item id, and there's transaction id, how do I set the dimension so user can see transaction count? Because the count of the measure is line item count.
So, I've been reading about this quite a bit recently, and I will try to answer each one as much as theory suggests:
For this, you should create something called 'junk' dimension: its basically a dimension with no attributes. http://en.wikipedia.org/wiki/Dimension_(data_warehouse)
You probably don't want the time dimension merged with calendar. You'll end up storing way too many records. If your granularity is minute, then one day would be 24 * 60 = 1440 records. You have to decide how granular you want to go (per minute, per second??) And then store an entire days worth of time in a 'Time' dimension. So you fact tables will have two keys, one to your calendar dimension, and one to your 'time' dimension.
Transaction count should be a 'measure', I think (no?). I assume you have transaction id repeated, because you have multiple line items per transaction. When you setup the measure, you can do 'distinct count' of transaction id.