Multiply by regrouping in dax? - powerbi

I have a dataset grouped by two rows as below
GroupName Score Multiply
Group1 2
Group2 3
Group2 5
Group1 1
I have a slicer based on the parameter table for storing the above GroupName values. so when I select Group1( I am using a selected function on variable in my dax) I want to multiply all rows for Group1(scores) by 4 and all rows for Group2(score) by 6.
I tried this but it is updating all rows.
var a=4*score
var b=6*score
var mm=selectedvalue('Group Paramter'[Group Name])
Return if( mm ="Group1", a, b)
But it multiplies all rows, how can I multiply by grouping using GroupName?
How can I achieve this? I apperciate for any help.

You can try some logic as below (measure)-
var mm = selectedvalue('Group Paramter'[Group Name])
Return if( mm ="Group1", min(table_name[score]) * 4, min(table_name[score]) * 6)

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

When a unique column is on the table, you only need to apply a filter on that column to make the transition happen

Link: https://learn.microsoft.com/en-gb/learn/modules/dax-power-bi-modify-filter/5-context-transition
When a unique column is on the table, you only need to apply a filter
on that column to make the transition happen. In this case, Power BI
applies a filter on the CustomerKey column for the value in row
context.
I am looking for an example to understand this.
Consider the following tables with and without a unique ID column.
Table1
ID
ABC
XYZ
Value
1
a
x
5
2
a
x
2
3
a
y
2
4
b
y
2
5
b
z
3
Table2
ABC
XYZ
Value
a
x
5
a
x
2
a
y
2
b
y
2
b
z
3
Let's define measures
SumValue1 = SUM ( Table1[Value] )
SumValue2 = SUM ( Table2[Value] )
If you calculate each respective measure within the row context (e.g. using a measure in a calculated column) of the first row on each table, then the context transition will turn the row context into a filter context that looks like this for the first table
CALCULATE (
[SumValue1],
FILTER ( Table1, Table1[ID] = 1 )
)
but like this for the second table
CALCULATE (
[SumValue2],
FILTER (
Table2,
Table2[ABC] = "a"
&& Table2[XYZ] = "x"
&& Table2[Value] = 5
)
)
Because there is no unique column in the second case, it's necessary to look at all of the columns to know which row we're referring to.
When there is a unique ID column, you can shortcut this since there is a one-to-one correspondence between rows and ID values, so that specifying the ID is equivalent to specifying the row and vice versa.

Equal bins in DAX equivalent of NTILE function

I would like to imitate NTILE function of SQL in DAX. For a given number of bins, I would like a measure which returns the bin number for any value in a column. The bins should contain more or less equal number of observations.
So the parameters are:
number of bins
test value
table column
Here is something similar in Excel:
= MAX( ROUNDUP( PERCENTRANK($A$1:$A$8, A1) *4, 0),1)
In DAX, you can use the PERCENTILE.INC as the base for such a calculation.
Bucket =
VAR N = 4
VAR Percentiles =
ADDCOLUMNS (
GENERATESERIES ( 1, N ),
"Percentile", PERCENTILE.INC ( Table1[Col1], [Value] / N )
)
RETURN
MINX ( FILTER ( Percentiles, Table1[Col1] <= [Percentile] ), [Value] )
For your data, the Percentiles table variable looks like this:
Value Percentile
1 24.8
2 66.5
3 81.8
4 85.0
Then for each row in your original table, you take the minimum value from the calculated table where that Percentile column is less than or equal to the original table column Col1 in that row.
Note that the above is for a calculated column. For a measure, you'd need to specify an aggregation for Table1[Col1] in the last line (e.g. MAX(Table1[Col1])).

Calculated Column with Current Row Values and Previous Dates in Power BI (DAX)

I'm trying to get a calculated column (not a measure) that gets the sum of a column based on the values in the current row of that table for dates that are 1 month lagged to the date on the current row. My table has dates that are the 1st day of every month only .. no other days in the month. I'm asking the question about DAX; however, I have no problem implementing in M Language in Power Query (actually would probably prefer) if there is a solution that way as well.
I have been able to get a measure to work using something like this..
CALCULATE(SUM(AMT), DATEADD(DATECOLUMN, -1, MONTH))
But I'd like to be a new column instead.
Assuming the table looks something like this..
A B C D AMT
6 BAC456 5/1/2019 TEST 25
2 EPS123 4/1/2019 TEST 45
2 EPS123 3/1/2019 TEST 65
6 BAC456 4/1/2019 TEST 43
6 BAC456 4/1/2019 TEST 88
7 GRE123 4/1/2019 TEST 90
9 BAC456 4/1/2019 TEST 43
I'd like to have another column in this table where the first row would be:
A B C D AMT NEWCOL
6 BAC456 5/1/2019 TEST 25 131
Second row would be:
A B C D AMT NEWCOL
2 EPS123 4/1/2019 TEST 45 65
etc..
In cases where the month column is the first month in the entire table NEWCOL would be 0
To get 131, I'm assuming that you are requiring a match on both columns A and B.
NewCol =
CALCULATE (
SUM ( Table1[AMT] ),
ALLEXCEPT ( Table1, Table1[A], Table1[B] ),
PREVIOUSMONTH ( Table1[C] )
)
This sums the column AMT keeping the row context of columns A and B and specifies the previous month as a filter on C. Note that this returns a blank for rows that don't have a previous month. If you'd prefer 0 then add + 0 after the last closing parenthesis.
If PREVIOUSMONTH doesn't work, then try this:
NewCol =
CALCULATE (
SUM ( Table1[AMT] ),
ALLEXCEPT ( Table1, Table1[A], Table1[B] ),
Table1[C] = EOMONTH ( EARLIER( Table1[C] ), -2 ) + 1
)
For date = 5/1/2019, EOMONTH ( date, -2 ) returns 3/31/2019. Add one day to get 4/1/2019.
To achieve this easily in a calculated column, you need something like this before writing the final DAX.
Month Num = MONTH(MyTable[C])
Month Diff = DATEDIFF(MyTable[C],MAX(MyTable[C]),MONTH)
And now you have these Month differences and Month numbers defined, you can write a DAX like this -
Amount - New Column =
Var selectedValue_B = MyTable[B]
Var SelectedValue_MonthDiff = MyTable[Month Diff]
Var out1 = CALCULATE(SUM(MyTable[AMT]), FILTER(ALL(MyTable), MyTable[Month Diff] = SelectedValue_MonthDiff+1 && MyTable[B] = selectedValue_B)) + 0
return out1
This makes my table to look something like,
I have used Var(Variables) in my formula to help you understand what is happening inside the formula.
Kindly accept the answer if it solves your problem.

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%