I have the following table
Column A
Column B
ColumnC
ColumnD
Cell 1
Cell 2
C1
D1
A 2
B2
C2
D2
Based on a slicer, I wanted to create a new table where ColumnCorD is ColumnC or ColumnD. If the slicer is "ColumnC", the new table is
Column A
Column B
ColumnCOrD
Cell 1
Cell 2
C1
A 2
B2
C2
If the slicer is "ColumnD", the new table is
Column A
Column B
ColumnCOrD
Cell 1
Cell 2
D1
A 2
B2
D2
Is there an easy way to do it with Power BI DAX?
I tried to use SELECTCOLUMNS but I do not find a solution for it.
You were already on the right track. SELECTCOLUMNS does exactly, what you requested.
DynamicTable = SELECTCOLUMNS(StaticTable, "A", [A], "B", [B], "C or D", [C]&"Or"&[D])
Thank you for your answer but it is not exactly what I wanted. I wrote a DAX below but when I select the slicer, the data in the new table are not correct.
My DAX:
NewTable =
SELECTCOLUMNS(
MyData,
"ColA", MyData[ColA],
"ColB", MyData[ColB],
"ColCorD", SWITCH( TRUE(),
SELECTEDVALUE(SlicerTable[Slicer]) = "ColumnC" , MyData[ColC],
SELECTEDVALUE(SlicerTable[Slicer]) = "ColumnD" , MyData[ColD],
"No selection")
)
When I click on Slicer C, I expect the values for ColCorD to be the values from ColC.
When I click on Slicer D, I expect the values for ColCorD to be the values from ColD.
Related
I have a table called DW_XYZ:
Year
Month
Type
Value
2021
1
A
10
2021
1
B
12
2021
2
A
20
*no data for type B in Month=2
I would like to create a measure that stores data that I already filtered by last month of data stored in DW_XYZ with dax formula:
CALCULATE(
SUM(DW_XYZ[Value]),
FILTER(DW_XYZ,DW_XYZ[Month]=
CALCULATE(MAX(DW_XYZ[Month]),
FILTER(DW_XYZ,DW_XYZ[Year]=
MAX(DW_XYZ[Year]))) && DW_XYZ[Year]=MAX(DW_XYZ[Year]))
From the measure, I create 2 card visuals to display data of Type A and Type B each. 1st visual to display type A, so I drag the measure, and also drag column Type to 'visual filter' and manually select 'A', and do the same for 2nd visual (type B).
For type A, the card display expected value (of type A and Month=2).
But for type B, the card display unexpected value (of type B and Month=1), it should be type B and Month=2 which is blank/zero. **btw, would be highly appreciated if the formula including blank value-handling to be zero (0) instead of blank.
Try
MyMeasure =
VAR MaxYear =
CALCULATE ( MAX ( DW_XYZ[Year] ), ALL ( DW_XYZ[Type] ) )
VAR MaxMonth =
CALCULATE ( MAX ( DW_XYZ[Month] ), ALL ( DW_XYZ[Type] ) )
RETURN
0
+ CALCULATE (
SUM ( DW_XYZ[Value] ),
FILTER ( DW_XYZ, DW_XYZ[Year] = MaxYear && DW_XYZ[Month] = MaxMonth )
)
A crucial difference between the above and your attempt is the inclusion of ALL applied to the Type column as a filter for CALCULATE when determining the latest year and month. Without this, the external filter you are applying to the visual will force the evaluation to consider that Type only when determining those values.
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)
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.
I have two tables A and B as shown below. The AccountID in A has a relationship with the AccountID in B.
A
AccountID CmpName AccFlag SysStartTime sysEndTime
A1 Test1 1 1/1/2020 12/31/9999
A2 Test2 0 1/2/2020 12/31/9999
A3 Test3 1 1/2/2020 12/31/9999
B
ContactId AccountID ConFlag SysStartTime SysEndTime
C1 A1 1 1/1/2020 12/31/9999
C2 A1 1 1/1/2020 12/31/9999
C3 A1 0 1/1/2020 12/31/9999
C4 A2 1 1/2/2020 12/31/9999
I want to get the count of records in A that have 3 or more related records in B. I also want to get the count filtered by the Accflag, conflag, sysStartTime and sysEndTime from both the tables. I have the following DAX and it gives me the count of records in A that have 3 or more related records in B filtered by the Accflag, sysStartTime and sysEndTime of A. I want to add the filtering with ConFlag, sysStartTime and sysEndTime as well but I'm not sure how to add it to the following DAX. Please help.
SUMX ( A,
IF ( COUNTROWS ( RELATEDTABLE ( B ) ) >= 3 &&
A[Accflag]=1 &&
A[SysStartTime]>=TODAY() &&
A[SysEndTime]>= VALUE("12/31/9999"),1 )
)
I think the easiest way to do this would be to create a calculated column which indicates whether each row passes the check or not. Something like the below might work:
Ind =
VAR AccountID=A[AccountID]
VAR Count1 = CALCULATE(COUNTROWS(B),FILTER(B,B[AccountID]=AccountID))
RETURN IF(Count1>=3 && A[Accflag]=1 && A[SysStartTime]>=TODAY() && A[SysEndTime]>= VALUE("12/31/9999"),1,0)
Ind will give out 0 or 1 for each row and then you can simply sum up the field to get the total number of rows which meet each criteria. This will be useful, in case you need to add further conditions to the calculation. Hope this helps.
You could do it like this:
Go to the query editor and add a blank query. Refer this blank query (lets call this query TableBGrouped) to your TableB:
= TableB
Now apply the following step:
The relationship will look now like this:
Add a measure to TableBGrouped:
3 or more Count = CALCULATE(COUNT(TableBGrouped[AccountID]); TableBGrouped[Count] > 2)
Add filter and you got your result:
You can now apply filter:
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%