How to write a nested IF statement that has multiple outputs? - powerbi

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

Related

Count Specific Word by Row in DAX

I'm trying to count the number of times the word "text" appears per row in Power BI. I've done a lot of google searching and seen examples like this:
Formula :=
CALCULATE (
COUNTROWS ( FILTER ( 'TestData', FIND ( "text", 'TestData'[Description],, 0 ) > 0 ) ),1=1
)
but it isn't quite getting me there. How can I get for row 1, a result of 1 and row 2, a result of 3.
CREATE TABLE [dbo].[TestData](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Description] [varchar](100) NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[TestData] ON
GO
INSERT [dbo].[TestData] ([ID], [Description]) VALUES (1, N'this is my demo text')
GO
INSERT [dbo].[TestData] ([ID], [Description]) VALUES (2, N'text text demo text')
GO
SET IDENTITY_INSERT [dbo].[TestData] OFF
GO
Expected Result
ID Description Text Word Count
1 this is my demo text 1
2 text text demo text 3
Calculated Column:
=
VAR MySearchText = "text"
RETURN
DIVIDE(
LEN( Table1[Description] )
- LEN( SUBSTITUTE( Table1[Description], MySearchText, "" ) ),
LEN( MySearchText )
)
Measure:
=
VAR MySearchText = "text"
VAR ThisDescription =
MIN( Table1[Description] )
RETURN
DIVIDE(
LEN( ThisDescription )
- LEN( SUBSTITUTE( ThisDescription, MySearchText, "" ) ),
LEN( MySearchText )
)
though note that both of these will return positive counts where MySearchText is found within other words: a description of "this is textual", for example, will return 1.
I don't believe DAX has a textsplit function, but you can do something like this to ensure you don't pick up words of which text is a substring.
Text Count (DAX) =
VAR pad = SUBSTITUTE(" " & [Description] & " ","text","~text~")
VAR lenPad = LEN(pad)
VAR lenText = LEN("~text~")
VAR lenRemText = LEN(SUBSTITUTE(pad,"~text~",""))
RETURN (lenPad-lenRemText)/lenText
```

how to create a new column with adding fixed value to another column cell value

I have a table looks like this
I have to create a new column C, with 1st value is the same as column B and next value should add a fix value 20 to each cell, how can I do this?
Output should look like this:
It will be a great help if someone can help me with this, thanks
You can just make it by saying:
C = ( [A] - 1 ) * 20 + 100
Alternative if you have a different value in [B] you can use the LOOKUPVALUE function and it'd look like this:
C = ( [A] - 1 ) * 20 + LOOKUPVALUE( Table[B], [B], MAX( [B] ) )
I'm not entirely sure what you're after but I'm guessing it's with random values in B and calculations based on that. If that's what you're talking about this should work:
C =
IF(
[B] = BLANK(),
LOOKUPVALUE(
'Table'[B],
'Table'[A],
CALCULATE(
LASTNONBLANK( 'Table'[A], 1),
FILTER(
'Table',
'Table'[A] <= EARLIER( 'Table'[A] )
&& not ISBLANK( 'Table'[B] )
)
)
) +
( [A] - CALCULATE(
LASTNONBLANK('Table'[A], 1),
FILTER(
'Table',
'Table'[A] <= EARLIER( 'Table'[A] )
&& not ISBLANK( 'Table'[B] )
)
) ) * 20,
[B]
)
This is what an example would look like:

Power BI taking min value from Multiple text columns

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

ALLEXCEPT not working when filtering blanks

I have a simple problem. My DAX measure does not seem to be working correctly when I filter for non-existing values. Here are some details:
Table:
Column1: A,A,A,A,A,B,B,B,B
Column2: 1,2,3,4,5,1,2,3,5
Measure = calculate(countrows(table), allexcept(column1))
Card Visual returns correct row count when I filter by column1 (any value in filtering pane)
However it returns wrong row count when I filter by column2 = "4" and Column1 = "B" (in filtering pane). It seems that it should ingore filtering by column2 and it does except when I specifically filer for value = "4". It gives "blank" result value in a card visual then.
Any ideas why?
Here's the screen. I would like to populate that blank cell with "4" (in a singe-table data model.enter image description here
In your case you dont need to add allexcept in your measure. Below code would be fine.
TestMeasure = countrows(Test_Data)
PFB screenshot
I am hoping that you have a data model as following
table name _dim1
colA
A
B
C
table name _dim2
colB
1
2
3
4
5
table name _fact
colA
colB
A
1
A
2
A
3
A
4
A
5
B
1
B
2
B
3
B
5
C
2
C
3
If you have this you can reach where you need by using following measures
Measure3 =
CALCULATE ( COUNTROWS ( _fact ), ALL ( _dim2[colB] ), VALUES ( _fact[colA] ) )
Measure9 =
VAR _1 =
MAX ( _dim2[colB] )
VAR _2 =
CALCULATE (
MAXX (
FILTER ( _dim2, _dim2[colB] <= _1 ),
LASTNONBLANKVALUE ( _dim2[colB], [Measure3] )
),
ALL ( _dim2[colB] )
)
RETURN
_2
Measure10 =
VAR _1 =
MAX ( _dim2[colB] )
VAR _2 =
CALCULATE (
MAXX (
FILTER ( _dim2, _dim2[colB] > _1 ),
FIRSTNONBLANKVALUE ( _dim2[colB], [Measure3] )
),
ALL ( _dim2[colB] )
)
RETURN
IF ( ISBLANK ( [Measure9] ) = TRUE (), _2, [Measure9] )
I don't think you can reach here from a single table like following
colA
colB
A
1
A
2
A
3
A
4
A
5
B
1
B
2
B
3
B
5
C
2
C
3

If it is the first date or last date use one aggregation; if any other day, use another aggregation

Let's assume I have the below dataset.
What I need to create the below matrix where if it is the beginning or month end, I aggregate A or B in Category 1 and calculate SUM but if it is any other day in a month but 1st or last, I am tagging A or B in Category 2 and calculate SUM. I guess I need to use SWITCH, don't I?
Edit in info from comments
Like to create 3 col:
isStart = IF ( main_table[date] = STARTOFMONTH ( main_table[date] ), 1, 0 )
isEnd = IF ( main_table[date] = ENDOFMONTH ( 'main_table'[date] ), 1, 0 )
in_between_date =
IF ( AND ( main_table[date] <> ENDOFMONTH ( 'main_table'[date] ),
main_table[date] <> STARTOFMONTH ( main_table[date] ) ), 1, 0 )
Then, create the columns with my categories, like
start_end =
IF ( OR ( NOT ( ISERROR ( SEARCH ( "A", main_table[code] ) ) ),
main_table[code] = "B" ),
"Category 1",
BLANK () )
and
in_between =
IF ( OR ( main_table[code] = "B", main_table[code] = "A" ), "Category 2", BLANK () )
But then, what should I use in switch/if ? = if(VALUES('main_table'[isStart]) = 1, then what?
You where on the right track but overcomplicated a bit. You only need one extra column "Category" giving for each row in what category the item falls.
Category =
IF (
startEnd[date] = STARTOFMONTH ( startEnd[date] )
|| startEnd[date] = ENDOFMONTH ( startEnd[date] );
"Category1";
"Category2"
)
table end result is: