multidimensionel cube: sum on filtered fact - mdx

I have a fact_dueAnalysis table which contains all my customers outstanding. The table is built with a Date Reporting, so when I pick a specific Date Reporting date, I can see all customers outstanding for that specific day.
Now I want to let my users use a date hierarchy so they can select e.g. week 39 and get all outstanding for the LAST day of that week (I will always use the last date in the hierarchy my users have selected).
I have made the following script:
([Measures].[Due Amount],[Date Reporting].[Year - Week - Date].[Week].members) =
sum(generate(tail(
DESCENDANTS([Date Reporting].[Year - Week - Date].[Week],, leaves),1),1
)
,[Measures].[Customer Due Amount]);
I am here trying to get the latest (tail) date (leave) on my Date Reporting and then sum the Customer Due Amount and get the result in a new measure called Due Amount.
When the users select a specific date, it does work, but when they select a week I get a #VALUE as result.
How should I create this correct?

Here is my end result:
([Measures].[Due Amount],[Date Reporting].[Year - Week - Date].[Week].members) =
SUM(TAIL(DESCENDANTS([Date Reporting].[Year - Week - Date].CURRENTMEMBER,
[Date Reporting].[Year - Week - Date].[Date]),1)
,[Measures].[Customer Due Amount]);
This will do what I want:
Get the latest date in a selected week and sum the Customer Due Amounts and present it as Due Amount.

Related

Iteration with a cursor via stored procedure or a Join/Partition SQL Server / Function

I have a 4-4-5 Fiscal Year calendar that starts from a Sunday (Jan 3, 2016).
I have created a view with a column Fiscal Year, Fiscal Week (From 1 - 52), and Min (Date), Max(Date). This creates the Start and End Date for each of the Fiscal Week in the Fiscal Year.
Given that I have a particular order that may span a number of invoices with various dates. I would like to create get the total number of weeks for the invoices based on the Invoice date.
Originally I was thinking of creating a stored procedure that would accept an orderid and then loop through each week and if there was an invoice date based on invoice it would increased the counter. It would go through all 52 weeks.
However, I am wondering if there is another way I can do this can I perhaps create a function that would look through the invoice table and do a join with the view and partition by the Weeks or something? Help would be immensely appreciated.

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)

DAX Counting Values in previous period(s)

I have a Month Column with the Month Field populated for each line for the 100K of lines of data I have.
I need to count the amount of times the Month Field is populated in the Previous Month (Period).
I also need to count the total amount of times the Month Field is populated in the Previous 11 months as well.
This is a rolling count for each months reporting that I do..
table name: 'ws pds' and field name [Month Tagged]
You can utilize the powerful time intelligence functions in DAX such as PARRALLELPERIOD to look at values from previous months. But in order to make use of these functions you need to create a calendar/date entity. Mark that entity as a Date table. And join to it by date from your "ws pds" table. The Date dimension should span the timeframe of your date with a continuous list of dates, one row per day.
Then your measure could look like this:
PreviousMonthCount=
CALCULATE (
COUNTROWS ( 'ws pds' ),
'ws pds'[Month Tagged] <> BLANK (),
PARALLELPERIOD ( Calendar[Date], -1, MONTH )
)

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?