How to find the quarter,year,month,semester start and end dates - date

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]

Related

Tableau Calculated Field of Running Average for the last month

Hey Guys I am trying to do a calculation for a Running Average over the last month in a calculated field in Tableau. However I struggle with defining the last month range and adding it to the calculation. I thought of using
IF [date] >= DATEADD('month', -1, TODAY()) THEN
RUNNING_AVG(SUM( IF [Entry Type] = 'Sale'
THEN [Invoiced Quantity]
ELSE 0
END )) END
However this is not working at all. I hope someone can help me out. Cheers
This worked out:
(SUM( IF [Entry Type] = 'Sale' AND [Posting Date] >= DATEADD('month',-1,TODAY())
THEN [Invoiced Quantity]
ELSE 0
END
))
You do not have to write this into calculated field. Just filter date column and set that to be always filter for last 30 days, or last month. Then, use "calculated field" section just for calculation independently from the "date" which is already considered in filter section.
Good luck

How to get month value using week value hive

I want to get month value using week no. I have week numbers stored in a table with year value
How to query database to get month value using that week value.
I am using hiveQL.
Eample:
201801(week) ------->201801(month)
201804(week) ------->201801(month)
Suppose week has full 7 days. Extract week number, multiply 7, add to 'year-01-01' and extract month from result date, lpad with 0 to get '01' from 1 and concatenate with year:
with data_example as(
select stack(3,'201801','201804','201805') as yr_wk
)
select yr_wk, concat(substr(yr_wk, 1,4),
lpad(month(date_add(date(concat(substr(yr_wk, 1,4),'-01-01')),int(substr(yr_wk, 5,2))*7)),2,0)
) yr_mth
from data_example
Result:
yr_wk yr_mth
201801 201801
201804 201801
201805 201802

How to pass a date into a ColdFusion collection to be searched

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" );

Create a measure to return the maximum date about the order in a fact table. (SSAS Multidimensional)

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)

SSAS 2012 How to use ParallelPeriod without Date Hierarchy?

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]
)
)