DAX HASONEVALUE row filter - dax

12 Total :=
SWITCH (
TRUE (),
HASONEVALUE ([level04] ), CALCULATE (
[Local],
DATESINPERIOD (
Calendar[Date],
MAX ( Calendar[Date] ),
-12,
MONTH
)
),
HASONEVALUE ([level03]),If([level 03]= "calculation:" && [level03]= "Cash:" && [level03]= "Costs:"=>0, Blank(), CALCULATE (
[Local],
DATESINPERIOD (
Calendar[Date],
MAX ( Calendar[Date] ),
-12,
MONTH
)
)
),
HASONEVALUE ( [level02] ), BLANK ()
)
I would like to add condition that if lever 03 = cash, calculation, and cost then return blank to remove sub total rows. I tried something like that and its not working properly it gives me error "value for column level03 cannot be determined in the current context". how can i add that condition in level03 column?

Just a side note, "its not working properly" is tremendously unhelpful in determining the source of problems. Specifically in a Power Pivot/Tabular model, this link provides a list of ways to help answerers help you.
Syntactically, there are some errors. Let's address those and see if the behavior is appropriate before diving into alternate measure definitions.
This section is the problem:
...
,HASONEVALUE([level03])
// Note, you are inconsistent in referring to [level03] and
// [level 03]
,If( // Below you are testing column references against scalar
// literals. DAX doesn't understand how to do this, so you
// have to wrap each reference in a VALUES() function, which
// can be implicitly converted to a scalar value
[level 03] = 'calculation:' // DAX needs double quotes: ""
&& [level03]= 'Cash:' // You have multiple predicates
&& [level03]=' Costs:'=>0 // for a single field combined
// with a logical and - these
// can't all simultaneously be true
,Blank()
,CALCULATE(
[Local]
,DATESINPERIOD(
Calendar[Date],
MAX ( Calendar[Date] ),
-12,
MONTH
)
)
)
,....
I am just going to change the combination of predicates to a logical or in my rewrite, so that it's possible for this IF() to return a BLANK. The way it's written above, even fixing the syntax errors will cause it to always evaluate the else condition, because [level 03] cannot simultaneously have the value of "calculation:", "Cash:", and " Costs:=>0".
...
,HASONEVALUE('<table>'[level03])
// As a best practice, you should always use fully qualified
// column references - 'table'[field]
// Also, I've referred only to [level03] below
,IF( // Note we've wrapped [level 03] in VALUES() below - this will
// allow our comparisons to scalar literals
VALUES('<table>'[level03]) = "calculation:" // double quotes
|| VALUES('<table>'[level03]) = "Cash:"
// Note: we've changed the logical and, &&, to a logical
// or, || - meaning if any 1 of these predicates is true,
// we'll return BLANK
|| VALUES('<table>'[level03]) = " Costs:=>0"
,BLANK()
,CALCULATE(
[Local]
,DATESINPERIOD(
Calendar[Date]
,MAX( Calendar[Date] )
,-12
,MONTH
)
)
)
,....
What this will do is check for any cell on the pivot table where [level03] has exactly one distinct value - when this is true, it will evaluate the IF() function.
The IF() function will check the value of [level03]. If that value is any one of the three following values, "calculation:", "Cash:", or " Costs:=>0", it will return BLANK. If the value of [level03] is not any of those three, it will evaluate the CALCULATE(), which returns the measure [Local] for a rolling 12 month period.

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])
)
)

DAX hasonevalue but also total

I have at table where I show a calcutated measure, but in the visulization I only want to show the measures if a specific dimension is in the filter.
This works great with HASONEVALUE:
Dividedmeasure:= IF(HASONEVALUE('Employment'[EmploymentGroup]), DIVIDE([measure1)], [Measure2],(BLANK() )))
The problem is HASONEVALUE also blanks the total which Is not what I want. I want to blank everything where EmploymentGroup is not in the filters since the report doens't make sense without Employmentgroup. Any suggestions?
enter image description here
Try this: I think You should remove BLANK(). Then It won't show anything where [EmploymentGroup] is not inside the filter.
1st Method: HASONEFILTER()
Dividedmeasure =
IF (
HASONEFILTER ( 'Employment'[EmploymentGroup] ),
DIVIDE ( [measure1], [Measure2] ),
)
2nd Method: ISFILTERED()
Dividedmeasure =
IF (
ISFILTERED ( 'Employment'[EmploymentGroup] ),
DIVIDE ( [measure1], [Measure2] )
)

DAX DIVIDE and / operator not returning the same result

We have a report connected, via a live connection, to an analysis services tabular model.
We have this measure:
MeasureX=
VAR NumLogins =
CALCULATE (
[Measure Dynamic Agg],
'Time Periods'[PeriodCalc] = "Current MtD",
'Calendar'[Is Current Month] = "TRUE",
'Measures - Financial Agg'[Formula] = "Num Logins",
ALL ( 'Measures - Financial'[Formula] )
)
VAR NumCategories =
CALCULATE (
[Measure Dynamic],
'Time Periods'[PeriodCalc] = "Current MtD",
'Calendar'[Is Current Month] = "TRUE",
'Measures - Financial'[Formula] = "Num Categories",
ALL ( 'Measures - Financial Agg'[Formula] )
)
RETURN
DIVIDE(NumLogins ,NumCategories)
In the dashboard this returns blanks everywhere.
If we change the last line of DAX to the following it returns numbers as expected:
...
...
RETURN
NumLogins / NumCategories
Does anyone have any clue what is happening? I was under the impression that semantically using the / operator and DIVIDE function were the same, apart from handling divide by zero differently?
DIVIDE handles both zeroes and blanks in the denominator. It's the blanks that are getting you here. Check out https://learn.microsoft.com/en-us/dax/best-practices/dax-divide-function-operator.

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 to search in lookupvalue filter value?

I want to use lookup value in Power BI, I'm using the following expression:
Type_to_Freq = LOOKUPVALUE(PP_Freq[Estimate],PP_Freq[Type],"Private")
the problem is, I need to pull different values from the table depending the result of one filter in my page.
Can the third field of lookup value could be dynamic?
If one filter I choose "Private", obtain the result for this, and if I choose "Public", change the result of "Private" to "Public".
What do I need to change to use that expression?
Something like this should do the trick:
Measure :=
VAR Selection =
SELECTEDVALUE ( PP_Freq[Type] )
RETURN
IF (
NOT ( ISBLANK ( Selection ) ),
LOOKUPVALUE ( PP_Freq[Estimate], PP_Freq[Type], Selection ),
"Make A Slection"
)

Resources