mdx query to calculate difference between measure at nearest quarter date and measure today - date

I am trying to calculate a measure which is the difference between a measure (say, profit) today and the nearest preceding quarter.
E.g.: if the quarter months are March, June, September and December, then:
If the current month is May, then the calculated measure = Profit(May)-Profit(March)
If the current month is November, then the calculated measure = Profit(November)-Profit(September)
If the current month is December, then the calculated measure = Profit(December)-Profit(September)
I'm new to MDX and would really like some help on this.
I'm aware of .CurrentMember.PrevMember to get the previous member, but that doesn't help if you don't know how many previous members calls you should use.

Welcome to MDX. To solve the problem you need to have basic understanding of "User Hierarchy".Plus you should know how the functions "currentmember", "parent", "lag()" and "lastchild".
Based on this follow the example below. I am trying to re-produce your example on AdventureWorks for year 2013.
Lets first see all the internet sales amount for 2013
select
{[Measures].[Internet Sales Amount]}
on columns,
{
[Date].[Month of Year].[Month of Year]
}
on rows from
[adventure works]
where
[Date].[Calendar Year].&[2013]
Result
Now lets make a measure that will result value for the last month of previous quater
with
member [Measures].[LastMonthOfPreviousQuater] as
([Date].[Calendar].currentmember.parent.lag(1).lastchild,
[Measures].[Internet Sales Amount])
select
{[Measures].[Internet Sales Amount],[Measures].[LastMonthOfPreviousQuater]}
on columns,
{
[Date].[Calendar].[Month].&[2013]&[09]
}
on rows from
[adventure works]
Result

Related

Previous Month Calculation without Date Filters or Attributes in Report

I am trying to create a report that shows total sales for the previous financial year (March-April), the current financial ytd, and the previous month in powerbi. I do not want to include any date attributes in the report or place any date filters on the report.
The 2 measures below are working as expected, but I am running into issues when trying to calculate for the previous month.
This Year = CALCULATE('Fact InvoiceLine'[Total Fare Currency],
DATESYTD(ENDOFYEAR(dateadd('Date'[Date], -2,Year),"3/31"),"3/31"))
Previous Year = CALCULATE('Fact InvoiceLine'[Total Fare Currency],
DATESYTD(ENDOFYEAR(dateadd('Date'[Date], -3,Year),"3/31"),"3/31"))
The closest I have been able to get, though it's still far from what I need.... is this,
Last Month = CALCULATE('Fact InvoiceLine'[Total Fare Currency],
DATESMTD(ENDOFMONTH(dateadd('Date'[Date], -2,YEAR))))
which goes back 11 calendar months, but what I need is to see the total for 1 calendar month. Using PREVIOUSMONTH does not work either, as that requires either a date filter or date value in the report.
Last Month =
CALCULATE([Total Fare Currency],
FILTER(ALL('Date'),'Date'[Date] >= EOMONTH(TODAY(),-2)+1),
FILTER(ALL('Date'),'Date'[Date] <= EOMONTH(TODAY(),-1))
)

Tableau Fiscal Date Calculation

How can I get this outcome in Tableau?
I am trying to compare This Fiscal Year Vs Last Fiscal Year Revenue Data.
I need a calculation that will be able to add an extra day for order_date dimension.
Please help!
dateadd('day',1,[order date])
Check out this link for more date calculations.
https://help.tableau.com/current/pro/desktop/en-us/functions_functions_date.htm

Sum from/to date

I have 3 tables, one with stocktakes conducted last year, one with stocktakes conducted this year and one with sales. All of them are joined by date to one table where I have dates.
Now the question is what can I do to get table with:
store name/ last year stocktake date/ this year stocktake date/ sum of sales from last year stocktake date to this year stocktake date.
If you choose store, than stocktake date from one table, stocktake from second table all looks good, the problem is that I can't get sales to show from/to.
C2Csales = calculate(sum(PP_SalesLessTax[SalesLessTax]),PP_SalesLessTax[date] >= [ly date])
[ly date] is just a measure with last year stocktake date
I have a feeling that this have to be very easy but have no idea how to get this work
thanks
daniel
please see data model. It is a part of bigger model but I have trimmed it so it is clear what is this about.
data model
And here is what I need. Please see picture.
thanks for all responses
result required
You don't need two tables to simulate years. You can have just one. The idea of last year should be calculated by a measure. If you have a complete date table with a day for each row without missing days then you can build time intelligence.
If I get you, you need something like this two measures:
Sales = sum(PP_SalesLessTax[SalesLessTax]
Sales LY = CALCULATE ( sum(PP_SalesLessTax[SalesLessTax] , SAMEPERIODLASTYEAR ( DatesTable[DateColumn] )
With this two measures you can take both of them on same visualizations to compare them.
The idea of having from and to can be solved on visualizations. The slicer with a date type column can create a range filter data that will apply for this two created measures.

Extract highest date per month from a list of dates

I have a date column which I am trying to query to return only the largest date per month.
What I currently have, albeit very simple, returns 99% of what I am looking for. For example, If I list the column in ascending order the first entry is 2016-10-17 and ranges up to 2017-10-06.
A point to note is that the last day of every month may not be present in the data, so I'm really just looking to pull back whatever is the "largest" date present for any existing month.
The query I'm running at the moment looks like
SELECT MAX(date_col)
FROM schema_name.table_name
WHERE <condition1>
AND <condition2>
GROUP BY EXTRACT (MONTH FROM date_col)
ORDER BY max;
This does actually return most of what I'm looking for - what I'm actually getting back is
"2016-11-30"
"2016-12-30"
"2017-01-31"
"2017-02-28"
"2017-03-31"
"2017-04-28"
"2017-05-31"
"2017-06-30"
"2017-07-31"
"2017-08-31"
"2017-09-29"
"2017-10-06"
which are indeed the maximal values present for every month in the column. However, the result set doesn't seem to include the maximum date value from October 2016 (The first months worth of data in the column). There are multiple values in the column for that month, ranging up to 2016-10-31.
If anyone could point out why the max value for this month isn't being returned, I'd much appreciate it.
You are grouping by month (1 to 12) rather than by month and year. Since 2017-10-06 is greater than any day in October 2016, that's what you get for the "October" group.
You should
GROUP BY date_trunc('month', date_col)

Get month from date parameter in MDX

I'm currently building reports using Reporting Services (and MDX language).
I get a date thanks to a parameter:
MEMBER [Measures].[retail sales amount] AS (
STRTOMEMBER(#TimecalendarTimecalendarmonthhierarchy)
, [Measures].[Retail sales amount invoiced including tax])
STRTOMEMBER(#TimecalendarTimecalendarmonthhierarchy) could be something like that [Time calendar].[Time calendar month hierarchy].[Time calendar date].&[2011-03-18T00:00:00]
I would like to only get the month of this parameter. When I use:
MTD([Time calendar].[Time calendar month hierarchy].[Time calendar date].&[2011-03-18T00:00:00]
, [Measures].[Retail sales amount invoiced including tax])
it works fine but that's not the case for
MTD(STRTOMEMBER(#TimecalendarTimecalendarmonthhierarchy)
, [Measures].[Retail sales amount invoiced including tax])
Try :
MTD( STRTOMEMBER(#TimecalendarTimecalendarmonthhierarchy) ) * {[Measures].[Retail sales amount invoiced including tax]}
If it does not work, what is the result of :
STRTOMEMBER(#TimecalendarTimecalendarmonthhierarchy).name
MTD will give you the set of dates from the first date of the month to the date specified. If you want to get the month, you can either use ANCESTOR or PARENT. Parent will work if Month is directly above Date in your hierarchy. The difference between SUM(MTD(date), measure) and (date.parent, measure) is obvious when you think about previous months - if you go to a month which is in the past MTD will give you partial results as opposed to the month member itself (for dates other than the last date of the month).
StrToMember essentially translates a string to a member expression in MDX. There is no difference (functionally) between writing:
([Time].[Month].&[201001], [Measures].[SomeMeasure])
and
(StrToMember("[Time].[Month].&[201001]"), [Measures].[SomeMeasure])
It would be interesting to hear what exactly the error is when you execute your non-working expression in order to debug.
If you just want to get the month amount for the measure, you should write:
(StrToMember(#param).Parent, [Measures].[Retail sales amount...])
or (if you have more levels in between):
(Ancestor(StrToMember(#param),
[Time calendar].[<hierarchy>].[<month level>]),
[Measures].[Retail sales amount...])
Also, when using MTD you want to write:
SUM(MTD(<date member>), [Measure].[Some Measure])
instead of
MTD(<date member>, [Measure].[Some Measure])
have a look at BOL's definition of MTD