SSAS MDX Multiple Criteria in Where Clause - ssas

How do you add multiple criterias for different dimensions of a MDX where clause?
For example with a normal SQL query you can use AND or OR to add multiple criteria:
Select * From MyTable Where Col1 = 'A' AND Col2 'B'
I've tried the same in MDX and it's not working. For example:
SELECT
NON EMPTY { [Measures].[Amount] } ON COLUMNS,
NON EMPTY { (
[Store].[Division].[Division].ALLMEMBERS *
[Department].[Dept].[Dept].ALLMEMBERS *
[Accounts].[Level 1].[Level 1].ALLMEMBERS *
[Years].[Year].[Year].ALLMEMBERS
) } ON ROWS
FROM
[Finance]
WHERE
{[Years].[Year].&[2018]} AND
{[Accounts].[Level 1].&[Sales]}

Try building a tuple like:
WHERE
([Years].[Year].&[2018],
[Accounts].[Level 1].&[Sales])

Related

Can I use filter on a formula within a postgres statement?

I have a weird query where I am aggregating some results and others I need to do formulas on but for the formulas I need to filter the results, but it's not working for me.
Here's my statement:
((table.grossprofit - table.totalrevenue) FILTER (WHERE lower(table.ticker) = 'aapl.us') as numeric) AS company_cogs
I get :
ERROR: syntax error at or near "FILTER"
What is the best way to do formulas while also filtering using a where statement?
Filter using with aggregation function. I don't see any aggregation function here.
see documentation
aggregate_name ( * ) [ FILTER ( WHERE filter_clause ) ]
example:
SELECT
count(*) AS unfiltered,
count(*) FILTER (WHERE i < 5) AS filtered
FROM generate_series(1,10) AS s(i);
unfiltered | filtered
------------+----------
10 | 4
(1 row)
If you are aggregating, then presumably you have an aggregation function. Then the syntax would look like:
sum(table.grossprofit - table.totalrevenue) FILTER (
WHERE lower(table.ticker) = 'aapl.us') AS company_cogs

custom count measure runs forever MDX

So this question goes off the one here
I've been trying to do a similar count measure and I did the suggested solution but it's still running.... and it's been more than 30 minutes with no results, while without that it runs in under a minute. So am I missing something? Any guidance would help. Here is my query:
WITH
MEMBER [Measures].[IteractionCount] AS
NONEMPTY
(
FILTER
(
([DimInteraction].[InteractionId].[ALL].Children,
[Measures].[Impression Count]),
[DimInteraction].[Interaction State].&[Enabled]
)
).count
SELECT
(
{[Measures].[IteractionCount],
[Measures].[Impression Count]}
)
ON COLUMNS,
(
([DimCampaign].[CampaignId].[CampaignId].MEMBERS,
[DimCampaign].[Campaign Name].[Campaign Name].MEMBERS,
[DimCampaign].[Owner].[Owner].MEMBERS)
,[DimDate].[date].[date].MEMBERS
)
ON ROWS
FROM
(
SELECT
(
{[DimDate].[date].&[2020-05-06T00:00:00] : [DimDate].[date].&[2020-05-27T00:00:00]}
)
ON COLUMNS
FROM [Model]
)
WHERE
(
{[DimCampaign].[EndDate].&[2020-05-27T00:00:00]:NULL},
[DimCampaign].[Campaign State].&[Active],
{[DimInteraction].[End Date].&[2020-05-27T00:00:00]:NULL}//,
//[DimInteraction].[Interaction State].&[Enabled]
)
I don't know if FILTER is affecting it in any way but I tried it with and without and it still runs forever. I do need it specifically filtered to [DimInteraction].[Interaction State].&[Enabled]. I have also tried to instead filter to that option in the WHERE clause but no luck
Any suggestions to optimize this would be greatly appreciated! thanks!
UPDATE:
I end up using this query to load data into a python dataframe. Here is my code for that. I used this script for connecting and loading the data. I had to make some edits to it though to use windows authentication.
ssas_api._load_assemblies() #this uses Windows Authentication
conn = ssas_api.set_conn_string(server='server name',db_name='db name')
df = ssas_api.get_DAX(connection_string=conn, dax_string=query))
The dax_string parameter is what accepts the dax or mdx query to pull from the cube.
Please try this optimization:
WITH
MEMBER [Measures].[IteractionCount] AS
SUM
(
[DimInteraction].[InteractionId].[InteractionId].Members
* [DimInteraction].[Interaction State].&[Enabled],
IIF(
IsEmpty([Measures].[Impression Count]),
Null,
1
)
)
SELECT
(
{[Measures].[IteractionCount],
[Measures].[Impression Count]}
)
ON COLUMNS,
(
([DimCampaign].[CampaignId].[CampaignId].MEMBERS,
[DimCampaign].[Campaign Name].[Campaign Name].MEMBERS,
[DimCampaign].[Owner].[Owner].MEMBERS)
,[DimDate].[date].[date].MEMBERS
)
PROPERTIES MEMBER_CAPTION
ON ROWS
FROM
(
SELECT
(
{[DimDate].[date].&[2020-05-06T00:00:00] : [DimDate].[date].&[2020-05-27T00:00:00]}
)
ON COLUMNS
FROM [Model]
)
WHERE
(
{[DimCampaign].[EndDate].&[2020-05-27T00:00:00]:NULL},
[DimCampaign].[Campaign State].&[Active],
{[DimInteraction].[End Date].&[2020-05-27T00:00:00]:NULL}//,
//[DimInteraction].[Interaction State].&[Enabled]
)
CELL PROPERTIES VALUE
If that doesn’t perform well the please describe the number of rows returned by your query when you comment out IteractionCount (sic) from the columns axis. And please describe how many unique InteractionId values you have.

How do you query an array in Standard SQL that meets a certain conditional?

I am trying to pull records whose arrays only meet a certain condition.
For example, I want only the results that contain "IAB3".
Here is what the table looks like
Table Name:
bids
Column Names:
BidderBanner / WinCat
Entries:
1600402 / null
1911048 / null
1893069 / [IAB3-11, IAB3]
1214894 / IAB3
How I initially thought it would be
SELECT * FROM bids WHERE WinCat = "IAB3"
but I get an error that says no match for operator types array, string.
The database is in Google Big Query.
Below is for BigQuery Standard SQL
#standardSQL
SELECT * FROM `project.dataset.bids` WHERE 'IAB3' IN UNNEST(WinCat)
You can test, play with above using sample data from your question as in example below
#standardSQL
WITH `project.dataset.bids` AS (
SELECT 1600402 BidderBanner, NULL WinCat UNION ALL
SELECT 1911048, NULL UNION ALL
SELECT 1893069, ['IAB3-11', 'IAB3'] UNION ALL
SELECT 1214894, ['IAB3']
)
SELECT * FROM `project.dataset.bids` WHERE 'IAB3' IN UNNEST(WinCat)
with result
you need to use single quotes in sql for all strings. it should be WHERE WinCat = 'IAB3' not WHERE WinCat = "IAB3"
One method uses unnest(), something like this:
SELECT b.*
FROM bids b
WHERE 'IAB3' IN (SELECT unnest(b.WinCats))
However, array syntax varies among the databases that support them and they are no part of "standard SQL".
this will work:
SELECT * FROM bids WHERE REGEXP_LIKE (WinCat, '(.)*(IAB3)+()*');

Results of MDX query against missing partition - Tabular 2012

UPDATE: the resolution was to do a process full at the database level instead of at the partition level.
I am trying to understand the behavior I am observing with a query a Tabular model. I partition by quarter, so when I built a new "current quarter" partition at the start of this quarter, the partition that used to contain 2016-Q4 was overwritten with 2017-Q1. Then when I ran my MDX query against the Tabular model filtering on the date dimension for 2016-Q4, instead of returning nothing like I would have expected, it returned data from the oldest partition 2014-Q1. It is like instead of returning no data, it decides to return the "first record" for that dimension (in this case all dates where 07/01/2014). Every other dimension I was filtering on still performed as expected.
Does anybody have any ideas as to why it behaves this way? FYI, I have tried restructuring my MDX statement a couple different ways:
SELECT
NON EMPTY { [Measures].[Measure1]} ON COLUMNS,
NON EMPTY { ([Dimension].[Dimension1])} DIMENSION PROPERTIES member_caption, member_unique_name ON ROWS
FROM [Model] WHERE ([Dimension].[Dimension2].&[Value], [DateDimension].[DateDimension].&[Value1] : [DateDimension].[DateDimension].&[Value2])
AND
SELECT
NON EMPTY { [Measures].[Measure1]} ON COLUMNS,
NON EMPTY { ( except([Dimension].[Dimension1].members,[Dimension].[Dimension1].[all]))} DIMENSION PROPERTIES member_caption, member_unique_name ON ROWS
FROM ( SELECT ( [Dimension].[Dimension1].&[Value] ) ON COLUMNS
FROM ( SELECT ( [Dimension].[Dimension2].&[Value] ) ON COLUMNS
FROM ( SELECT ( [DateDimension].[DateDimension].&[Value1] : [DateDimension].[DateDimension].&[Value2] ) ON COLUMNS
FROM [Model])))
Edit: added example of actual MDX
SELECT
NON EMPTY { [Measures].[ConvertedNetRevenue],
[Measures].[LoadConvertedNetRevenue],
[Measures].[OrderConvertedNetRevenue],
[Measures].[TotalOrderBrokerageCount],
[Measures].[TotalLoadBrokerageCount]
} ON COLUMNS,
NON EMPTY {
(
except([BrokerageQuery].[KeyBranch].members,[BrokerageQuery].[KeyBranch].[all]),
except([BrokerageQuery].[LoadNumber].members,[BrokerageQuery].[LoadNumber].[all]),
except([BrokerageQuery].[CustomerOrderNumber].members,[BrokerageQuery].[CustomerOrderNumber].[all]),
except([BrokerageQuery].[BranchName].members,[BrokerageQuery].[BranchName].[all]),
except([BrokerageQuery].[BranchCode].members,[BrokerageQuery].[BranchCode].[all]),
except([BrokerageQuery].[BranchRoleDescription].members,[BrokerageQuery].[BranchRoleDescription].[all]),
except([BrokerageQuery].[ModeDescription].members,[BrokerageQuery].[ModeDescription].[all]),
except([BrokerageQuery].[ServiceTypeDescription].members,[BrokerageQuery].[ServiceTypeDescription].[all]),
except([BrokerageQuery].[SystemDisplayName].members,[BrokerageQuery].[SystemDisplayName].[all]),
except([BrokerageQuery].[IsPartialFinLock].members,[BrokerageQuery].[IsPartialFinLock].[all]),
except([BrokerageQuery].[KeyDate_Financial].members,[BrokerageQuery].[KeyDate_Financial].[all])
)
} DIMENSION PROPERTIES member_caption, member_unique_name ON ROWS
FROM ( SELECT
(
{
[dimCurrency].[CurrencyCode].&[USD]
}
) ON COLUMNS
FROM ( SELECT
(
{
{
[BrokerageQuery].[KeyBranch].[10279]
}
}
) ON COLUMNS
FROM ( SELECT
(
[BrokerageQuery].[KeyDate_Financial].&[20161106] :
[BrokerageQuery].[KeyDate_Financial].&[20161106]
) ON COLUMNS
FROM [Brokerage])))
Instead of processing our partitions individually (at the beginning of each quarter when re-partitioning occurs), we need to do a database-level process full.

Filter dimensions for a specific member in MDX

I need to filter the dimension [Line] just for a specific [Year].Member.
For example,
[Time].[2004] to show results ([Product].[Line].[Classic Cars] and [Product].[Line].[Ships]) - excude the rest of [Product].[Line] members for [Time].[2004] but do not exclude [Product].[Line] members for the other [Time].Members.
I need a code compatible with Mondrian.
Any suggestion?
SELECT
NON EMPTY {[Measures].[Sales]} ON COLUMNS,
NON EMPTY NonEmptyCrossJoin([Time].[Years].Members, [Product].[Line].Members) ON ROWS
FROM
[SteelWheelsSales]
Something like this should work:
SELECT
NON EMPTY
{[Measures].[Sales]} ON COLUMNS
,NON EMPTY
{
(
[Time].[2004]
,{
[Product].[Line].[Classic Cars]
,[Product].[Line].[Ships]
}
)
,NonEmptyCrossJoin
(
Except
(
[Time].[Years].MEMBERS
,[Time].[2004]
)
,[Product].[Line].MEMBERS
)
} ON ROWS
FROM [SteelWheelsSales];