MDX - How to "Summarize" a Filtered result set - filter

i'm struggling to determine the proper MDX query syntax in order to return the expected result set after moving the filters in order to support advanced nested filtering.
The query below is what is being generated today and the screenshot shows the results. Does anyone have any insights as to how to get the results needed as described in the screenshot based on the query below.
Note - i believe the Filters need to stay where they are in order to support nested complex filters. i.e. ((A=1 OR B<1) AND (A>1 OR C=2)) || (B=3 AND C=4)
WITH
SET AllRowsSet AS
{
Filter
(
NonEmpty
(
called_cities.[called_city].Children*
called_countries.[called_country_name].Children*
billing_months.[billing_month].Children*
sources_pivot_v1.[source_name_l1].Children*
products_pivot.[product_name_l1].Children*
products_pivot.[product_name_l2].Children
,{
[Measures].[actual_duration]
,[Measures].[amount]
}
)
,
billing_months.[billing_month].CurrentMember.Member_Caption = '201805'
AND products_pivot.[product_name_l1].CurrentMember.Member_Caption = 'Usage'
AND products_pivot.[product_name_l2].CurrentMember.Member_Caption = 'LD International'
AND called_countries.[called_country_name].CurrentMember.Member_Caption <> 'TEST_NULL'
AND (
sources_pivot_v1.[source_name_l1].CurrentMember.Member_Caption = 'Company for Joint'
OR sources_pivot_v1.[source_name_l1].CurrentMember.Member_Caption = 'Cisco Call Manager'
)
)
}
MEMBER [Measures].totalrows AS AllRowsSet.Count
MEMBER [Measures].[called_city] AS called_cities.[called_city].CurrentMember.Member_Caption
MEMBER [Measures].[called_country_name] AS called_countries.[called_country_name].CurrentMember.Member_Caption
MEMBER [Measures].[billing_month] AS billing_months.[billing_month].CurrentMember.Member_Caption
MEMBER [Measures].[source_name_l1] AS sources_pivot_v1.[source_name_l1].CurrentMember.Member_Caption
MEMBER [Measures].[product_name_l1] AS products_pivot.[product_name_l1].CurrentMember.Member_Caption
MEMBER [Measures].[product_name_l2] AS products_pivot.[product_name_l2].CurrentMember.Member_Caption
SELECT
{
[Measures].[actual_duration]
,[Measures].[amount]
,[Measures].totalrows
,[Measures].[called_city]
,[Measures].[called_country_name]
,[Measures].[billing_month]
,[Measures].[source_name_l1]
,[Measures].[product_name_l1]
,[Measures].[product_name_l2]
} ON COLUMNS
,SubSet
(
Order
(
Filter
(
NonEmpty
(
called_cities.[called_city].Children*
called_countries.[called_country_name].Children*
billing_months.[billing_month].Children*
sources_pivot_v1.[source_name_l1].Children*
products_pivot.[product_name_l1].Children*
products_pivot.[product_name_l2].Children
,{
[Measures].[actual_duration]
,[Measures].[amount]
}
)
,
billing_months.[billing_month].CurrentMember.Member_Caption = '201805'
AND products_pivot.[product_name_l1].CurrentMember.Member_Caption = 'Usage'
AND products_pivot.[product_name_l2].CurrentMember.Member_Caption = 'LD International'
AND called_countries.[called_country_name].CurrentMember.Member_Caption <> 'TEST_NULL'
AND (
sources_pivot_v1.[source_name_l1].CurrentMember.Member_Caption = 'Company for Joint'
OR sources_pivot_v1.[source_name_l1].CurrentMember.Member_Caption = 'Cisco Call Manager'
)
)
,[Measures].[amount]
,BDESC
)
,0
,250
) ON ROWS
FROM
(
SELECT {billing_months.[v1_disabled].[v1_disabled].&[0]} ON 0 FROM calls
);

To aggregate you can -
1. Move the ON ROWS set into the WITH clause
2. Wrap it in the Aggregate
3. You will need to find a 'spare' dimension in your cube that is not being used in this query to create this new custom MEMBER:
WITH
SET AllRowsSet AS
{
Filter
(
NonEmpty
(
called_cities.[called_city].Children*
called_countries.[called_country_name].Children*
billing_months.[billing_month].Children*
sources_pivot_v1.[source_name_l1].Children*
products_pivot.[product_name_l1].Children*
products_pivot.[product_name_l2].Children
,{
[Measures].[actual_duration]
,[Measures].[amount]
}
)
,
billing_months.[billing_month].CurrentMember.Member_Caption = '201805'
AND products_pivot.[product_name_l1].CurrentMember.Member_Caption = 'Usage'
AND products_pivot.[product_name_l2].CurrentMember.Member_Caption = 'LD International'
AND called_countries.[called_country_name].CurrentMember.Member_Caption <> 'TEST_NULL'
AND (
sources_pivot_v1.[source_name_l1].CurrentMember.Member_Caption = 'Company for Joint'
OR sources_pivot_v1.[source_name_l1].CurrentMember.Member_Caption = 'Cisco Call Manager'
)
)
}
MEMBER [Measures].totalrows AS AllRowsSet.Count
MEMBER [Measures].[called_city] AS called_cities.[called_city].CurrentMember.Member_Caption
MEMBER [Measures].[called_country_name] AS called_countries.[called_country_name].CurrentMember.Member_Caption
MEMBER [Measures].[billing_month] AS billing_months.[billing_month].CurrentMember.Member_Caption
MEMBER [Measures].[source_name_l1] AS sources_pivot_v1.[source_name_l1].CurrentMember.Member_Caption
MEMBER [Measures].[product_name_l1] AS products_pivot.[product_name_l1].CurrentMember.Member_Caption
MEMBER [Measures].[product_name_l2] AS products_pivot.[product_name_l2].CurrentMember.Member_Caption
MEMBER [SpareDimension].[SpareHierarchy].[myAggregation] AS //<< REPLACE <[SpareDimension].[SpareHierarchy]> WITH A VALID DIMENSION/HIERARCHY IN YOUR CUBE THAT IS NOT BEING USED IN THE SCRIPT
Aggregate(
SubSet
(
Order
(
Filter
(
NonEmpty
(
called_cities.[called_city].Children*
called_countries.[called_country_name].Children*
billing_months.[billing_month].Children*
sources_pivot_v1.[source_name_l1].Children*
products_pivot.[product_name_l1].Children*
products_pivot.[product_name_l2].Children
,{
[Measures].[actual_duration]
,[Measures].[amount]
}
)
,
billing_months.[billing_month].CurrentMember.Member_Caption = '201805'
AND products_pivot.[product_name_l1].CurrentMember.Member_Caption = 'Usage'
AND products_pivot.[product_name_l2].CurrentMember.Member_Caption = 'LD International'
AND called_countries.[called_country_name].CurrentMember.Member_Caption <> 'TEST_NULL'
AND (
sources_pivot_v1.[source_name_l1].CurrentMember.Member_Caption = 'Company for Joint'
OR sources_pivot_v1.[source_name_l1].CurrentMember.Member_Caption = 'Cisco Call Manager'
)
)
,[Measures].[amount]
,BDESC
)
,0
,250
)
)
SELECT
{
[Measures].[actual_duration]
} ON COLUMNS
,[SpareDimension].[SpareHierarchy].[myAggregation] ON ROWS
FROM
(
SELECT {billing_months.[v1_disabled].[v1_disabled].&[0]} ON 0 FROM calls
);

Related

How to count two column values

I want count two columns using power bi to create a visual.my measure as below
Test Measure = COUNTA('Export'[Line])+COUNTA('Export'[Line 2]),
You need to create a calculated table with all the lines values, like this
TableValues =
DISTINCT ( UNION ( DISTINCT ( 'Table'[Line] ), DISTINCT ( 'Table'[Line_2] ) ) )
With that table done, you can write the following measure
CountValues =
VAR _CurrentValue =
SELECTEDVALUE ( TableValues[Line] )
VAR _C1 =
CALCULATE ( COUNTA ( 'Table'[Line] ), 'Table'[Line] = _CurrentValue )
VAR _C2 =
CALCULATE ( COUNTA ( 'Table'[Line_2] ), 'Table'[Line_2] = _CurrentValue )
RETURN
_C1 + _C2
The calculations count all the instances of a specific Line. It's important that the TableValues doesn't have any relationship with the other tables.
Output
Using 'TableValues'[Line] as the Line and CountValues as the metric.

Function 'SAMEPERIODLASTYEAR' expects a contiguous selection Issue

I have a tabular data model in Visual Studio, I made a formula to get last year's sales over a given period:
Selected Measure LY:= CALCULATE([Selected Measure], SAMEPERIODLASTYEAR('date' [date]))
I made a formula to have the decomposition of all the months in the 'date' table:
FY_Month='date' [FY Month No]&'date' [Month_label]
And:
FY_Month_No=format(if('date'[month_no]>6,'date'[month_no]-6,'date'[month_no]+6), "00")
Month_label : all the months of the year
The "Selected Measure LY" display works fine when I have all months selected but when I remove one I get this error message:
Calculation error in "Selected Measure LY": the "SAMEPERIODLASTYEAR" function expects a contiguous selection when the date column comes from a tabkle on side 1 of a bidirectional relationship.
Between my 'date' table and my sales table I have a 1/N relationship, I tried to change this relationship too but the error always comes back.
I looked for solutions on forums and I found this but I don't find the same numbers as with my first formula:
Selected Measure LY:=
CALCULATE([Selected Measure],
FILTER (
ALL ( 'date' ),
YEAR ( 'date'[date] ) = YEAR ( TODAY () )
&& 'date'[date] <= TODAY ()
)
)
My code for Selected Measure:
Selected Measure:=
VAR hasFilter =
HASONEFILTER ( 'Measure Selection'[Measure Name] )
VAR selMeas =
SELECTEDVALUE ( 'Measure Selection'[Measure Name], BLANK () ) /*SWITCH works slow, so using many IFs*/
VAR preCalc =
IF (
selMeas = "Units",
[Sku Piece Quantity],
IF (
selMeas = "Net Sales",
[Euro Net Amount],
IF (
selMeas = "Gross Sales",
[Euro Gross Amount],
IF (
selMeas = "Average Wholesale",
[Average_Wholesale],
IF (
selMeas = "Average Units",
[Average Units],
IF (
selMeas = "No of Styles",
[Style Count],
IF (
selMeas = "No of Colours",
[Colour Count],
IF (
selMeas = "ROD by Style",
[ROD by Style],
IF (
selMeas = "Standard Cost",
[Sum_Cost_Base],
IF (
selMeas = "Net Margin",
[Net_Margin],
IF (
selMeas = "Gross Margin",
[Gross_Margin],
IF ( selMeas = "ROD by Colour", [ROD by Colour], BLANK () )
)
)
)
)
)
)
)
)
)
)
)
VAR calc =
IF ( hasFilter, preCalc, BLANK () )
RETURN
calc
How can I correct it?

How Crossjoin two tables in Dax created from Generateseries?

I want to crossjoin to "table" created with GenererateSeries in a single Expression
Table =
VAR TableAnnee =
GENERATESERIES ( 2010; 2020; 1 )
VAR TableMois =
GENERATESERIES ( 1; 12; 1 )
RETURN
CROSSJOIN ( TableAnnee; TableMois )
Then I have an error message:
"CROSSJOIN function does not authorize two columns with the same name: [Value]."
How can I crossjoin TableAnnee and TableMois without creating two real tables but in one expression in that table?
You can use the SELECTCOLUMNS function to give your generated tables appropriate column names. The default column name is [Value], so you need to rename that default to do the crossjoin.
Table =
VAR TableAnnee =
SELECTCOLUMNS ( GENERATESERIES ( 2010; 2020 ); "Year"; [Value] )
VAR TableMois =
SELECTCOLUMNS ( GENERATESERIES ( 1; 12 ); "Month"; [Value] )
RETURN
CROSSJOIN ( TableAnnee; TableMois )

QueryBuilder : how to compose subselect clause

I'm having trouble converting a plain SQL clause to a Doctrine query.
I've added the clause in plain SQL below:
$query = $doc->getEntityManager ()
->createQueryBuilder ()
->select ( 'r')
->from ( 'AppBundle:CFormResponse', 'r' )
->where ( 'r.formId = :id AND NOT (select f.private from cform f where f.FORM_ID = :id) ' )
->setParameter ( 'id', $formId )->getQuery ();
$result = $query->getResult();
My attempt to convert this clause into DQL is as follows (so far):
$qb = $doc->getEntityManager ()->createQueryBuilder ();
$query = $qb
->select ( 'r')
->from ( 'AppBundle:CFormResponse', 'r' )
->where ( $qb->expr()->andX(
$qb->expr()->eq('r.formId', ':id'),
$qb->expr()->addSelect('(SELECT f.private
FROM AppBundle:CForm f
WHERE f.formId = :id)'
)
) )
->setParameter ( 'id', $formId )->getQuery ();
$result = $query->getResult();
with the addSelect indicating where I'm stuck.
Can anyone point me in the direction of how to do this?
Basically, you need to add not to your addSelect:
$query = $qb
->select ( 'r')
->from ( 'AppBundle:CFormResponse', 'r' )
->where ( $qb->expr()->andX(
$qb->expr()->eq('r.formId', ':id'),
$qb->expr()->not(
$qb->expr()
->select(`f.private`)
->from('AppBundle:CForm', 'f')
->where('f.formId = :id'))
)
) )
->setParameter ( 'id', $formId )->getQuery ();

Error Calling DB2 Stored Procedure

I defined a stored procedure with input and output parameters and am getting the following error when I try to call the stored procedure.
[SQL0312] Variable KUNNR not defined or not usable.
Here is the call:
CALL R3QA6DATA.SP_ADDRESS_CHANGES ('1999-12-31 23:59:59', '2016-06-01 23:59:59', :KUNNR,:KUNN2,:NAME1,:NAME2,:STRAS,:ORT01,:REGIO,:PSTLZ,:LAND1,:TELF1,:TELFX,:DEFPA)
GO
Here is the stored procedure:
CREATE PROCEDURE R3QA6DATA.SP_ADDRESS_CHANGES
(IN STARTDATE TIMESTAMP, IN ENDDATE TIMESTAMP,
OUT KUNNR GRAPHIC(10), OUT KUNN2 GRAPHIC(10), OUT NAME1 GRAPHIC(35), OUT NAME2 GRAPHIC(35), OUT STRAS GRAPHIC(35), OUT ORT01 GRAPHIC(35),
OUT REGIO GRAPHIC(3), OUT PSTLZ GRAPHIC(10), OUT LAND1 GRAPHIC(3), OUT TELF1 GRAPHIC(16), OUT TELFX GRAPHIC(31), OUT DEFPA GRAPHIC(1) )
LANGUAGE SQL
BEGIN
SELECT DISTINCT
knvp.kunnr
, knvp.kunn2
, kna1.name1
, kna1.name2
, kna1.stras
, kna1.ort01
, kna1.regio
, kna1.pstlz
, kna1.land1
, kna1.telf1
, kna1.telfx
, knvp.defpa
INTO KUNNR, KUNN2, NAME1, NAME2, STRAS, ORT01, REGIO, PSTLZ, LAND1, TELF1, TELFX, DEFPA
FROM
R3QA6DATA.KNA1 AS kna1
INNER JOIN
R3QA6DATA.ZMBCM AS zmbcm
ON
kna1.KUNNR = zmbcm.KUNAG
INNER JOIN
R3QA6DATA.KNVV AS knvv
ON
( kna1.KUNNR = knvv.KUNNR )
INNER JOIN
R3QA6DATA.KNVP AS knvp
ON
(
knvv.KUNNR = knvp.KUNNR
AND
knvv.VKORG = knvp.VKORG
AND
knvv.VTWEG = knvp.VTWEG
AND
knvv.SPART = knvp.SPART
)
WHERE
kna1.MANDT = '010'
AND
knvp.PARVW IN ('WE', 'AG')
AND
(
knvv.VKORG = zmbcm.VKORG
AND
knvv.VTWEG = zmbcm.VTWEG
AND
knvv.SPART = zmbcm.SPART
)
AND
kna1.loevm = ' '
AND
knvv.loevm = ' '
AND
knvp.KUNN2 IN
(
SELECT
SUBSTRING(bdcp2.tabkey, 4, 10)
FROM
R3QA6DATA.BDCP2 AS bdcp2
WHERE
bdcp2.mestype = 'DEBMAS'
AND
(
( bdcp2.tabname = 'KNA1'
AND
bdcp2.fldname IN
('NAME1'
, 'NAME2'
, 'STRAS'
, 'ORT01'
, 'REGIO'
, 'LAND1'
, 'PSTLZ'
, 'TELF1'
, 'TELFX') )
OR
( bdcp2.tabname = 'KNVP' )
)
AND
(
bdcp2.cretime > STARTDATE
AND
bdcp2.cretime < ENDDATE
)
)
AND
zmbcm.STOREID = 4 ;
END
Go
In RPGLE or COBOL this issue usually happens if you have the variables defined in an external source and accessed through COPY statement. If this is the case in your problem then try using the source through INCLUDE.

Resources