I have these data
ID Daig 1 Daig 2 Daig 3 Daig 4
AA1 5 X X 2
AA2 X 1 4 X
AA3 7 3 X X
AA4 X X 2 6
AA5 X X X 3
I want to extract the min values from the columns ignoring the text(X)
I wrote the below DAX
Min Value = VAR V1 = IF(Data[Daig 1 ]="X",100,VALUE(Data[Daig 1 ]))
VAR V2 = IF(Data[Daig 2]="X",100,VALUE(Data[Daig 1 ]))
VAR V3 = IF(Data[Daig 3]="X",100,VALUE(Data[Daig 1 ]))
VAR V4 = IF(Data[Daig 4]="X",100,VALUE(Data[Daig 1 ]))
VAR temp = {V1, V2, V3, V4}
RETURN
MINX(Data,temp)
and it give error
Expected Output
This one is a measure:
Min Value =
MIN (
MIN (
MIN (
VALUE ( SUBSTITUTE ( SELECTEDVALUE ( Data[Daig 1] ), "X", 999 ) ),
VALUE ( SUBSTITUTE ( SELECTEDVALUE ( Data[Daig 2] ), "X", 999 ) )
),
VALUE ( SUBSTITUTE ( SELECTEDVALUE ( Data[Daig 3] ), "X", 999 ) )
),
VALUE ( SUBSTITUTE ( SELECTEDVALUE ( Data[Daig 4] ), "X", 999 ) )
)
And this approach i use as new calculated column:
Min_Value =
var __innerTab =
SELECTCOLUMNS(
GENERATE( VALUES(Data[ID])
, ROW(
"d1", IF(Data[Daig 1]="X",100,VALUE(Data[Daig 1])),
"d2", IF(Data[Daig 2]="X",100,VALUE(Data[Daig 2])),
"d3", IF(Data[Daig 3]="X",100,VALUE(Data[Daig 3])),
"d4", IF(Data[Daig 4]="X",100,VALUE(Data[Daig 4]))))
,
"min", min(min( Min([d1], [d2]),[d3]),[d4]))
return
__innerTab
Related
In BI, is there a way to write a function that looks at Name 2 and pulls the score from the second column for that individual? Any help would be greatly appreciated!
Name
Score
Name 2
Score 2
Bob
100
Sue
Sue
80
Nick
Nick
50
Bob
If you want to implement it as a calculated column in your data model:
Score 2 =
VAR Current_Name = Table[Name 2]
RETURN
CALCULATE ( SUM ( Table[Score] ), ALL ( Table ), Table[Name] = Current_Name )
If you want to implement it as a measure:
Score 2 =
CALCULATE (
SUM ( Table[Score] ),
ALL ( Table )
Table[Name] IN VALUES ( Table[Name 2] )
)
You sure can with the following measure
_score2 =
SUMX (
'fact',
VAR _0 = 'fact'[Name 2]
VAR _1 =
CALCULATE (
CALCULATE ( SUM ( 'fact'[Score] ), 'fact'[Name] = _0 ),
ALL ( 'fact' )
)
RETURN
_1
)
How can I write a measure to count the number of userID for which sum(x1) is equal to count(order_id), in Power BI?
For example, my data table is:
userID
x1
order_id
141
1
719
172
0
616
172
0
189
172
0
2211
172
0
317
1103
1
98
1103
1
213
1103
1
15
2524
0
4902
2524
1
3620
and I use table visual of power bi for this, to explain my mean:
userID
sum(x1)
count(order_id)
141
1
1
172
0
4
1103
3
3
2524
1
2
Note that the userID column is one of the columns in my data table, and calculating sum(x1) and count(order_id) in this sample is by Power BI default features.
The result for this sample should be 2. I need a measure that returns 2.
Measure1 =
VAR _base1 =
SUMMARIZE ( 'Table 1', 'Table 1'[userID] )
VAR _base2 =
ALLEXCEPT ( 'Table 1', 'Table 1'[userID] )
VAR _ct =
ADDCOLUMNS ( _base1, "X", CALCULATE ( COUNT ( 'Table 1'[order_id] ), _base2 ) )
VAR _sum =
ADDCOLUMNS ( _base1, "X", CALCULATE ( SUM ( 'Table 1'[x1] ), _base2 ) )
VAR _nt =
NATURALINNERJOIN ( _sum, _ct )
RETURN
COUNTROWS ( _nt )
or
Measure4 =
VAR _1 =
COUNTX (
VALUES ( 'Table 1'[userID] ),
VAR _base =
ALLEXCEPT ( 'Table 1', 'Table 1'[userID] )
VAR _1 =
CALCULATE ( SUM ( 'Table 1'[x1] ), _base )
VAR _2 =
CALCULATE ( COUNTROWS ( 'Table 1' ), _base )
VAR _3 =
IF ( _1 = _2, 1 )
RETURN
_3
)
RETURN
_1
This should work
count_valid_rows =
VAR sum_x1_table =
SUMMARIZECOLUMNS ( 'table'[userID], 'table', "sumx1", SUM ( 'table'[x1] ) )
VAR count_orderId_table =
SUMMARIZECOLUMNS (
'table'[userID],
'table',
"countOfOrders", COUNT ( 'table'[x1] )
)
RETURN
COUNTROWS (
FILTER (
NATURALINNERJOIN ( sum_x1_table, count_orderId_table ),
[sumx1] = [countOfOrders]
)
)
Docs of the functions used.
NATURALINNERJOIN
SUMMARIZECOLUMNS
Another suggestion:
Count :=
SUMX (
SUMMARIZECOLUMNS (
'Table'[userID] ,
"Sum" , SUM ( 'Table'[x1] ),
"Count" , COUNT ( 'Table'[order_id] )
),
IF ( [Sum] = [Count] , 1 )
)
As you see from the other answers there are heaps of ways to calculate this. I suggest you look over all the suggestions to understand what is going on in each, and then write out your preferred way of dealing with this type of issue after.
Your new measure may looks like this one:
calculate( countrows('YourTabel'), FILTER(ALL('YourTabel'), somestatementIfneeded && var __x1 = [x1] var __x2 = [x2] return __x1 = __x2))
The main part is to use variable PLACEHOLDER;
My data:
week
sale
20
50
20
20
21
10
21
10
22
5
22
5
Desired result:
week
sale
change
20
50
any
20
20
any
21
10
-50
21
10
-50
22
5
-10
22
5
-10
Where:
Week 20 total = 70,
Week 21 total = 20,
Week 22 total = 10
Diff for week 21 = 20-70 = -50
Diff for week 22 = 10-20 = -10
Note: "any" = can be anything (i.e. 0 or null)
Calculated column
Column 2 =
VAR _1 =
CALCULATE ( MAX ( 'Table'[week] ) ) - 1
VAR _2 =
SUMX ( FILTER ( ALL ( 'Table' ), 'Table'[week] = _1 ), 'Table'[sale] )
VAR _3 =
CALCULATE ( SUM ( 'Table'[sale] ), ALLEXCEPT ( 'Table', 'Table'[week] ) )
VAR _4 =
IF ( ISBLANK ( _2 ) = TRUE (), BLANK (), _3 )
RETURN
_4
Measure
currentWeekTotal:= SUM('Table'[sale])
prevWeekTotal:=
VAR _1 = MAX('Table'[week])-1
VAR _2 = SUMX(FILTER(ALL('Table'),'Table'[week]=_1),'Table'[sale])
RETURN _2
change:= IF([prevWeekTotal]=BLANK(),BLANK(),[currentWeekTotal]-[prevWeekTotal])
Here is a simple code for a calculated column :
change =
VAR _firstweek = MIN ( 'Table'[week] )
VAR _thisweek = 'Table'[week]
VAR _thisweeksales = CALCULATE ( SUM ( 'Table'[sale] ) , ALLEXCEPT ( 'Table' , 'Table'[week] ) )
VAR _lastweeksales = CALCULATE ( SUM ( 'Table'[sale] ) , ALL ( 'Table' ) , 'Table'[week] = _thisweek - 1 )
VAR _diff = IF ( _thisweek <> _firstweek , _thisweeksales - _lastweeksales )
RETURN _diff
This also handles skipped weeks that are not the first week.
I have data like this,
App_Num Days Price
A1 10 100
A1 11 150
A2 11 200
A3 12 250
A3 12 300
A4 20 350
A4 21 400
The average of the days is displayed on a card visual as 13.857.
Now, there are two parameters that are set for user to adjust the values and see.
Total Value (Min and Max Range)
Days
For example, if the user selects 0-280- it is expected to list A1 (100 + 150 = 250 less than 280) and A2 (200 being less than 280).
I used a DAX like this and built a table like this,
Apps_in_scope =
Var min_amount = Min('Total Value'[Total Value])
Var max_amount = Max('Total Value'[Total Value])
var required_app_num = SELECTEDVALUE(Table1[App_Num])
Var required_amount = CALCULATE(sum(Table1[Price]),FILTER(Table1,Table1[App_Num] = required_app_num))
var in_scope = if(And(required_amount <= max_amount, required_amount >= min_amount),1,0)
return in_scope
And I was able to produce a Visual like this,
App_Num Apps_in_scope
A1 1
A2 1
A3 0
A4 0
Now after selecting the total price range, if the user selects the days parameter manually to be 15 then my average will shift as per this logic.
A1 has 2 transactions and with in the selected price range of 280 will become (15*2)
A2 has 1 transaction and with in the selected price range of 280 become (15*1)
A3 has 2 transaction and will remain unchanged (12+12)
A4 has 2 transactions and will remain unchanged (20+21)
So my new measure which I want to place on the card is expected to show now (15+15+15+12+12+20+21)/7 = 15.714
How can I write this measure. Kindly help me with this
I'd tweak your measure slightly so that it works better for taking the average:
Apps_in_scope_2 =
VAR min_amount = MIN ( 'Total Value'[Total Value] )
VAR max_amount = MAX ( 'Total Value'[Total Value] )
VAR required_amount =
CALCULATE ( SUM ( Table1[Price] ), ALLEXCEPT ( Table1, Table1[App_Num] ) )
VAR in_scope =
IF ( AND ( required_amount <= max_amount, required_amount >= min_amount ), 1, 0 )
RETURN
in_scope
With this tweak the average is fairly simple:
AvgMeasure =
VAR DaysParam = SELECTEDVALUE ( DaysSlicer[Days] )
RETURN
AVERAGEX( Table1, IF( [Apps_in_scope_2] = 1, DaysParam, Table1[Days] ) )
Edit:
Here's an alternative version that doesn't use the first measure but should scale better to large data tables.
AvgAlternate =
VAR min_amount = MIN ( 'Total Value'[Total Value] )
VAR max_amount = MAX ( 'Total Value'[Total Value] )
VAR DaysParam = SELECTEDVALUE ( DaysSlicer[Days] )
VAR apps =
ADDCOLUMNS (
SUMMARIZE (
Table1,
Table1[App_Num],
"#Price", SUM ( Table1[Price] ),
"#Rows", COUNT ( Table1[Price] )
),
"#Days",
IF (
AND ( [#Price] <= max_amount, [#Price] >= min_amount ),
DaysParam * [#Rows],
CALCULATE ( SUM ( Table1[Days] ) )
)
)
RETURN
DIVIDE ( SUMX ( apps, [#Days] ), SUMX ( apps, [#Rows] ) )
This is assuming that you have separate tables for your Price range and Days selection (as in what-if parameter tables).
My measure =
VAR apps =
SELECTCOLUMNS (
FILTER (
SUMMARIZE ( Table1, Table1[App_Num], "Total Price", SUM ( Table1[Price] ) ),
[Total Price] >= MIN ( 'Total Value'[Total Value] )
&& [Total Price] <= MAX ( 'Total Value'[Total Value] )
),
"App_Num", [App_Num]
)
RETURN
AVERAGEX (
Table1,
IF ( Table1[App_Num] IN apps, SELECTEDVALUE ( Days[Days] ), Table1[Days] )
)
The context:
Column 1 Column 2 Column 3 Column 4 (IF statement result column)
a,b 2.99 $2.99 1/2 mismatch
a a,b 3.49 $2.89 Column 1/2 mismatch,3/4 mismatch
b a 1.99 $2.99 Column 1/2 mismatch,3/4 mismatch
a,b a,b 3.49 $3.49
so only in stance of and exact match (I have many columns that have comparable values that I need to follow this pattern). Is there a way to do this?
I don't think you need nested IF statements, just CONCATENATEX:
Result =
VAR ColList = { [Column1], [Column2], <...more columns if needed..> }
RETURN
CONCATENATEX ( FILTER ( ColList, LEN ( [Value] ) > 0 ), [Value], "," )
Edit: The basic approach above can still be used but you'll want to modify what is in your ColList.
Result =
VAR Pair12 = IF ( [Column 1] <> [Column 2], "1/2 mismatch" )
VAR Pair34 = IF ( [Column 3] <> [Column 4], "3/4 mismatch" )
VAR ColList = FILTER ( { Pair12, Pair34 }, LEN ( [Value] ) > 0 )
RETURN
"Column " & CONCATENATEX ( ColList, [Value], "," )
Or refactored a bit to remove variables:
Result =
"Column "
& CONCATENATEX (
FILTER (
{
IF ( [Column 1] <> [Column 2], "1/2 mismatch" ),
IF ( [Column 3] <> [Column 4], "3/4 mismatch" )
},
LEN ( [Value] ) > 0
),
[Value],
","
)