DAX measure depending on multiple conditions - powerbi

I am new with Dax. I have a matrix table in Power BI which has been imported from Excel. I need to create a dynamic DAX measure which will give me the values if both conditions are filtered.
This is a very big table and the measure has to be dynamic as values keep changing.
For eg:
Criteria : A, B, C
Code : 101,102,103
Values: 94, 50, 63
If Criteria is A and Code is 101 then value = 94
If Criteria is B and Code is 102 then value = 50

If you need measure based on columns that you put in your matrix on rows or columns, you may use this.
YourMeasure = IF( MAX( Tab[fruit] ) = "Apple" & MAX( Tab[size] ) = "Big", 1, 0 )
Probably what you are after, and what is safer, is adding new calculated column in your source table:
CalculatedColumn =
IF(
Tab[friut] = "Apple" // condition 1
& // AND opperator
Tab[size] = "Big", // condition 2
1, // result if true
0 // result if false
)
Where 1 is returned if both conditions are met.
If your condition path is complex you might use SWITCH function.
SWITCH( TRUE(),
Tab[column1] = "Apple" & Tab[column2]="Big", "Big Fruit",
Tab[column1] = "Carot" & Tab[column2]="Small", "Small Vegetable",
"Other thing"
)

Related

How to put formula based order in Rankx (Power BI)?

Am trying to sort a ranking column in matrix based on IF condition, but the Rankx doesn't accept a formula other than ASC/TRUE/1 0R DESC/FALSE/0 .
The if condition basically provide ouput as either 1 or 0.
Ex:
VAR A= IF(x>y,1,0)
Return Rankx(table, expression,,A,Dense)
Change your measure to:
vyz =
VAR A = IF(TODAY()> TODAY()-1
, Rankx(ALL('Table'), [Measure],, 1, Dense) // TRUE order 1
,Rankx(ALL('Table'), [Measure],, 0, Dense) // FALSE order 0
)
Return
A

Best Practice for Measures in DAX

Just wondering about best practices, and SPEED in asking this:
Does it matter if You Create a measure on a measure that is based on another measure,
or is it better to base that new Measure on the base Measure?
 
eg
BaseMeasure= Sum(x)
Measure1= Calculate(BaseMeasure,y=1 || y=2)
Should Measure2 be
(a) Calculate(Measure1, y=2)
OR
(b) Calculate(BaseMeasure,y=2)
this is not valid DAX, but let's assume it's some pseudo-code
The main problem is that a and b measures would return different results
The (a) measure sets a filter context to y = 2, but then the Measure1 replaces this filter context with y = 1 || y = 2, and then evaluate the BaseMeasure with this filter. The result is precisely the same as if Measure1 was directly invoked.
The (b) measure instead sets the filter context to y = 2 and then invokes the BaseMeasure with this filter.
This can be simulated on dax.do with the following DAX code
DEFINE
TABLE T =
DATATABLE ( "Y", INTEGER, "X", INTEGER, { { 1, 10 }, { 2, 20 } } )
MEASURE T[BaseMeasure] =
SUM ( T[X] )
MEASURE T[Measure1] =
CALCULATE ( [BaseMeasure], T[Y] = 1 || T[Y] = 2 )
MEASURE T[Measure2] =
CALCULATE ( [Measure1], T[Y] = 2 )
MEASURE T[Measure3] =
CALCULATE ( [BaseMeasure], T[Y] = 2 )
EVALUATE
ROW ( "Measure2", [Measure2], "Measure3", [Measure3] )
that returns
here is the link to the dax.do example
To answer the question about nesting measure call, when done correctly it's ok, but focusing mainly on the code architecture and readability.
From the performance point of view, it's hard to predict the performance just by looking ad the code (unless really bad), the right path for optimization is through testing.

DAX: please check if I understood the use of Variabes in DAX correctly

can someone please check if my theoretical understanding of variables is correct?
Suppose that I have a table with 2 columns, Condition1 and Condition2.
I want to count the rows for which Condition1 = 4, and Condition2 = Black.
Soppose then that I write a measure called Black, that creates a Table where all rows have Condition2 = "Black". For Example:
Black:= FILTER (Table, Condition2 = "Black")
And then I write the combined code using variables:
Black4_version1 =
var B = [Black]
return = CALCULATE(
COUNTROWS(B),
Condition1 = 4)
Then this code will not work because DAX thinks that the variable B is a single number (because it's calling a measure and measure is by default seen as a single value?), even though I have created a measure that should have created a table.
But if I create the table within a variable itself, then DAX will know that it's a table and then it will work?
Black4_Version2 =
var B = FILTER (Table, Condition2 = "Black")
return = CALCULATE(
COUNTROWS(B),
Condition1 = 4)
I'm asking this because I want to be 100% sure that I have understood the answer given here: DAX: please explain why this measure with a variable will not work
also because I have been using variables already at work, so I will need to re-check all the dashboards that I have built and talk to my manager about screwing up a big time. So you could say that my job depends on this.
Variables are to be considered constants even when they contain a table. In your Black4_Version2 measure, the CALCULATE() doesn't change the COUNTROWS(B) result, since it's counting the rows of a constant table and no filtering is happening.
Black4_Version2 =
VAR B =
FILTER(
Table,
Condition2 = "Black"
)
RETURN
CALCULATE(
COUNTROWS( B ),
Condition1 = 4
)
but you can iterate over a constant table, therefore FILTER works
Black4_Version3 =
VAR B =
FILTER(
Table,
Condition2 = "Black"
)
RETURN
COUNTROWS(
FILTER(
B,
Condition1 = 4
)
)
P.S. I used the pseudo-DAX sintax used in the answer, since instead of Condition1 = 4 a column reference like Table[Column1] = 4 should be used

Calculate sum of a column in power BI depending on a condition that should be evaluated over each row

I have a table which looks like this:
I need to calculate sum of all numbers in column :ADD , IF the number in column CHANGE is 0 AND column DELETE is 0.
The actual data looks like below. Its same as above. The items with red backets should be filtered out because there are values against delete and change for each unique number in MEMO column.
Create a Measure as below to Exclude records from the list-
exclude =
VAR current_row_tsframe_account = MIN(your_table_name[TSframe_Account])
VAR current_row_tsframe_memo = MIN(your_table_name[TSframe_Memo])
VAR find_delete_change_for_memo =
CALCULATE(
COUNT(your_table_name[TSframe_Memo]),
FILTER(
ALL(your_table_name),
your_table_name[TSframe_Memo] = current_row_tsframe_memo
&& your_table_name[TSframe_Account] = current_row_tsframe_account
&& your_table_name[TSframe_Mode] IN {"CHANGE","DELETE"}
)
)
RETURN
IF(
find_delete_change_for_memo > 0,
"Yes",
"No"
)
The above Measure will return Yes/No per row. You can now Apply visual/page level filter so that records only show where measure Exclude = No. Now this below measure will show your expected value-
total = SUM(your_table_name[TSframe_Memo])

DAX - calculate the % likelihood of a person who has "A" with the chances the person has "B" as well

I have a requirement where I have two columns like this.
ID Disease-Type
1 A
1 D
1 A
2 B
2 B
2 A
2 C
3 D
3 D
And Now I want to select a disease from Disease-Type. Lets say "A".
I want to see in the graph like this,
B 100%
C 100%
D 50%
B is 100% because if that id has disease B then also had Disease "A" (the selected disease). C is 100% because if that id had disease C then also had Disease "A" (the selected disease).
while D is ionly 50%, because out of 2 Unique ID's (1, 3) which had "D" only 1 has Disease Type "A".
Also to note, A shouldn't be seen in the output visual. which will be plotted against Disease-Type.
How can I use DAX to do this. Please explain me with VAR.
You'll want a disconnected table to use as a slicer to select A/B/C/D. You can create a calculated table like this
Disease = VALUES ( Table1[Disease-Type] )
Now you need a measure which reads your slicer selection and computes the desired percentages.
Percent =
VAR SelectedType =
SELECTEDVALUE ( Disease[Disease-Type] )
VAR SelectedIDs =
CALCULATETABLE ( VALUES ( Table1[ID] ), Table1[Disease-Type] = SelectedType )
VAR CurrentIDs =
VALUES ( Table1[ID] )
RETURN
DIVIDE (
COUNTROWS ( INTERSECT ( SelectedIDs, CurrentIDs ) ),
COUNTROWS ( CurrentIDs )
)
The first variable SelectedType reads in your slicer selection and the second variable SelectedIDs is a list of ID values associated with that type (for selection A this is the set {1, 2}). The third variable CurrentIDs is a list of ID values in the current evaluation context (e.g. on the row/column of a matrix visual). For D, this set is {1, 3}.
Finally, you take the count of the intersection divided by the count of CurrentIDs to get your percentage. For the row/column with D, this would be
|{1}| / |{1, 3}| = 1 / 2 = 50%