Converting simple DAX expression to MDX - dax

I need to convert a DAX expression I'm using i PowerBI to MDX to use in Excel > Olap > Calculated Measure (or member?). I'm no developer and do not have full access to the database, but connect to it using analysis services.
This DAX measure is working. I want to the same logic in Excel as a calculated measure (or calculated member?). Running measure displays sales quantity where the products has a 1 in either FilterA, FilterB or FilterC.
CALCULATE(
[SalesQtn],
FILTER(
'Product',
'Product'[FilterA] = "1" || 'Product'[FilterB] = "1" || 'Product'[FilterC] = "1"
)
)
I'm 98% newbie in In DAX and 100% newbie in MDX. I've managed to do like below in Calculated Measure to filter using FilterA = "1", but I recon a function is needed for combination FilterB and C using OR operation
([Measures.[SalesQtn],
[Product].[FilterA].&[1])

Try the following:
SUM(
{
([Product].[FilterA].&[1], [Product].[FilterB].[All], [Product].[FilterC].[All]),
([Product].[FilterA].[All], [Product].[FilterB].&[1], [Product].[FilterC].[All]),
([Product].[FilterA].[All], [Product].[FilterB].[All], [Product].[FilterC].&[1])
},
[Measures].[SalesQtn]
)
Or if there are some rows where two FilterA and FilterB are 1 that might produce a wrong result. In that case try:
Sum(
Filter(
[Product].[FilterA].[FilterA].Members
* [Product].[FilterB].[FilterB].Members
* [Product].[FilterC].[FilterC].Members,
[Product].[FilterA].CurrentMember is [Product].[FilterA].&[1]
Or [Product].[FilterB].CurrentMember is [Product].[FilterB].&[2]
Or [Product].[FilterC].CurrentMember is [Product].[FilterC].&[1]
),
[Measures].[SalesQtn]
)

Related

DAX: Unwanted cartesian product lines?

Please help to calculate/understand properly lastDate and rankDate measures for following simplified example (download):
Desired result:
Reality (incorrect subtypes):
Why relationship is broken?
How to avoid this cartesian product lines?
My Measure (I commented workaround, because it's kind of postfilter, not prefilter):
rnkDate =
VAR t =
CALCULATETABLE(
VALUES(tstTable[Date]),
REMOVEFILTERS(tstTable[Date])
)
RETURN
//IF( MAX(tstTable[Amount])<>BLANK(), // WORKAROUND To hide unwantedd rows
RANKX(
t,
LASTDATE(tstTable[Date])
)
//)
P.S. Mess happens only if I use fields from dimensional table dimType[Type] (within one table everything is Ok):
The problem is that the query generated by Power BI performs the cartesian product and filers the result by checking the result of the measure.
in our case is something similar to
SUMMARIZECOLUMNS(
'dimType'[Type],
'tstTable'[subType],
'tstTable'[Date],
"MinAmount", CALCULATE(MIN('tstTable'[Amount])),
"lastDate", 'tstTable'[lastDate],
"rnkDate", 'tstTable'[rnkDate]
)
SUMMARIZECOLUMNS doesn't use relationships when iterating on different tables, it applies them when evaluating the measures. There is an article explaining what is the equivalent DAX code executed by SUMMARIZECOLUMNS
Introducing SUMMARIZECOLUMNS
the problem is that RANKX evaluated on an empty table retuns 1. This can be seen executing this on dax.do
EVALUATE
VAR t =
FILTER ( ALL ( 'Date'[Date] ), FALSE )
RETURN
{ RANKX ( t, [Sales Amount] ), CALCULATE ( [Sales Amount], t ) }
so the solution is to first check that the table t is not empty, which is the reason because the workaround that you implemented solved the issue
lastDate =
IF( NOT ISEMPTY(tstTable), // checks fact table in particular context
CALCULATE(
LASTDATE(tstTable[Date]),
REMOVEFILTERS(tstTable[Date])
)
)
rnkDate =
VAR t =
CALCULATETABLE(
VALUES(tstTable[Date]),
REMOVEFILTERS(tstTable[Date])
)
RETURN
IF( NOT ISEMPTY(tstTable),
RANKX(
t,
LASTDATE(tstTable[Date])
)
)

Power BI: COUNTA across multiple columns with multiple filter criteria

I am fairly new to Power BI and DAX and I am struggling with getting the below measure to work (I have provided to different version of what I have tried). I am trying to COUNTA the # of items that met certain criteria in two separate columns. Each column that the measure looks at has at least 3 filter criteria and in column two I need to use wildcards as the data there in has entries that share the first two values but the 3rd value could change over time. Any assistance would be amazing as I have tried dozens of different ways and I still cannot figure it out.
GA = CALCULATE(COUNTA(Table,[Item]),filter(Table,Table[Column 1]in{"GA","SP"}) , Filter(Table,Table[Column2]in{"MT*","GA*","SP*","OP*"}))
GA = CALCULATE(COUNTA(Table[Item]),Table[Column1]= "GA" || Table[Column1]= "SP" && Table[Column2]="GA*" || Table[Column2]= "OP*" || Table[Column2]]= "SP*" || Table[Column2]= "MT*")
Unfortunately I cannot provide an example of the data.
Thank you in advance
As #Alexis Olson mentions in his reply to this post, there does not appear to be a shorthand way to use the IN operator with wildcards.
Hence:
GA =
CALCULATE(
COUNTROWS( 'Table' ),
FILTER(
'Table',
'Table'[Column1]
IN { "GA", "SP" }
&& (
CONTAINSSTRING( 'Table'[Column2], "MT" )
|| CONTAINSSTRING( 'Table'[Column2], "GA" )
|| CONTAINSSTRING( 'Table'[Column2], "SP" )
|| CONTAINSSTRING( 'Table'[Column2], "OP" )
)
)
)

Calculated column that ignores row context

I'm trying to calculate the Total Price per Order number. It specifically needs to be a column, because I'll be needing it for further calculations.
Can someone help me write code that calculates the total per Order Number, instead of line amount as it does now?
Since it's a calcualted column, just avoiding any context transition gives a straightforward solution
Total Price Per Order =
VAR CurrentOrder = SalesDetail[Order Number]
RETURN
SUMX (
FILTER (
SalesDetail,
SalesDetail[Order Number] = CurrentOrder
),
SalesDetail[Unit Price] * SalesDetail[Quantity]
)

Multiple data types in a Power BI matrix

I've got about 20 different metrics across 10 locations and want to make a matrix with metrics as rows and the locations as the different columns. The issue I'm running into is that the metrics are different data types, some are whole numbers, some are %s and some are $s.
Is there any way to custom format each row as a different data type like there is in excel?
Edit: Sorry I wasn't clear. I don't want the same value showing up multiple times.
See below screenshots.
Test Data Screenshot:
What I want, but I want it in Power BI, not Excel:
What I don't want when I use measures that are formatted as different data types:
The formatting is not controlled by the rows or columns but rather each measure can be assigned its own data type using the Modeling tab.
Edit: I see a few options here.
Option 1: Write a text measure that switches formats like this:
FormatMetric =
VAR Val = SUM ( TestData[Value] )
RETURN
SWITCH (
SELECTEDVALUE ( TestData[Metric] ),
"# quantity", FORMAT ( Val, "0" ),
"$ Sales", FORMAT ( Val, "$0.00" ),
"% to plan", FORMAT ( Val, "0%" )
)
You'll get a table that looks like this:
Be aware that this measure returns text values and won't work in a chart.
Option 2: Create three separate measures and format each separately:
# quantity = CALCULATE ( SUM ( TestData[Value] ), TestData[Metric] = "# quantity" )
$ Sales = CALCULATE ( SUM ( TestData[Value] ), TestData[Metric] = "$ Sales" )
% to plan = CALCULATE ( SUM ( TestData[Value] ), TestData[Metric] = "% to plan" )
If you make sure you have Format > Values > Show on rows turned on and put these three measures in the Values box:
These measures can be used in charts.
Option 3: Pivot your data table on the Metric column in the query editor so you don't have mixed data types in a single column. Your data table should look like this now:
From here, you can write three simple measures format as in the previous option:
# quantity = SUM ( Pivot[# quantity] )
$ Sales = SUM ( Pivot[$ Sales] )
% to plan = SUM ( Pivot[% to plan] )

How do I create a DAX expression to display a calculation from a derived table?

I'm using DaxStudio to test some measures, but am having trouble getting them to work. I can run the following expression, but don't know how to run an average of the field Mean to show just the mean of that. I'm basically expecting output to be a single cell with the average.
DAX Query:
EVALUATE
FILTER(
NATURALINNERJOIN(Alldata, NATURALINNERJOIN('Label', NATURALINNERJOIN('LabelBSkill', 'LabelCSkill'))),
'LabelCSkill'[Name] = "Critical"
&& 'Label'[Type]="Red"
)
Mean is in the table Alldata if that matters
Give this a try:
EVALUATE
ROW (
"Mean", CALCULATE (
AVERAGE ( Alldata[Mean] ),
'LabelCSkill'[Name] = "Critical",
'Label'[Type] = "Red"
)
)

Resources