If I have two Dimensions: Origin and Destination how can I count the number of times Origin = Destination?
SELECT [Location] ON COLUMNS
FROM [DELIVERIES]
WHERE ( [Origin] = [Destination] )
[Origin] = [Destination] obviously isnt the answer...
The following code produces the correct result:
SELECT
SUM(Transfers) AS Same_Skill_Transfers
FROM (
SELECT
CONVERT(varchar(30),"[From VQ].[LOB].[LOB].[MEMBER_CAPTION]") As From_VQ,
CONVERT(varchar(30),"[To VQ].[LOB].[LOB].[MEMBER_CAPTION]") As To_VQ,
CONVERT(integer,"[Measures].[Transfers]") As Transfers
FROM
OPENQUERY(TRANSFERS,'
SELECT
{[Measures].[Transfers]} ON COLUMNS,
Filter(NonEmptyCrossjoin( [From VQ].[LOB].Members, [To VQ].[LOB].Members),
[From VQ].[LOB].[LOB].[Life Cycle].Properties(''Caption'') = [To VQ].[LOB].[LOB].[Life Cycle].Properties(''Caption'')
) ON 1
FROM
[Transfers]
WHERE (
[Date].[Date Hierarchy].[Month].[July 2014],
[From VQ].[Hierarchy].[AOB].[Consumer],
[From Agent].[Employee Id].&[612117]
)
')
) A
WHERE
To_VQ = From_VQ
Lets say I have a [FROM_VQ].[LOB] = "BOTH" and I want to include in the SUM when [FROM_VQ].[LOB] = "Both" and [TO_VQ].[LOB] is either A or B?
Your question is missing some info, but I'll take a stab and then update once you provide more info. I'm assuming that there are two dimensions, origin and destination, and that each of these dimensions have a location attribute.
In general, you can use a filter statement for this. If your location attribute has the same key in each dimension, you can do this:
select [Origin].[Location].children on 0,
Filter(NonEmptyCrossjoin( [Origin].[Location].Children, [Destination].[Location].Children),
[Origin].[Location].Properties('Key') = [Destination].[Location].Properties('Key')
) on 1 from [DELIVERIES]
If the keys aren't the same, but the label you see for the field is you can switch Key for Caption.
Here's an example where someone did this with dates.
Related
Can you please help me to get the item with the highest count using DAX?
Measure = FIRSTNONBLANK('Table1'[ItemName],CALCULATE(COUNT('Table2'[Instance])))
This shows the First ItemName in the table but doesnt get the ItemName of the Highest Value.
Thanks
Well, it's more complicated than I would have wanted, but here's what I came up with.
There things that you are hoping to do that are not so straightforward in DAX. First, you want an aggregated aggregation ;) -- in this case, the Max of a Count. The second thing is that you want to use a value from one column that you identify by what's in another column. That's row-based thinking and DAX prefers column-based thinking.
So, to do the aggregate of aggregates, we just have to slog through it. SUMMARIZE gives us counts of items. Max and Rank functions could help us find the biggest count, but wouldn't be so useful for getting Item Name. TOP N gives us the whole row where our count is the biggest.
But now we need to get our ItemName out of the row, so SELECTCOLUMNS lets us pick the field to work with. Finally, we really want a value not a 1-column, 1-row table. So FirstNonBlank finishes the job.
Hope it helps.
Here's my DAX
MostFrequentItem =
VAR SummaryTable = SUMMARIZE ( 'Table', 'Table'[ItemName], "CountsByItem", COUNT ( 'Table'[ItemName] ) )
VAR TopSummaryItemRow = TOPN(1, SummaryTable, [CountsByItem], DESC)
VAR TopItem = SELECTCOLUMNS (TopSummaryItemRow, "TopItemName", [ItemName])
RETURN FIRSTNONBLANK (TopItem, [TopItemName])
Here's the DAX without using variables (not tested, sorry. Should be close):
MostFrequentItem_2 =
FIRSTNONBLANK (
SELECTCOLUMNS (
TOPN (
1,
SUMMARIZE ( 'Table', 'Table'[ItemName], "Count", COUNT ( 'Table'[ItemName] ) ),
[Count], DESC
),
"ItemName", [ItemName]
),
[ItemName]
)
Here's the mock data:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcipNSspJTS/NVYrVIZ/nnFmUnJOKznRJzSlJxMlyzi9PSs3JAbODElMyizNQmLEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Stuff = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Stuff", type text}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Stuff", "ItemName"}})
in
#"Renamed Columns"
EVALUATE
FILTER
(
SUMMARIZE (
NATURALLEFTOUTERJOIN (
'Target_Category',
'Target_Form'
),
'Target'[Area],
'Target'[id],
'Target'[Target date],
'Target'[Target Time Range],
'Target_Category'[Origin],
'Target_Category'[Sectotion],
'Target'[Location],
'Target_Category'[Ethencity],
'Target_FormResponse'[Area Used],
'Target'[Description]
),
'Target'[id] = Value("111373268")
)
ORDEr BY 'Target'[Target Key]
I have the sample DAX query above. Is there away i can manipulate 'Target_FormResponse'[Area Used] such that if it is blank or empty, i return "No" otherwise if its not blank or empty i return "Yes".
In SSRS, i can do something like =IIF(Len(Fields!Form_Response.Value) > 0,"Yes","No") but i want to achieve this at the DAX query level.
If you are satisfied with adding an extra column that contains the "Yes" or "No" values, simple wrap the entire expression in a call to ADDCOLUMNS:
EVALUATE
ADDCOLUMNS (
FILTER (
SUMMARIZE (
NATURALLEFTOUTERJOIN ( 'Target_Category', 'Target_Form' ),
'Target'[Area],
'Target'[id],
'Target'[Target date],
'Target'[Target Time Range],
'Target_Category'[Origin],
'Target_Category'[Sectotion],
'Target'[Location],
'Target_Category'[Ethencity],
'Target_FormResponse'[Area Used],
'Target'[Description]
),
'Target'[id] = VALUE ( "111373268" )
),
"Area Used Yes/No", IF ( 'Target_FormResponse'[Area Used] > 0, "Yes", "No" )
)
ORDER BY 'Target'[Target Key]
If you want to get rid of the original column in the output, you'd have to use SELECTCOLUMNS instead, but unfortunately, you'd then have to specify the names of each of the columns you want to keep, so the code ends up a lot longer.
I have a dimension (page_type) where the names are not unique (so two keys can have the same name). Now I would like to see the clicks by page_type-name.
The following query unfortunately show the dimension names, but one line per key.
SELECT
{[Measures].[count_clicks]} ON COLUMNS,
[page_type].[page_type].members ON ROWS
FROM
[customer_journey]
The result:
category 150.000
product 100.000
category 80.000
...
How can I change this query, to get only one line per page_type?
category 230.000
product 100.000
...
This is slow but does this work:
with set SetOfPagesWithSameName as
filter
(
[page_type].[page_type].members as p,
p.current.name = [page_type].[page_type].currentmember.name
)
member Measures.TotalCountOFClicks as
sum(
existing SetOfPagesWithSameName,
[Measures].[count_clicks]
)
member Measures.CountSimilarPagesGrt1 as
IIF(SetOfPagesWithSameName.count > 0 , 1, null)
select
NonEmpty([page_type].[page_type].members, Measures.CountSimilarPagesGrt1) on 1,
Measures.TotalCountOFClicks on 0
from [customer_journey]
I would like to filter a dimension for cube security with some information that are in another dimension.
So - I have a dimension which holds some account Responsible (Account Number and the initials on the one responsible) and another Dimension with all accounts.
I would like to make sure, that a person only can see movements on the accounts on which they are responsible.
I can make the filtering work like this:
SELECT
{} ON 0
,{
Exists
(
Filter
(
[Accounts].[Accounts].[AccountNo]
*
[AccountResponsible].[AccountResponsible].[AccountNo]
,
[Accounts].[Accounts].Properties("key")
=
[AccountResponsible].[AccountResponsible].Properties("key")
)
,[AccountResponsible].[Responsible].&[MSA]
)
} ON 1
FROM mycube;
the problem is, that there are two columns, and I can't use that in cube security. Is there a way to rewrite this, so that I actually get only one column with the members that the user are allowed to see?
Try using the Extract function:
SELECT
{} ON 0
,
EXTRACT(
{
Exists
(
Filter
(
[Accounts].[Accounts].[AccountNo]
*
[AccountResponsible].[AccountResponsible].[AccountNo]
,
[Accounts].[Accounts].Properties("key")
=
[AccountResponsible].[AccountResponsible].Properties("key")
)
,[AccountResponsible].[Responsible].&[MSA]
)
}
,[Accounts].[Accounts] //<<HIERARCHY YOU WISH TO EXTRACT
) ON 1
FROM mycube;
I am facing some problem to calculate values from comparing dimension's value. I have 3 dimensions (Datatype, Customer, Product) and one measure (GrossSales).
What would be the MDX query if I want GrossSales for ProductID = 1,2,3 and Dataype = 4,5,6?
Here Datatype has relationship with GrossSales, Customer has relationship with GrossSales and Product has relationship with Customer.
I am trying this but doesn't work
CREATE MEMBER CURRENTCUBE.[Measures].Forecast_Gross_Sales AS
(
SELECT NON Empty [Measures].[Gross Sale]
FROM [Measures]
WHERE (
[Data Type].[ID].[ID] = 4
AND [Chain].[Customer ID] = [Measures].[Customer ID]
)
), VISIBLE = 1
, DISPLAY_FOLDER = 'Forecast'
, ASSOCIATED_MEASURE_GROUP = 'Data Types';
It looks like you are just getting started with MDX. There are some fundamental concepts that will help you get what you need. This comparison of SQL and MDX might be helpful. MDX uses the where clause as a slicer (to select certain dimension members) rather than a filter. You can't put member = somevalue in the where clause. And you can't really use the where clause to define a relationship to some other table.
Instead, your where clause would be something more like
[Data Type].[ID].[ID].&[4]
Since I can't see your data model, I can't be sure, but I would guess that [Chain].[Customer ID] = [Measures].[Customer ID] is something that you want to define the the dimension usage of your cube rather than in the query.
Edit: Now that the question has been edited, it looks like you are creating a calculated member. in this case there is no select or where clause. It will look more like this:
CREATE MEMBER CURRENTCUBE.[Measures].Forecast_Gross_Sales AS
Aggregate([Data Type].[ID].[ID].&[4], [Measures].[Gross Sale])
, VISIBLE = 1
, DISPLAY_FOLDER = 'Forecast'
, ASSOCIATED_MEASURE_GROUP = 'Data Types';
The relationship from the measure group through the Customer dimension to the Chain dimension is something that should be defined in the dimension usage. This is called a Reference dimension relationship.