I found an example of how to use the Parallel Period function in my SSAS OLAP Cube here: https://www.mssqltips.com/sqlservertip/2915/sql-server-analysis-services-period-over-period-variance-analysis/
However it assumes that you have a Date Hierarchy, which I don't. I tried using it without success. My Date Dimension only has a date attribute and a Year Month attribute (ex: 2015/01 for January 2015). It has no hierarchy nor anything else.
(Interested why? Because it just works, and a hierarchy confused my users)
I need to compare values month over month and year over year.
This is what I could infer with the example, but it is not working:
IIF([Fact Date].[Date].CurrentMember.level.ordinal = 0,
[Measures].[Billed Amount],
(ParallelPeriod([Fact Date].[Year Month].[Year Month],
1,
[Fact Date].[Date].CurrentMember),
[Measures].[Billed Amount]
)
)
What would be the correct syntax to achieve this?
At the end I fixed it adding a Year attribute (ex: 2015), Month attribute (ex: 201501), and made a simple hierarchy (Year, Month, Date). It worked with the following code:
IIF([Fact Date].[Date Hierarchy].CurrentMember.level.ordinal = 0,
[Measures].[Billed Amount],
(ParallelPeriod([Fact Date].[Date Hierarchy].[Month],
1,
[Fact Date].[Date Hierarchy].CurrentMember),
[Measures].[Billed Amount]
)
)
Related
I'm using a ColdFusion collection to search events and I need to pass a date into the collection as a "mmm" so it can be searched. Every time I try I get an error.
custom4="DateFormat(start_date, "mmm")"
Update:
I'm trying to search "month" of the current year
You should use the above code like
custom4=dateFormat(start_date, "mmm")
(Remove the outer double quotes)
Quotes aren't the problem and the suggestion of removing them will actually cause an error. The problem is DateFormat() can't be applied to an entire query column. It's only capable of operating on a single value.
It would help to have more context about what you're trying to achieve, to determine the best approach.
If you want to find items dated in July of a specific year (i.e. July 2019) - then storing the full date, and searching for a date range, is probably a better way to go
If you want to find items dated in July of ANY year, then it's simpler to extract a month number within your SQL query, and store it in the collection. Then you need only search for a number.
Date Range Search
To search for a specific month/year, like July 2019, populate the collection with a timestamp field from your SQL query. Add the suffix _dt to the custom field name so it's treated as a date field
cfindex( query="yourQuery"
, collection="yourCollection"
, action="Update"
, type="Custom"
, Start_Date_dt="yourTimeStampColumn"
, ...
);
In the search criteria, use the date range July 1 through August 1, 2019 (yes - August 1st). Dates must be formatted for Solr, which expects YYYY-MM-DDThh:mm:ssZ. NB: Dates should be in UTC (not local time).
cfsearch (name="searchResults"
, collection="yourCollection"
, criteria=' start_date_dt:[2019-07-01T04:00:00Z TO 2019-08-01T04:00:00Z}'
);
Explanation/Notes
[ and } - Brackets indicates a range (i.e. from dateX TO dateY)
[ - Square bracket means inclusive (i.e. include July 1st)
} - Curly bracket means exclusive (i.e. exclude August 1st)
Field names should be in lower case
Month Number Search
To search for a specific month in any year, like July, extract the month number within your SQL query. (The exact syntax will be DBMS specific. You didn't mention which one you're using, so see your database's documentation on date functions.) Add the suffix _i to the custom field name so it's handled as an integer
cfindex( query="yourQuery"
, collection="yourCollection"
, action="Update"
, type="Custom"
, monthNumber_i="theMonthNumberColumn"
, ...
);
Then simply search for the desired month number, i.e. 7 - July
cfsearch ( name="searchResults"
, collection="yourCollection"
, criteria=' monthnumber_i:7 '
);
Full Example
Sample Query
sampleData = queryNew("MyID,Start_Date,MonthNumber"
, "integer,timestamp,integer"
, [{MyID=10, Start_Date="2019-06-30 12:30:00", MonthNumber=6}
, {MyID=20, Start_Date="2019-07-01 00:00:00", MonthNumber=7}
, {MyID=30, Start_Date="2019-07-01 16:30:00", MonthNumber=7}
, {MyID=40, Start_Date="2019-07-31 23:50:00", MonthNumber=7}
, {MyID=50, Start_Date="2019-08-01 00:00:00", MonthNumber=8}
]);
Create Collection
cfcollection ( action="create", collection="MyCollection");
Update Collection
cfindex( query="sampleData"
, collection="MyCollection"
, action="Update"
, type="Custom"
, key="MyID"
, title="SampleData"
, MonthNumber_i="MonthNumber"
, Start_Date_dt="Start_Date"
, body="MyID"
);
Find July, by month number
cfsearch ( name="monthNumberResults"
, collection="MyCollection"
, criteria=' monthnumber_i:7 '
);
// results
writeDump( var=monthNumberResults, label="Month Number Search" );
Find July 2019, by date range
// Search range: July 1 to August 1, 2019
fromDate = "2019-07-01";
toDate = dateAdd("m", 1, fromDate);
// Format dates for Solr
// Note: DateTimeFormat uses "n" for minutes. Valid in CF2016 Update 3 or higher
fromDate = dateTimeFormat( dateConvert("local2UTC", fromDate), "yyyy-mm-dd'T'HH:nn:ss'Z'");
toDate = dateTimeFormat( dateConvert("local2UTC", toDate), "yyyy-mm-dd'T'HH:nn:ss'Z'");
cfsearch ( name="dateRangeResults"
, collection="MyCollection"
, criteria=' start_date_dt:[#fromDate# TO #toDate#} '
);
// results
writeDump( var=dateRangeResults, label="Date Range Search" );
Trying to nail down the syntax for a pretty straightforward problem.
I have a table called Events and a full-feature DATES table with a relationship between the Dates[Date] field.
Using the event name as a slicer, I trying to create a [First Monday] measure that will return the date of the first Monday of the month.
So for example, if my event date was 2/14/19, [First Monday] would return 2/4/19.
Your Date table needs to contain 2 columns:
Year-Month: for example, "2018-01"
Weekday Number: for example, 1 (for Monday); or Weekday Name (i.e, "Monday")
Then:
First Monday =
CALCULATE( MIN('Date'[Date]),
ALL('Date'),
VALUES('Date'[Year-Month]),
'Date'[Weekday Name] = "Monday")
How it works:
First, we need to access all dates in the Date table, so we use ALL()
Second, we need to see only dates for the current context year and month, for which we can use VALUES()
Third, for each month we only want Mondays, hence Date[Weekday] = "Monday"
All this unfiltering/filtering generates a set of Mondays for the Year-Month visible in the current filter context. All we need to do now is to find the earliest of the Mondays using Min(Date).
I have to variables: year and month, both of type integer.
E.g:
year = 2016
month = 1
I want to, in my select statement, return a timestamp given those two variables.
I've had a look at the documentation, specifically to_timestamp and to_date, but all the examples I've come across show a string being converted into a timestamp.
I do not really want to convert my year and month into a string, such as:
to_timestamp(to_char(int,year) + ' ' + to_char(int, month),YYYY/MM/DD HH24:MI:SS)
So, how can I (if it is possible) convert my year and month into a timestamp?
If you are using Postgres 9.4 or later, you could try using make_timestamp():
make_timestamp(2016, 1, 1, 0, 0, 0.0)
This would create a timestamp for January 1, 2016 at midnight. We need to specify values for the other components, even if they end up not being relevant to your query/calculation (e.g. you only need the date).
I want to create a measure which return the maximum date about Orders but before the actual day
I will write an example :
My tables here
(In my table Calendar i have the year 2016,2017,2019, and in my Order table, i have an order for 2016 and 2019,
I want the last date order but before the actual day (18/05/2017), so i want the Date 01/01/2016).
I have 2 table, a dimension Calendar and a fact table Order.
I was thinking about the function filter, so i search how to use filter in
google, and all the solutions i found use 'With' and 'Select'.
(I can't use 'With' and 'Select' when i create a measure in SSAS multidimensional).
Hope i will see your advice.
Just like this similar case in adv cube?
[max order date] return the maximum date about [Internet Sales Amount]
with member [max order date] AS
tail(NONEMPTY([Date].[Date].[Date],[Measures].[Internet Sales Amount])).item(0).item(0).PROPERTIES( "name" )
select {[max order date] } on 0 from [Adventure Works]
if yes, then you can create a measure in your cube like this:
Create Member CurrentCube.[Measures].[max order date]
As tail(NONEMPTY([Date].[Date].[Date],[Measures].[Internet Sales
Amount])).item(0).item(0).PROPERTIES( "name" );
if only till current day, then(following is refer to adv cube, you need do some code changes per your cube):
Create Member CurrentCube.[max order date] AS
Tail
(
NonEmpty
(
{
Head([Date].[Date].[Date]).Item(0).Item(0)--the first day in your Date dim
:
StrToMember("[Date].[Date].&[" + Format(Now(),"yyyyMMdd") + "]")-- as of current day
}
,[Measures].[Internet Sales Amount]
)
).Item(0).Item(0).Properties("name")
IDE to Write, Analyze, Tuning, Debug MDX efficiently (www.mdx-helper.com)
I have a date dimension set in the SSAS Cube. Below is the screenshot attached.
I have been trying get quarter,year,month,semester start and end dates using ClosingPeriod() and OpeningPeriod() functions but not getting the exact value. How the get correct dates for a given date.
Syntax ClosingPeriod( [ Level_Expression [ ,Member_Expression ] ] )
You have to add calendar members ON 1. Example
WITH MEMBER [Measures].[X] AS
(
ClosingPeriod (
[Time].[Year Quarter Month].[Year Name],
[Time].[Year Quarter Month].CurrentMember
),
[Measures].[A]
)
SELECT [Measures].[X] ON 0,
[Time].[Year Quarter Month].[Year Name].Members ON 1
FROM [ABC]
I have month sorted by month name instead of the month number. After correction following query is working fine.
SELECT
{
OpeningPeriod([Dim Time].[Calendar].[Date],[Dim Time].[Calendar].[Date].&[2014-08-18T00:00:00].Parent.Parent.Parent.Parent)
} ON 0,
{
[Measures].[Ending Market Value Base]
} ON 1
FROM [Cube]