MDX where Date is less than now - mdx

This is my code below......
SELECT
{
[Measures].[ACPPurchaseValue]
} ON COLUMNS
,(
[Date].[YYYYMMDD].[YYYYMMDD]
) ON ROWS
FROM [Kahuna]
WHERE
(
[Reporting Currency].[reportingCurrency].&[1]
,strToSet(#MdxBOSP)
,strToSet(#MdxVIPType)
,strToSet(#MdxHost)
,strToSet(#MdxOperatorName)
);
How can I say where [Date].[YYYYMMDD].[YYYYMMDD] < getdate()

You need to initially create a member, or a single member set, that corresponds to today.
The following looks a little complex but is actually a fairly standard approach - put forward by Tomislav Piasevoli - it is against the AdvWrks dimension Date:
WITH
MEMBER [Measures].[Key for Today] AS
Format
(
Now()
,'yyyyMMdd'
)
MEMBER [Measures].[Today string] AS
'[Date].[Calendar].[Date].&[' + [Measures].[Key for Today] + ']'
SET [Today] AS
StrToMember
(
[Measures].[Today string]
,constrained
)
...
So applying to your situation:
WITH
MEMBER [Measures].[Key for Today] AS
Format
(
Now()
,'yyyyMMdd'
)
MEMBER [Measures].[Today string] AS
'[Date].[YYYYMMDD].[YYYYMMDD].&[' + [Measures].[Key for Today] + ']'
SET [Today] AS
StrToMember
(
[Measures].[Today string]
,constrained
)
SELECT
[Measures].[ACPPurchaseValue] ON 0
,{null:[Today].item(0).item(0)} ON 1
FROM [Kahuna]
WHERE
(
[Reporting Currency].[reportingCurrency].&[1]
,strToSet(#MdxBOSP)
,strToSet(#MdxVIPType)
,strToSet(#MdxHost)
,strToSet(#MdxOperatorName)
);
Two other better solutions which could simplify life for everyone:
Do not materialize future dates in your cube
Leave future dates in, but add a custom set called [Today] and a calculated member called today as a child to Date's All member.

Related

MDX : ParallelPeriod and leap year

I have data for several years.
I have a problem with February this year when I try to get last year.
I think it's because 2020 is a leap year. But I have no clue how to solve this. I tried a lot of things.
My request is like that :
IIF
(
Iserror
(
Sum
(
YTD
(
ParallelPeriod
(
[Date Facture].[Mensuel].[Année]
,1
,StrToMember
("[Date Facture].[Mensuel].[Date].&["
+
Tail
(
(EXISTING
Descendants
(
[Date Facture].[Mensuel].CurrentMember,
,leaves
))
).Item(0).Member_Key
+ "]"
)
)
)
,[Measures].[Quantité Facturée AEC]
)
)
,null
,Sum
(
YTD
(
ParallelPeriod
(
[Date Facture].[Mensuel].[Année]
,1
,StrToMember
("[Date Facture].[Mensuel].[Date].&["
+
Tail
(
(EXISTING
Descendants
(
[Date Facture].[Mensuel].CurrentMember,
,leaves
))
).Item(0).Member_Key
+ "]"
)
)
)
,[Measures].[Quantité Facturée AEC]
)
)
What can I do to solve this?
Can you not just simplify to use the CURRENTMEMBER function?
IIF
(
Iserror
(
Sum
(
YTD
(
ParallelPeriod
(
[Date Facture].[Mensuel].[Année]
,1
,[Date Facture].[Mensuel].CURRENTMEMBER
)
)
,[Measures].[Quantité Facturée AEC]
)
)
,null
,Sum
(
YTD
(
ParallelPeriod
(
[Date Facture].[Mensuel].[Année]
,1
,[Date Facture].[Mensuel].CURRENTMEMBER
)
)
,[Measures].[Quantité Facturée AEC]
)
)

get the date along with year and month

The date condition below is good for fetching current year and current month I would like to add current date to it ,please append the query to get current date .
If (( Extract ( Month, current_date )) = 1)
Then ( Cast ( ( Cast((( Extract (Year, current_date) -1)), varchar(4)) || '12'), int) )
Else ( If ((Extract ( Month, current_date) -1) < 10 )
Then ( Cast(( ( Cast ((Extract ( Year, current_date )), varchar(4))) || '0' || ( Cast (( Extract ( Month, current_date) - 1), varchar(2)))), int) )
Else ( Cast (( Cast ((Extract ( Year, current_date )), varchar(4)) || ( Cast(( Extract ( Month, current_date) - 1), varchar(2)))), int) )
)
thanks in advance
Your code is Cognos right? I can't help you there, but in DB2, this will get you current year, current month and current date.
values (year(current date), month(current date), current date)
e.g. it will return
1 2 3
---- - ----------
2018 3 2018-03-21

MDX Calculation in SSAS for Sum of Sales from Start date till today

I am trying to write a query in ssas calculations tab for which should produce the below result. It is like a YTD calculation which i am calculating from 1st Feb 2016 till today. I have written the below query in Management Studio but i need to convert it into SSAS calculations and write it into Calculations tab.
WITH
MEMBER [Measures].[ytd Sales Target 2] AS
Sum
(
StrToMember
(
'[Sales Date].[Date].&[' + Format(Now(),'yyyy-') + '02-01T00:00:00]'
)
:
StrToMember
(
'[Sales Date].[Date].&[' + Format(Now(),'yyyy-') + Format(Now(),'MM-')
+
Format
(
Now()
,'dd'
)
+ 'T00:00:00]'
)
,[Measures].[sales target]
)
SELECT
[Measures].[ytd Sales Target 2] ON 0
FROM [sales];
It should be as simple as:
CREATE MEMBER CURRENTCUBE.[Measures].[ytd Sales Target 2]
AS
Sum
(
StrToMember
(
'[Sales Date].[Date].&[' + Format(Now(),'yyyy') + '-02-01T00:00:00]'
)
:
StrToMember
(
'[Sales Date].[Date].&[' + Format(Now(),'yyyy-MM-dd')+'T00:00:00]'
)
,[Measures].[sales target]
),
VISIBLE = 1;
This way won't take your Date into account. But with a little change it can be "time aware". I added a little check for dates before 1st Feb to be ignored.
CREATE MEMBER CURRENTCUBE.[Measures].[ytd Sales Target 2]
AS
iif(
[Sales Date].[Date].CURRENTMEMBER.MEMBER_KEY < StrToMember('[Sales Date].[Date].&[' + Format(Now(),'yyyy') + '-02-01T00:00:00]').MEMBER_KEY
,NULL
,Sum (
StrToMember
(
'[Sales Date].[Date].&[' + Format(Now(),'yyyy') + '-02-01T00:00:00]'
)
:
StrToMember
(
[Sales Date].[Date].CURRENTMEMBER
)
,[Measures].[sales target]
)
),
VISIBLE = 1;

Sum by month in SQL

I am trying to sum my daily data (trunc(DM_OWNER.LD_LEG_DW.CPLD_DTT) by month. This is created in a Web Intelligence BI report.
SELECT
DM_OWNER.LD_LEG_DW.LGST_GRP_CD,
DM_OWNER.LGST_GRP_T.LGSTGRP_DESC,
DM_OWNER.LD_LEG_DW.CARR_CD,
DM_OWNER.LD_LEG_DW.LD_LEG_ID,
DM_OWNER.LD_LEG_DW.BILL_TO_CUST_CD,
trunc(DM_OWNER.LD_LEG_DW.CPLD_DTT),
trunc(DM_OWNER.VCHR_AP_T.CRTD_DTT),
AP_Detail.PYMNT_AMT_DLR
FROM
DM_OWNER.LD_LEG_DW,
DM_OWNER.LGST_GRP_T,
DM_OWNER.VCHR_AP_T,
DM_OWNER.CHRG_DETL_T_NOTOTAL_V AP_Detail
WHERE
( DM_OWNER.LD_LEG_DW.LD_LEG_ID=DM_OWNER.VCHR_AP_T.LD_LEG_ID(+) )
AND ( DM_OWNER.VCHR_AP_T.VCHR_NUM=AP_Detail.VCHR_NUM_AP )
AND ( DM_OWNER.LD_LEG_DW.LGST_GRP_CD=DM_OWNER.LGST_GRP_T.LGST_GRP_CD )
AND
(
DM_OWNER.LD_LEG_DW.LGST_GRP_CD In ( 'PBRK' )
AND
trunc(DM_OWNER.VCHR_AP_T.CRTD_DTT) >= '01-10-2012 00:00:00'
)
Without clear field names, or an explanation of the data structure, this is too vague. But the principle is
select
displayfields,
year(datefield), month(datefield),
sum(valuefield)
from yourtable
group by
displayfields,
year(datefield), month(datefield),
Without getting into the specifics of your query, here's a general approach.
The CAST is nasty, but this has the benefit of giving you an actual DATETIME as a grouper.
SELECT
SUM(Amount) AS MonthTotal,
CAST(CAST(YEAR(DateCollected) AS VARCHAR(4))
+ '/' + CAST(MONTH(DateCollected) AS VARCHAR(2))
+ '/01' AS DATETIME) AS PaymentMonth
FROM
PaymentsReceived
GROUP BY
CAST(CAST(YEAR(DateCollected) AS VARCHAR(4))
+ '/' + CAST(MONTH(DateCollected) AS VARCHAR(2)) + '/01' AS DATETIME)

Need help with SQL to calculate if store is open or not

I have a list of storecodes in table "Stores" and another table "StoreClosedDates" which tells me if the store is closed on a Saturday or Sunday. My StoreOpenDates table looks like this:
CREATE TABLE [dbo].[StoreClosedDates](
[StoreCode] [varchar](50) NOT NULL,
[ClosedOnSunday] [bit] NOT NULL,
[ClosedOnSaturday] [bit] NOT NULL
) ON [PRIMARY]
This table needs to be changed later to include holiday dates as well, so that those can be covered as well. I am not entirely surely how I can change this table to cover both options (holidays and weekends). Now I need to write a query which returns me a list of stores that are open for the current date. I am not sure how to compare for the weekend in the where clause - I know I should be using: DATEPART ( dw , getdate() ), but I cant seem to see the entire picture to solve it.
The StoreClosedDates contains only stores that closed. If a store is not present in that table, then the store is open for the current date.
SELECT *
FROM StoreClosedDates
WHERE NOT (DATEDIFF(day, GETDATE(), '2007/01/01') % 7 = 5 AND ClosedOnSaturday = 1)
AND NOT (DATEDIFF(day, GETDATE(), '2007/01/01') % 7 = 6 AND ClosedOnSunday = 1)
Better avoid usage of DATEPART, because it's locale dependent.
To check for the fixed date holidays, create a table with two separate columns containing the month and the date:
CREATE TABLE holiday (
hmon TINYINT, hday TINYINT,
PRIMARY KEY (hmon, hday),
CHECK(CAST('2008-' + CAST(hday AS VARCHAR) + '-' + CAST(hmon AS VARCHAR) AS DATETIME) > 0)
)
and use it in a query:
SELECT *
FROM Stores
WHERE id NOT IN
(
SELECT StoreID
FROM StoreClosedDates
WHERE NOT (DATEDIFF(day, GETDATE(), '2007/01/01') % 7 = 5 AND ClosedOnSaturday = 1)
AND NOT (DATEDIFF(day, GETDATE(), '2007/01/01') % 7 = 6 AND ClosedOnSunday = 1)
)
AND NOT EXISTS
(
SELECT NULL
FROM Holidays
WHERE hday = DAY(GETDATE())
AND hmon = MONTH(GETDATE())
)
Does it need to be in that table? Store the Holidays in a separate table and you can join to get to get the functionality you need.
SELECT * FROM Stores WHERE StoreCode NOT IN
(
SELECT StoreCode FROM StoreClosedDates
WHERE
(
DATEPART ( dw , getdate() ) = 1 AND ClosedOnSunday = 1
) OR
(
DATEPART ( dw , getdate() ) = 7 AND ClosedOnSaturday = 1
)
)
This assumes your DatePart call returns 1 for sunday (first day of week) and 7 for saturday. I could be wrong on that, perhaps its 0 and 6, and it also depends on what you have set as your "First Day of week"