count of matching words of two text columns in power bi - powerbi

i have a table , which has two columns id and text. This is what data i have:
id text
765 hi how are you
876 John made $57.
743 apple is my favourite fruit.
435 mango is not my favorite fruit.
892 this is my favourite movie
have input column as slicer,in which i can choose any one id.for eg: 743
-765
-876
-**743**
-435
-892
I need the output, in which it would show the id ,text,number of words matching of the selected id from slicer with all the other ids:
id text matching words
743 apple is my favourite fruit. 5
765 hi how are you 0
876 John made $57. 0
435 mango is not my favorite fruit. 4
892 this is my favourite movie 3

OK, I have a solution but I'm not convinced it is the most elegant.
Thanks to the following resource for a text.split function in DAX https://www.excelnaccess.com/text-split-using-dax/.
Create a base table named Table as follows:
Create 2 calculated tables with the following code.
New Table1 =
VAR myvalues =
ADDCOLUMNS ( 'Table', "Paths", TRIM( SUBSTITUTE ( SUBSTITUTE( [text],".","") , " ", "|" ) ))
RETURN
SELECTCOLUMNS (
GENERATE (
myvalues,
ADDCOLUMNS (
GENERATESERIES ( 1, PATHLENGTH ( [Paths] ) ),
"#word", PATHITEM ( [Paths], [Value], TEXT )
)
),
"id", [id],
"word", [#word],
"text", [text]
)
New Table2 =
VAR myvalues =
ADDCOLUMNS ( 'Table', "Paths", TRIM( SUBSTITUTE ( SUBSTITUTE( [text],".","") , " ", "|" ) ))
RETURN
SELECTCOLUMNS (
GENERATE (
myvalues,
ADDCOLUMNS (
GENERATESERIES ( 1, PATHLENGTH ( [Paths] ) ),
"#word", PATHITEM ( [Paths], [Value], TEXT )
)
),
"id", [id],
"word", [#word],
"text", [text]
)
Drag New Table 1 [id] to a slicer
Drag New Table 2 [id] and [text] to a table.
Create a measure as follows and drag it into the table.
Matching Words = IF(ISFILTERED('New Table1'[id]), COUNTROWS('New Table2')+0, 0)
Create a relationship between word and word as follows:
That should be everything.

Related

Power BI - Dynamic Table with Measure

I have a Dataset like this
patient_name
age
gender
test_result
Alex
48
M
Positive
Joe
35
M
Negative
Divya
45
F
Positive
And in my power BI Dashboard i need to display a free form table like below
Item Description
Value
Total number of Adult-Male Patients with Positive Test Result
1252
Total number of Adult-Female Patients with Positive Test Result
856
Percent of Positive
2.8
I have the measures created for calculations. I tried to create a custom table with measures, but they are not changing dynamically. The table is showing only static values, when slicer selected value changes.
Is there a better way to present this ?
Thank you,
NSR
first create an empty table
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i44FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Item Description" = _t, Value = _t])
in
Source
then we will fill this table with your measures by creating a table...
Modelling --> New Table
Report Table =
UNION (
Report,
ROW (
"Item Description", "Total number of Adult-Male Patients with Positive Test Result",
"Value",
CALCULATE (
COUNT ( 'Table'[test_result] ),
FILTER (
ALLSELECTED ( 'Table' ),
'Table'[test_result] = "positive"
&& 'Table'[gender ] = "m"
&& 'Table'[age ] > 18
)
)
),
ROW (
"Item Description", "Total number of Female-Male Patients with Positive Test Result",
"Value",
CALCULATE (
COUNT ( 'Table'[test_result] ),
FILTER (
ALLSELECTED ( 'Table' ),
'Table'[test_result] = "positive"
&& 'Table'[gender ] = "f"
&& 'Table'[age ] > 18
)
)
),
ROW (
"Item Description", "Percentage Positive",
"Value",
FORMAT (
DIVIDE (
CALCULATE (
COUNT ( 'Table'[test_result] ),
FILTER (
ALLSELECTED ( 'Table' ),
'Table'[test_result] = "positive"
&& 'Table'[age ] > 18
)
),
CALCULATE ( COUNT ( 'Table'[test_result] ), ALL ( 'Table' ) )
),
"Percent"
)
)
)
did you try ALLSELECTED in your measures?
Total number of Adult-Male Patients with Positive Test Result =
CALCULATE (
COUNT ( 'Table'[test_result] ),
FILTER (
ALLSELECTED ( 'Table' ),
'Table'[test_result] = "positive"
&& 'Table'[gender ] = "m"
&& 'Table'[age ] > 18
)
)

Count Occurances of value in 2 columns

I have a simple job to do in PowerBi but for some reason I can not get my head around it.
I have projects table - in this table we have "date of start" and "date of end". What I am after is a smaller table with "Year & Quarter" first column and a count of projects started and count of project ended in that "Year & Quarter".
Project Number
Started
Ended
xxxx23
2019-01
2019-03
xxxx24
2019-03
2020-01
xxxx25
2019-03
2020-02
what I am after is something like below:
Year & Quarter
Project Started
Project Ended
2019-01
1
0
2019-03
2
1
2019-04
0
0
When doing the calculations, I am getting wrong counts. Any help is really appreciated!
You can use UNION to create a new table. You can use the original table twice but with different columns.
NewTable =
UNION(
SELECTCOLUMNS( Projects,
"date",Projects[started],
"project",Projects[project],
"Started",1,
"Ended",0
)
,
SELECTCOLUMNS( Projects,
"date",Projects[ended],
"project",Projects[project],
"Started",0,
"Ended",1
)
)
If you have a Calendar Table, you can achieve the ask by doing following two ways
#1
newTable =
VAR _1 =
SUMMARIZE ( 'Calendar', 'Calendar'[Yr & Qt] )
VAR _2 =
ADDCOLUMNS (
ADDCOLUMNS (
_1,
"Project Started",
COUNTX (
FILTER ( Projects, Projects[started] = EARLIER ( [Yr & Qt] ) ),
[started]
)
),
"Project Ended", COUNTX ( FILTER ( Projects, Projects[ended] = EARLIER ( [Yr & Qt] ) ), [ended] )
)
RETURN
_2
#2
Table 2 =
ADDCOLUMNS (
ADDCOLUMNS (
SUMMARIZE ( 'Calendar', 'Calendar'[Yr & Qt] ),
"Project Start Count",
CALCULATE (
CALCULATE (
COUNTX ( Projects, Projects[started] ),
TREATAS ( SUMMARIZE ( 'Calendar', 'Calendar'[Yr & Qt] ), Projects[started] )
)
)
),
"Project End Count",
CALCULATE (
CALCULATE (
COUNTX ( Projects, Projects[ended] ),
TREATAS ( SUMMARIZE ( 'Calendar', 'Calendar'[Yr & Qt] ), Projects[ended] )
)
)
)
results in following
The solution has minimum dependency to a Calendar tbl with a column like Yr & Qt as following

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

Power BI if condition if true then column with date value else NULL

The below table has 2 columns
Where Column A is a Date column and Column B is a Text column where some values are equal to "x" and some are blank.
I need to create an output column which based on the below formula
IF (
AND ( ColumnA < EOMONTH ( ColumnA, 3 ), ( ColumnB = "x" ) ),
EOMONTH ( ColumnA, 3 ),
"-"
)
I have written the following DAX formula for it:
Output =
IF (
AND (
ColumnA
< EOMONTH ( DATE ( YEAR ( ColumnA ), MONTH ( ColumnA ), DAY ( ColumnA ) ), 3 ),
( ColumnB = "x" )
),
EOMONTH ( ColumnA, 3 ),
"-"
)
I'm getting an error with this formula that NULL is not allowed in this context
Note: We can leave Blank in place of "x".
How do I write the correct DAX formula to achieve the above?
The problem with your calculation is that you are mixing different data types in the same column.
The Output column is handling a date data types with a text data types, that's why you are getting an error. The columns could only handle date or text but not both at the same time.
To fix your calculation your need to change your ELSE statement from "-" to BLANK()

Convert comma separated text to a list of numbers

In Power BI, I have created a DAX query created with a var giving comma-separated text using CONCATENATEX function.
Output like
Var = "1,2,3,4,5,6"
Now I want to search this var into my table column with syntax
Table[col] in {var}.
It is throwing an error.
I even tried converting the column to string with syntax
Convert(table[col], string) in {var}
The error is removed but the data doesn't match with column data.
I found this Power BI community thread that is essentially the same question.
The solution is to convert the string into a path object.
Here's the code from the link:
Measure 3 =
VAR mymeasure =
SUBSTITUTE ( [Measure], ",", "|" )
VAR Mylen =
PATHLENGTH ( mymeasure )
VAR mytable =
ADDCOLUMNS (
GENERATESERIES ( 1, mylen ),
"mylist", VALUE ( PATHITEM ( mymeasure, [Value] ) )
)
VAR mylist =
SELECTCOLUMNS ( mytable, "list", [mylist] )
RETURN
CALCULATE ( COUNTROWS ( Table1 ), Table1[ID] IN mylist )
There's a bit of redundancy in the above, so I'd probably condense it a bit and write it like this:
SomeMeasureFilteredByVar =
VAR PathVar = SUBSTITUTE ( [Var], ",", "|" )
VAR VarList =
SELECTCOLUMNS (
GENERATESERIES ( 1, PATHLENGTH ( PathVar ) ),
"Item", VALUE ( PATHITEM ( PathVar, [Value] ) )
)
RETURN
CALCULATE ( [SomeMeasure], 'Table'[ID] IN VarList )
Edit: To just create a calculated table, you can skip the last step:
TableFromString =
VAR PathVar = SUBSTITUTE ( "1,2,3,4,5,6", ",", "|" )
RETURN
SELECTCOLUMNS (
GENERATESERIES ( 1, PATHLENGTH ( PathVar ) ),
"Item", VALUE ( PATHITEM ( PathVar, [Value] ) )
)
Note that it is not possible to create a calculated table that is dynamically responsive to report filters and slicers. Materialized calculated tables are only calculated once when the data loads, not every time you adjust a filter or slicer.
Therefore, you can use a measure in the table instead of the string "1,2,3,4,5,6" but the table output will be the same regardless of what the measure returns within different filter contexts.