Power BI - search and include and exclude certain strings - powerbi

I have simple table:
Column Desired result
Messi Ronaldo 0
Ronaldo 0
Messi Pogba 0
Messi alala 1
And I try to count how many times Messi is in the string and Ronaldo and Pogba are not present.
The code I have got is:
Desired result =
IF (ISBLANK ( SEARCH ( "Messi", 'Table'[Column], 1, BLANK () ) )
&&
(
ISBLANK ( SEARCH ( "Ronaldo", 'Table'[Column], 1, BLANK () ) )
||ISBLANK ( SEARCH ( "Pogba", 'Table'[Column], 1, BLANK () ) )
)
,
0,
1)
But it does not exclude Ronaldo or Pogba?
Current(wrong) results
Column Current results
Messi Ronaldo 1
Ronaldo 0
Messi Pogba 1
Messi alala 1

Try this instead:
Desired result =
IF (NOT(ISBLANK ( SEARCH ( "Messi", 'Table'[Column], 1, BLANK () ) ))
&& ISBLANK ( SEARCH ( "Ronaldo", 'Table'[Column], 1, BLANK () ) )
&& ISBLANK ( SEARCH ( "Pogba", 'Table'[Column], 1, BLANK () ) ),
1,
0)
This returns 1 if searching for "Messi" doesn't return a blank but the search for "Ronaldo" and "Pogba" do return a blank.

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
```

PowerBI percentage by count of another column

How do I calculate the percentage of values in column A from the count of column B?
Or the percentage of non-blank values from count of all values (blank and not)?
I have a column with unique IDs and a 4 columns with values, and I want to see the 4 columns together in a bar chart which I can do with count.
ID
A
B
C
D
12345
1
3
12346
1
2
3
4
12347
3
4
12348
3
4
With count it would show as A=2, B=1, C=4, D=3.
In the percentage I'm looking for it would show as A=50%, B=25%, C=100%, D=75%.
Thanks!
Maya
Achievable with following measures
_a =
DIVIDE (
CALCULATE ( COUNT ( tbl[A] ), ALL ( tbl[ID] ) ),
CALCULATE ( COUNT ( tbl[ID] ), ALL ( tbl[ID] ) )
)
_b =
DIVIDE (
CALCULATE ( COUNT ( tbl[B] ), ALL ( tbl[ID] ) ),
CALCULATE ( COUNT ( tbl[ID] ), ALL ( tbl[ID] ) )
)
_c =
DIVIDE (
CALCULATE ( COUNT ( tbl[C] ), ALL ( tbl[ID] ) ),
CALCULATE ( COUNT ( tbl[ID] ), ALL ( tbl[ID] ) )
)
_d =
DIVIDE (
CALCULATE ( COUNT ( tbl[D] ), ALL ( tbl[ID] ) ),
CALCULATE ( COUNT ( tbl[ID] ), ALL ( tbl[ID] ) )
)
Try this approach:
Create 4 formulas
A% = CONCATENATE(FORMAT((CALCULATE(COUNT(Sheet5[A]),ALLNOBLANKROW(Sheet5[A]))/COUNTROWS(ALL(Sheet5)))*100,"") , "%")
B% = CONCATENATE(FORMAT((CALCULATE(COUNT(Sheet5[B]),ALLNOBLANKROW(Sheet5[B]))/COUNTROWS(ALL(Sheet5)))*100,"") , "%")
C% = CONCATENATE(FORMAT((CALCULATE(COUNT(Sheet5[C]),ALLNOBLANKROW(Sheet5[C]))/COUNTROWS(ALL(Sheet5)))*100,"") , "%")
D% = CONCATENATE(FORMAT((CALCULATE(COUNT(Sheet5[D]),ALLNOBLANKROW(Sheet5[D]))/COUNTROWS(ALL(Sheet5)))*100,"") , "%")
End result is:

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

DAX Statement not returning blanks

I have imported a dataflow from a database into PowerBI. I am trying to write a DAX statement to mimic these constraints on a query I made:
SELECT COUNT(IPID) as theSwitches
FROM MVIEW_E_SWITCH
WHERE (NORMALPOSITIONA = 0 OR NORMALPOSITIONB = 0 OR NORMALPOSITIONC = 0)
AND (FEEDERID2 IS NULL)
Here is my DAX statement:
Open =
( MVIEW_E_SWITCH[NORMALPOSITIONA] = 0
|| MVIEW_E_SWITCH[NORMALPOSITIONB] = 0
|| MVIEW_E_SWITCH[NORMALPOSITIONC] = 0 )
& ( ISBLANK ( MVIEW_E_SWITCH[FEEDERID2] ) )
Dummy Data is below.
IPID NORMALPOSITIONA NORMALPOSITIONB NORMALPOSITIONC FEEDERID2
123141 1 1 1 GC12
145361 0 0 1
096842 0 0 0 BC32
053912 0 0 0
018249 1 1 1
827247 0 1 0 HD32
In DAX, & denotes string concatenation. Use && for logical AND.
Edit: Given that FEEDERID2 is text, I'm guessing your problem is that you have empty strings "" instead of true blanks. Instead of ISBLANK ( MVIEW_E_SWITCH[FEEDERID2] ) try
( ISBLANK ( MVIEW_E_SWITCH[FEEDERID2] ) || MVIEW_E_SWITCH[FEEDERID2] = "" )
or
LEN ( MVIEW_E_SWITCH[FEEDERID2] ) = 0
Try this solution if it helps mark it as the answer.
Create a calculated column with the following DAX:-
Open = IF((Table[NPA] = 0 || Table[NPB] = 0 || Table[NPC] = 0) && ISBLANK(Table[FEEDERID]), TRUE(), FALSE())
This should give you the desired output.
P.S. Column is to check if the FEEDERID is blank or not.

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: