trouble with dynamic year filter in MDX - mdx

I have the following query that I use in Azure data factory(this is on the source of a copy action):
SELECT
{ [Measures].[0INV_QTY],
[Measures].[0NET_VAL_S] }
ON COLUMNS,
NON EMPTY
{ [0CUST_SALES].[LEVEL01].MEMBERS *
[0SALESORG].[LEVEL01].MEMBERS *
[0COMPANY].[LEVEL01].MEMBERS *
[0MATERIAL].[LEVEL01].MEMBERS *
[ZDEBITOR].[LEVEL01].MEMBERS *
[0FISCPER].[LEVEL01].MEMBERS *
[0DEB_CRED].[LEVEL01].MEMBERS *
[0BILLTOPRTY].[LEVEL01].MEMBERS *
[0DOC_CATEG].[LEVEL01].MEMBERS *
[0SHIP_TO].[LEVEL01].MEMBERS }
DIMENSION PROPERTIES
[0SALESORG].[20SALESORG],
[0COMPANY].[20COMPANY],
[0CUST_SALES].[80CUST_SALES],
[0CUST_SALES].[20CUST_GRP1],
[0CUST_SALES].[20PMNTTRMS],
[ZDEBITOR].[20CRED_LIMIT],
[0MATERIAL].[20MATERIAL],
[0DEB_CRED].[20DEB_CRED],
[0BILLTOPRTY].[20BILLTOPRTY],
[0DOC_CATEG].[20DOC_CATEG],
[0SHIP_TO].[20SHIP_TO],
[0FISCPER].[80FISCPER]
ON ROWS
FROM $0SD_C03
WHERE ({[0CALYEAR].[2020], [0CALYEAR].[2021], [0CALYEAR].[2018], [0CALYEAR].[2019]})
In here I would like to replace the WHERE with something like Cast(YEAR(GETDATE())-4 as varchar(10)) Now I am really new to MDX and I keep getting stuck. Could anyone point me in the right direction?
So what i want to achieve is not having to adjust the query every year and be able to only have the last 4 years.

If you are looking for a SQL equivalent as below in MDX
SELECT ... From ... WHERE date > DATEADD(year,-4,GETDATE())
Try using "with member" and function parallelperiod.
CREATE MEMBER CurrentCube.Measures.[Last4Years] AS
ParallelPeriod( [Date].[Date].[Date Yr], 4, StrToMember(“[Date].[Date].&[” + Format(now(), “yyyyMMdd”) + “]”))
: StrToMember(“[Date].[Date].&[” + Format(now(), “yyyyMMdd”) + “]”)
;

Related

how to get a value from the results of another column calculation in same table

I tried to retrieve a value from the calculation of several columns,
in this case try to apply the formula "(a + (a / 25 * b)) - c" to be processed using sql language which I will use in codeigniter.
I also tried using "derived table" like SELECT .... FROM (SELECT... FROM...) AS dt
but I had difficulty when applying it to my case in codeigniter
private function _get_datatables_query(){
$intvl = '2';
$tgl_stok = '2019-09-30';
$this->db->SELECT('p.hso, p.no_part, p.nama_part, jml, sp.oh, sum(p.qty)+(sum(p.qty)/25*$intvl)-sp.oh as suggest');
$this->db->FROM('penjualan p');
$this->db->JOIN('stok_part sp', 'sp.no_part = p.no_part', 'left');
$this->db->WHERE("sp.tgl = '$tgl_stok' AND p.tgl BETWEEN DATE_SUB('$tgl_stok', INTERVAL $intvl DAY) AND '$tgl_stok'");
$this->db->GROUP_BY('p.no_part');
//...other code...
}
I want a column with the alias suggest in the code to produce a calculated value of several other columns
I know writing code that I created is not in accordance with the rules of writing SQL, I tried a number of ways but it did not work. I am very grateful for your help
Instead of var you should use parameter in this way you can easly pass the value you need and avoid sqlinjection eg:
$sql = "SELECT p.hso, p.no_part, p.nama_part, jml, sp.oh, sum(p.qty)+(sum(p.qty)/25*?)-sp.oh as suggest
FROM enjualan p
LEFT JOIN stok_part sp ON sp.no_part = p.no_part
WHERE sp.tgl = ? AND p.tgl BETWEEN DATE_SUB(?, INTERVAL ? DAY) AND ?
GROUP BY p.no_part";
$this->db->query($sql, array($intval,$tgl_stok, $tgl_stok, $intvl, tgl_stok ));
}
I was solved this case.
the problem is in my query:
$this->db->SELECT('p.hso, p.no_part, p.nama_part, jml, sp.oh, sum(p.qty)+(sum(p.qty)/25*$intvl)-sp.oh as suggest');
and replace it with
$this->db->SELECT("p.hso, p.no_part, p.nama_part, sum(p.qty) as jml, sp.oh, sum(p.qty)+(sum(p.qty)/25*'$intvl')-sp.oh as s_po");
my mistake was writing quotation marks in the query

Why do I get a blank return during this SQL query?

SELECT dependents.[Dependent_name],
"Child of " + employee.[Lname] + "," + employee.[Fname] AS Dependence
FROM dependents, employees AS employee
WHERE employee.[Lname] LIKE 'W%'
AND employee.Ssn = dependents.Essn
AND NOT dependents.Relationship = "Spouse"
ORDER BY employee.[Lname], dependents.[Dependent_name];
I'm attempting to create a query that will return a table with 2 fields. Those being dependent_name and Dependence. The only thing that is not properly functioning is the LIKE condition and I do not know why. It causes the whole table to not return anything. It should return 2 entries
MS Access uses * as the wildcard in LIKE, not %. So, try this WHERE condition:
WHERE employee.[Lname] LIKE 'W*' AND
. . .

Named Set MDX Error

I'm having this error after creating a named set in SSAS to retrieve the last 10 weeks from the first day of current week. the expression I used is :
strtoset(
"LASTPERIODS(
10
,[AxeTemps].[Semaine].&["+Format(NOW()-WEEKDAY(NOW(),2),'yyyyMMdd')+"]")
My date hierarchy has the following members structure :
[AxeTemps].[Semaine].&[20000101].
I can't get this work, any idea ?
You have convert the a member first, then you can pass it to the LastPeriods():
LastPeriods(
10,
StrToMember("[AxeTemps].[Semaine].&[" + Format(NOW()-WEEKDAY(NOW(),2),'yyyyMMdd') + "]")
)

Parse a List to an MDX Query (List in MDX M Power BI)

I'm looking for a way to integrate a previously retrieved list as an input for a FILTER in a MDX query. I currently have the following code:
segment_var = Segment,
mdxQ = "SELECT
NON EMPTY ( { [Measures].[AMT] * [Forecast Type].[Forecast Type].[Forecast Level 2] } ) DIMENSION PROPERTIES MEMBER_NAME ON COLUMNS,
NON EMPTY ( {
[Period].[Year Month Name].[Name] *
[PL Spec].[PL Spec].[Level 6 Code].allmembers
} ) ON ROWS
FROM (
SELECT ( { [Forecast Type].[Current Indicator].&[Y] } ) ON COLUMNS
FROM (
SELECT ( { [PL Spec].[PL Spec].[Level 1 Code].&[101] } ) ON COLUMNS
FROM [AIR]))
WHERE (
FILTER([Management Structure - Segment].[Management Structure - Segment].[MS Level 5].ALLMEMBERS , [Management Structure - Segment].[Management Structure - Segment].currentmember.name=""" & segment_var & """))",
Segment on row 1 used to be a single value parameter but I want to use a list of segments (listSegment) as a reference instead so the result filters multiple segments. I've tried WHERE IN solutions but I can't seem to get it to work. Any help would be appreciated.
Error:
Expression.Error: We cannot apply operator & to types Text and List.
Try to generate segment_var as
{[Management Structure - Segment].[Management Structure - Segment].[1], [Management Structure - Segment].[Management Structure - Segment].[2], ...}
Managed to solve the issue in the following way:
Make the list of values to be filtered in the query
Transpose and combine the list using ", "
Convert to single variable
Code:
Convert =
Table.ToList(
Table.Transpose(
Table.FromList(#"Distinct")),
Combiner.CombineTextByDelimiter(", ")),
Var = List.First(Convert)

MDX Date Formatting

Can any one please tell me how to format date in MDX queries? We dont use SSRS to generate report ,we have our own customised reporting tool built on SSAS.Date filter sends date in yyyy/mm/dd format . As of now we dont have a date dimension. My date member looks like:
[CNB_DimSampleInfo].[COAReleasedON].&[2013-01-02T03:20:00].
How can I format date in STRTOmemeber? I have tried doing this. My question is how will the value coming from user suit my member format as below. I know ssrs does it easily but we are not using SSRS. Below is my Code
my code
SELECT
[Measures].[Result] ON COLUMNS
,NON EMPTY
{
[CNB_DimProduct].[ProductUcode].[ProductUcode].ALLMEMBERS*
[CNB_DimProduct].[ProductDesc].[ProductDesc].ALLMEMBERS*
[CNB_DimTest].[TestUcode].[TestUcode].ALLMEMBERS*
[CNB_DimTest].[TestName].[TestName].ALLMEMBERS*
[CNB_DimSampleInfo].[LotNo].[LotNo].ALLMEMBERS*
[CNB_DimSampleInfo].[BatchNo].[BatchNo].ALLMEMBERS*
[CNB_DimSampleInfo].[COAReleasedBy].[COAReleasedBy].ALLMEMBERS*
[CNB_DimSampleInfo].[COAReleasedON].[COAReleasedON].ALLMEMBERS*
[CNB_DimSampleInfo].[SampleReferenceNo].[SampleReferenceNo].ALLMEMBERS*
[CNB_DimSampleInfo].[AnalysedBy].[AnalysedBy].ALLMEMBERS*
[CNB_DimSampleInfo].[AnalysedOn].[AnalysedOn].ALLMEMBERS
} ON ROWS
FROM
(
SELECT
StrToMember
(
"[CNB_DimSampleInfo].[COAReleasedON].[" + Format("2013-01-02","yyyy MM")
+ "]:STRTOMember([CNB_DimSampleInfo].[COAReleasedON].["
+
Format
("2013-01-02"
,"yyyy MM"
)
+ "]"
) ON COLUMNS
FROM Cube001
);
There is also StrToSet which is better in your circumstance as you're using the : operator which returns a set:
...
...
FROM
(
SELECT
StrToSet
(
"[CNB_DimSampleInfo].[COAReleasedON].[" + Format("2013-01-02","yyyy MM")
+ "]:[CNB_DimSampleInfo].[COAReleasedON].["
+
Format
("2013-01-02"
,"yyyy MM"
)
+ "]"
,CONSTRAINED
) ON COLUMNS
FROM Cube001
);
does the following definitely return the date formatted the same as your key values?
Format("2013-01-02","yyyy MM")
Do your key values for dates look like this?...
[CNB_DimSampleInfo].[COAReleasedON].[2013 01]
Your date member
[CNB_DimSampleInfo].[COAReleasedON].&[2013-01-02T03:20:00]
looks like a bad candidate for a user date parameter, as it's precise down to the minute (perhaps the second). Unless the user exactly matches the time, they'll get nothing. But perhaps you're enforcing exact matches by using a LimitToList select list in the UI.
To get this member name, you can format the input string like this:
format([Your Input Parameter],'yyyy-MM-ddThh:mm:ss')
I have tried out using filter as below
SELECT
[Measures].[Result] ON COLUMNS
,NON EMPTY
{
filter([CNB_DimSampleInfo].[COAReleasedON].members,instr([CNB_DimSampleInfo].[COAReleasedON].currentmember.member_caption,"2013-01-02")>0 or instr([CNB_DimSampleInfo].[COAReleasedON].currentmember.member_caption, "2013-04-01")>0)
*[CNB_DimProduct].[ProductUcode].[ProductUcode].ALLMEMBERS*
[CNB_DimProduct].[ProductDesc].[ProductDesc].ALLMEMBERS*
[CNB_DimTest].[TestUcode].[TestUcode].ALLMEMBERS*
[CNB_DimTest].[TestName].[TestName].ALLMEMBERS
} ON ROWS
FROM Cube002