Previous Year's End Value - ssas

I'm trying to retrieve the previous year's end value of a measure.
I know this code gets the value of the previous year, but at the same point in time during that year (so Mar 2012 looks to Mar 2011).
([Measures].[MeasureName], ParallelPeriod([Time].[Calendar].[Year]))
I'd like any date in 2012 to look at the last value in 2011 (Dec 2011). So if we're looking at the Year level of 2012 or any Month level, it all points to Dec 2011.

where are you trying to do this? On a MDX query? Or on a KPI?
Like you can access the value of Dec 2011 like this for example:
ParallelPeriod([Time].[Calendar].[Year], 1, [Time].[Calendar].[Year].[December 2012])

Combine the time functions:
ClosingPeriod([Time].[Calendar].[Month], Ancestor(ParallelPeriod([Time].[Calendar].[Year], 1), [Time].[Calendar].[Year]) )

Related

MDX Calculated Measure Year to Date

I have an OLAP multi dimensional cube, which in its simplest form contains:
A fact table with a NetAmount measure
A Time dimension, with fields for Year, Month, Day, Day of Month, Day of Year, etc
The requirement is that the user wants to be able to compare the total NetAmount from the beginning of each year up to a specific date. The date can be chosen by setting filters on the Day and Month fields. For example, the user wants to see total value for:
01 Jan 2019 to 20 Jan 2019
01 Jan 2018 to 20 Jan 2018
01 Jan 2017 to 20 Jan 2017
etc
What would be the best way to tackle this requirement?
You can create calculated member with YTD function like this:
CREATE MEMBER CURRENTCUBE.[Measures].[NetAmountYTD]
as
Aggregate(YTD ([Time].[Time].CurrentMember), [Measures].[NetAmount])
You could also create Utility dimension for example TimeUtility, and add YTD member, and define it like this:
Scope([TimeUtility].[YTD]);
this = Aggregate(YTD ([Time].[Time].CurrentMember), [TimeUtility].[DefaultMember]);
End Scope;
This way whatever measure you look against [TimeUtility].[YTD] it will give you Year to Date for that measure, not just NetAmount, so it will be general solution.

How to build a Dax to view data of all the months till data w.r.t all the years?

How can I build a DAX function which calculates all the data until a certain date and compare that with the previous year which have the same months as the "until" date?
For example, today's date is 5 April 2018, so if I select 2017 year inside the slicer, I should be able to see a graph which shows me the comparison between the start of year i.e 1 Jan 2018 to 5 April 2018, and 1 Jan 2017 to 5 April 2017 with the previous year.
Currently I am using YtD, but I think it's calculating all the 12 months of data of all the years except the year 2018, where it shows me data from Jan 1 to April 5. Can anyone shed some light here?
Currently I am using this YTDQty = TOTALYTD(sum(Bookscan[QtySold]),DATESYTD(Bookscan[Week Date]))
Which is showing me correct data of 2018, till date, I should be able to compare the 4 months of data to my previous years 2017, 2016, 2015, these years are showing me total data for all the years i.e 12months of data, However I only need to see data start from 2018 Jan till todays date or let say March 1, so all the years should show me this current data how to do this?
Very similar to this question.
Do you have a Date Dimension in your model?
TotalQuantity =
SUM(Bookscan[QtySold])
TotalQuantity YTD =
TOTALYTD([TotalQuantity],'Date'[Date])
TotalQuantity YTD LY =
CALCULATE(
[TotalQuantity YTD],
SAMEPERIODLASTYEAR('Date'[Date])
)

Update statement Syntax (PostgreSQL-Redshift)

In the time dimension we have added additional two columns called Fiscal Year and Fiscal Year Month which are to be populated. In our case Fiscal year starts from
Sep 01 and ends on Aug 31. (For example - Sep 01, 2017 to Aug 31,2018 is Fiscal Year 2018). I am trying to write an update statement in PostgreSQL.
(Logic - case when X.monthname in ('September','October','November','December') then (X.Year+1) else X.Year end)- But looks like I am not able to get the right syntax. Can you help build it ?
We have Date, Year, MonthName columns in our Time table which can be used in update statement.
Thanks.
I think the update would look like:
update x
set Year = Year + 1
where x.monthname in ('September', 'October', 'November', 'December') ;

Convert date to first of current month if date is previous to current month

I have a query with an IIF() expression in one column that I am using to identify if a date is previous to the current month to then amend it if so. So if I run the query on 19th March 2014 and the EffFrom date is before 1st March 2014, I would want that column entry to now appear as 1st March 2014.
I am using the below expression which is pretty much doing what I want, however I know it is not considering the year -- i.e. it is changing an entry of 1st Jan 2015 to be 1st March 2014.
EffFrom:
IIf(Month([Table.Efffrom])"Less than symbol"Month(Date()),Date()-Day(Date())+1,[Table.Efffrom])
Can someone correct the expression for me?
I interpreted "if I run the query on 19th March 2014 and the efffrom date is before 1st March 2014 I would want that column entry to now appear as 1st March 2014" to mean you want something like this from a query run today (Mar 19th 2014):
id Efffrom adjusted_date
1 1/1/2014 3/1/2014
2 3/1/2014 3/1/2014
3 3/31/2014 3/31/2014
4 1/1/2015 1/1/2015
If that is correct, your IIf expression can use DateSerial to check whether Efffrom is before the first of this month, and transform the older dates.
SELECT
y.id,
y.Efffrom,
IIf
(
y.Efffrom < DateSerial(Year(Date()), Month(Date()), 1),
DateSerial(Year(Date()), Month(Date()), 1),
y.Efffrom
) AS adjusted_date
FROM YourTable AS y;
Here's what i do to get the first day of the next month
EffFrom: DateAdd("m",1,CDate(Format([Table.Efffrom],"m/\1/yy")))

Getting the last month date's data in sql

I have the below query to get the last month date's data. Suppose if today is 23rd Dec, then it fetches 23rd Nov data. But when it's 31st Dec, then there would be no 31st Nov. In that case it should fetch 30th Nov data.
So, I want to decrease one day and check the condition again and should fetch the data. In this scenario, suppose on March 31st, first that query even should check for feb 31st, then it should check for feb 30, then 29th and so on until the valid date of that particular month.
My current query:
select *
from dataTable
where date(datecolumn)=date(add_months(DATE(sysdate-1) ,-1));
So, this only fetches last month's date data. So, can someone please suggest me how to check for the mentioned validation in the query and fetch the data?
try this:
SELECT dateadd(mm, -1, Convert(Datetime, '2013-12-31')