Calculating the no. of days for the selected date period using MDX - ssas

I am trying to create a calculated column on a SSAS cube to work out the following:
Net Net X Rate = [Net Net Revenue] / [X]
where X = no of days
I need an output for X (using MDX), Something like the no. of days for the date period selected
For example upon the selection of
30 days for the chosen month of April 2021
X = 30
rate for 30 days
14 days for the chosen month of December 2020
X=14
rate for 14 days

I don't have access to SSAS at this point, but maybe somthing like
Net Net X Rate = [Net Net Revenue] / COUNT( EXISTING [Date].[Calendar].[Day].Members )
Answered here maybe: Count children members in MDX

Maybe this could help: DateDiffDays or .
I had something like this in mind: DateDiffDays([Measures].[From_Date], Now()). It will count the days difference between some day in the past and now. DateDiffWorkdays will get you a number of working days between two dates.
Alternatively, you could pre-calculate this value in a view and then pull the number into a cube.

Assuming your Date dimension is on day granularity, a very efficient way from query performance point of view to get what you want would be to add a column to your date dimension table. This could either be done in a view in the relational data model, or in the DVS as a calculated column. Name it e. g. number of days or just X, and make it be the value 1 on each row, i. e. the column expression is just 1. Then you create a new measure group based on this table, with the only measure being X, which would just sum this column. Then, whatever your query context would be, the X measure would just be the number of days. If you want, you can then make the measure invisible.

Related

MDX - Average over the whole time period, even when no data exists

I have a fact table with 1 row for each day. Example:
ID Date Value
1 20190101 10
1 20190102 15
2 20190101 31
If I take a simple Value average in SSAS cube I get:
ID Average <Formula>
1 12.5 (10+15)/2
2 15.5 31/2
As I understand, 15.5 is there because in total there are 2 days in the scope as only two days exist in the fact data when I select the whole month.
However, I need to calculate a monthly average instead. It should check that there are 31 days in that month (based on Date dimension) and get this result:
ID Average <Formula>
1 0.8 (10+15)/31
2 1 31/31
So far I've tried to create some "fake rows" if my data, for example I've tried to create rows with Value = 0 for dates 20190103-20190131 for ID=1.
This works, it forces the calculation for ID=1 to always take all days in the period, but it messes up my other calculations in the cube.
Any other ways to force average calculation in SSAS multidimensional cube to always calculate for the entire month?
If you want to do the calculation in the Cube, you can use the Descendants function on your Date dimension
For eg., the following gives the number of days in a month using the AdventureWorks sample
WITH MEMBER Measures.DayCount AS
Descendants
(
[Date].[Calendar].CurrentMember,
[Date].[Calendar].[Date],
LEAVES
).Count
SELECT [Measures].[DayCount] ON 0,
[Date].[Calendar].[Month].ALLMEMBERS ON 1
FROM [Adventure Works]
I would recommend:
select id, eomonth(date) as eom,
sum(value) * 1.0 / day(eomonth(date)) as average
from t
group by id, eomonth(date);
EOMONTH() returns the last day of the month. You can extract the day to get the number of days in the month.
The * 1.0is because SQL Server does integer division. Your numbers look like integers, but if you are getting 15.5, then you actually have numerics or something other than an integer.

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)

Tabular Query to Compare Measures Across Dates

I've got a number of rows and I want to calculate the difference per date.
So say I have the following:
[Date] [Transaction Number] [Value]
1 Jan 16 1 1000
2 Jan 16 1 980
I then want a fact that for every row will compare the value with the measure from the previous date.
So If I have a measure on SUM(Value) for the current date, I basically want SUM(CurrentDate) - SUM(PreviousDate) to see the movement.
A couple of things to note:
There will actually be a couple of comparisons: previous date, previous month end, previous year end.
I want this as a calculated measure not column so that I do not need to filter on the transaction number in the previous period.
What I've tried but it just comes up empty:
Previous Value :=CALCULATE(SUM(Table[Value])) - CALCULATE(SUM(Table[Value]), FILTER(Table, Table[Date] = PreviousDay(Table[Date])))
Unfortunately I cannot tell why your measure didn't work, but following should:
Previous Value := CALCULATE(SUM(Table[Value]) - CALCULATE(SUM(Table[Value]), PREVIOUSDAY(Table[date])))

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?

MDX Beginning to Current Date Sum

This is regarding calculating the sum for a set of values starting from the date a measure has entries till the current date.
So far what I could find was the YTD function. This limits the aggregation capability till the current date beginning from the first day of the year. But what my requirement is to start the calculation from the first value, this could be in the last year or may be two years before.
Eg:
Date-----------Value
11/9/2010-----2000
2/10/2011-----500
8/5/2011------1000
With YTD the value is: 1500
What I need is: 3500
I really appreciate any help on this.
Something like:
SUM([Date].[Day].AllMembers, [Measures].[Value])
OR
SUM(OpeningPeriod([Date].[Day]):ClosingPeriod([Date].[Day]), [Measures].[Value]),
where [Date].[Day] - it's a level DAY of your dimension;
[Measures].[Value] - your measure.