Slicer filtering on a list inside a table - powerbi

assuming I have a table that has a list inside
+---------+--------------+
| tag | val |
+---------+--------------+
| [a,b,c] | 1 |
| [a,e] | 2 |
| [f,g] | 3 |
| [e,f] | 4 |
+---------+--------------+
can I create a slicer that when selected will still filter the item inside the list of the tag column?
eg. i select on the filter "a" it will show 1 and 2. "e" will filter 2 and 4, "f" will filter 3 etc.

You may create a measure (returning 1/0) where you use PATHCONTAINS function. We need to remove square bracket and replace commas to pipe "|"; This measure you can put to filter pane in table/matrix visualization https://dax.guide/pathcontains/
ForFilter =
var __selectedTag = SELECTEDVALUE(disconnected[tagList])
var __tags = SELECTEDVALUE('Table'[Tag])
var __path = SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(__tags,",","|"),"[",""),"]","")
return
IF(PATHCONTAINS(__path, __selectedTag),1,0)
EDIT:
version for multiple selection
var __string = CONCATENATEX( VALUES(disconnected[tagList]), disconnected[tagList],"|")
var __tags = SELECTEDVALUE('Table'[Tag])
var __path = SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(__tags,",","|"),"[",""),"]","")
VAR Table0 =
SELECTCOLUMNS(
TOPN(1,
SELECTCOLUMNS(
ADDCOLUMNS (
GENERATE (
ROW ( "tag", __path ,"Text",__string ),
VAR TokenCount =
PATHLENGTH ([Text] )
RETURN
GENERATESERIES ( 1, TokenCount )
),
"Word", PATHITEM ([Text], [Value] )
),
"Word",IF(PATHCONTAINS([tag],[Word]),1,0),
"Tag", [tag],
"Values", [Value]
), [Word],DESC, [Values])
,"Bool", [Word])
return
Table0

Related

DAX PERCENTILE.INC by category in calculated column

I would like to calculate a quartile (ntile) by category using PERCENTILE.INC in a calculated column. My question is a variation to this question:
Equal bins in DAX equivalent of NTILE function
How to modify the accepted solution to the problem if we want to calculate the result by a category?
Bucket =
VAR N = 4
VAR Percentiles =
ADDCOLUMNS (
GENERATESERIES ( 1, N ),
"Percentile", PERCENTILE.INC ( Table1[Col1], [Value] / N )
)
RETURN
MINX ( FILTER ( Percentiles, Table1[Col1] <= [Percentile] ), [Value] )
I tried this but the results are not desired values:
VAR N = 4
VAR Percentiles =
ADDCOLUMNS(
CROSSJOIN( VALUES( Tab[Category] ), GENERATESERIES( 1, N ) ),
"Percentile",
PERCENTILEX.INC(
VAR Category = 'Tab'[Category] RETURN FILTER( Tab, 'Tab'[Category] = Category ),
Tab[Quantity] * 1.0,
[Value] / N
)
)
RETURN
MINX(
FILTER(
Percentiles,
Tab[Quantity] <= [Percentile]
&& 'Tab'[Category] = [Category]
),
[Value]
)
Edit.
Sample data:
Table =
DATATABLE (
"No", INTEGER,
"Category", STRING,
"Quantity", DOUBLE,
{
{ 1 , "apple" , 1 },
{ 2 , "apple" , 5 },
{ 3 , "apple" , 1 },
{ 4 , "apple" , 4 },
{ 5 , "apple" , 1 },
{ 6 , "apple" , 2 },
{ 7 , "apple" , 5 },
{ 8 , "apple" , 4 },
{ 9 , "banana" , 9 },
{ 10 , "banana" , 7 },
{ 11 , "banana" , 6 },
{ 12 , "banana" , 4 },
{ 13 , "banana" , 5 },
{ 14 , "banana" , 7 },
{ 15 , "banana" , 8 },
{ 16 , "banana" , 9 }
}
)
This is the solution I was suggesting in the comments from the prior thread.
Bucket =
VAR N = 4
VAR Percentiles =
ADDCOLUMNS (
GENERATESERIES ( 1, N ),
"Percentile",
VAR K = [Value] / N
RETURN
CALCULATE (
PERCENTILE.INC ( Table1[Col1], K ),
ALLEXCEPT ( Table1, Table1[Category] )
)
)
RETURN
MINX ( FILTER ( Percentiles, Table1[Col1] <= [Percentile] ), [Value] )
I managed to put my solution to work:
Bucket by Category =
VAR N = 4
VAR Percentiles =
ADDCOLUMNS(
CROSSJOIN(
SELECTCOLUMNS( VALUES( 'Table'[Category] ), "TheCategory", 'Table'[Category] ), // Change column name to new name
GENERATESERIES( 1, N )
),
"Percentile",
PERCENTILEX.INC(
VAR Category = 'Table'[Category] RETURN FILTER( 'Table', 'Table'[Category] = Category ),
'Table'[Quantity] * 1.0,
[Value] / N
)
)
RETURN
MINX(
FILTER(
Percentiles,
'Table'[Quantity] <= [Percentile]
&& 'Table'[Category] = [TheCategory] // Here we can reference the new name
),
[Value]
)
It turned out that the only fix was to change the column name of Percentiles table [Category] to something else. So that when we reference the new name [TheCategory] it is clear that we reference Percentiles table and not 'Table'. This solution may not be optimized so I welcome any other solutions.

How to get the Top N vales based on a column for each category in PowerBI?

I am facing the issue while filtering out the data based on a "Date" column to fetch top 3 for each category. Below is the sample data:
Can anybody help me with this to get the below-expected output?
You can try this (here dummy data); You can choice ASC or DESC based on your need:
Ranking by Date = var _cat = SELECTEDVALUE( Sheet1[Category])
return
IF(RANKX(FILTER(ALL(Sheet1), Sheet1[Category]= _cat), calculate(MAX(Sheet1[Date])),,ASC ,Skip)<=2, 1,BLANK())
or by Sale:
Ranking by Sales =
IF (
ISINSCOPE ('Sheet1'[Date] ),
VAR ProductsToRank = 2
VAR SalesAmount = [SumOf]
RETURN
IF (
SalesAmount > 0,
VAR VisibleProducts =
CALCULATETABLE (
VALUES ( 'Sheet1' ),
ALLSELECTED ( 'Sheet1'[Date] )
)
VAR Ranking =
RANKX (
VisibleProducts,
[SumOf],
SalesAmount
)
RETURN
IF (
Ranking > 0 && Ranking <= ProductsToRank,
1
)
)
)
Or you can create a new table in DAX like this:
Top2 = GENERATE(VALUES(Sheet1[Category]), TOPN(2, FILTER(SELECTCOLUMNS(ALL(Sheet1[Category], Sheet1[Date]),"Cat",[Category],"Date",[Date]),[Cat] = [Category]),[Date]))

Power BI Dax - Trying to break circular dependency after one attempt

I am trying to figure out ancestor bloodline in data I have. I feel like there is something I am missing to make this work. This data wouldn't change so not sure if I am missing something I can write in the power query editor when loading / refreshing the data.
While technically it is circular in fields it never going to return the same row it is currently in and the first generation is hard number making a starting point. I only want to reference the mother and father to calculate the child's bloodline. The first generation is a basic IF() statement. Below is as far as I can get before hitting the circular dependency error. I have tried a few things to break it thinking its going to loop.
Logic is:
Each blood is 100% for 1st generations based on their birthplace then it is ((mother blood + father blood) / 2) for each generation after that. I found I can use PATHITEM() to isolate the type of blood but errors with a circular dependency. (This is where I can't figure out how to reference the mother / father to do the calculation.) If I take this part out I get the image below working for 1st generation and correct mother / father for second generation.
Asisa Blood =
VAR current_id = 'Sheet1'[ID]
VAR current_gen = 'Sheet1'[Generation]
VAR current_blood = 'Sheet1'[Birthplace]
VAR current_mother_blood =
PATHITEM(
CALCULATE(
DISTINCT('Sheet1'[Mother's Blood Mix]),
FILTER(
ALLNOBLANKROW('Sheet1'[ID]),
'Sheet1'[ID] = current_id
),
REMOVEFILTERS('Sheet1')
),1,INTEGER)
VAR current_father_blood =
PATHITEM(
CALCULATE(
DISTINCT('Sheet1'[Father's Blood Mix]),
FILTER(
ALLNOBLANKROW('Sheet1'[ID]),
'Sheet1'[ID] = current_id
),
REMOVEFILTERS('Sheet1')
),1,INTEGER)
VAR gen1_value = 100
RETURN
IF(AND(LOWER(current_gen) = "1",LOWER(current_blood) = "asisa"),
gen1_value,
((current_mother_blood + current_father_blood)/2)
)
Blood Mix concatenates the four blood types into one field for easy look up in next step.
Blood mix =
VAR current__id = 'Sheet1'[ID]
VAR current_blood_a = 'Sheet1'[Asisa Blood]
VAR current_blood_b = 'Sheet1'[Africa Blood]
VAR current_blood_c = 'Sheet1'[Europe Blood]
VAR current_blood_d = 'Sheet1'[North America Blood]
RETURN
current_blood_a & "|" & current_blood_b & "|" & current_blood_c & "|" & current_blood_d
Mother and Father are lookups on blood mix with mother or father ids
Mother's Blood Mix =
VAR current_id = 'Sheet1'[ID]
VAR current_gen = 'Sheet1'[Generation]
VAR gen_value = 'Sheet1'[Blood mix]
VAR current_parent_id =
IF(LOWER(current_gen) = "1",current_id,'Sheet1'[Mother ID])
VAR result =
CALCULATE(
DISTINCT('Sheet1'[Blood mix]),
FILTER(
ALLNOBLANKROW('Sheet1'[ID]),
'Sheet1'[ID] = current_parent_id
),
REMOVEFILTERS('Sheet1')
)
RETURN
result
You can try to do this way (rather high resource consuming):
NEW COLUMN:
heritage_Father = PATH(Blood[ID],(Blood[FatherID]))
heritage_Mother = PATH(Blood[ID],(Blood[MotherID]))
Mancestor = LOOKUPVALUE(Blood[heritage_Mother],Blood[ID],Blood[FatherID]) &"|"& Blood[heritage_Mother]
Fancestor = LOOKUPVALUE(Blood[heritage_Father],Blood[ID],Blood[MotherID])&"|"&Blood[heritage_Father]
NEW MEASURE:
ASIA_check =
VAR Table0 =
SELECTCOLUMNS(
ADDCOLUMNS (
GENERATE (
ROW ( "Text", "0"&SELECTEDVALUE(Blood[Fancestor]) ),
VAR TokenCount =
PATHLENGTH ( [Text] )+1
RETURN
GENERATESERIES ( 1, TokenCount )
),
"Word", PATHITEM ( [Text], [Value] )
),
"Word",VALUE([Word]))
VAR Table1 =
SELECTCOLUMNS(
ADDCOLUMNS (
GENERATE (
ROW ( "Text", "0"&SELECTEDVALUE(Blood[Mancestor]) ),
VAR TokenCount =
PATHLENGTH ( [Text] )+1
RETURN
GENERATESERIES ( 1, TokenCount )
),
"Word", PATHITEM ( [Text], [Value] )
),
"Word",VALUE([Word]))
RETURN
DIVIDe(
if(SELECTEDVALUE(Blood[Generation]) <> 1,
calculate(sum(Blood[AsiaBlood]), FILTER(ALL(Blood), Blood[ID] in Table0 || Blood[ID] in Table1)),0
),
(POWER(2, SELECTEDVALUE(Blood[Generation])-1)))
AFRICA_check =
VAR Table0 =
SELECTCOLUMNS(
ADDCOLUMNS (
GENERATE (
ROW ( "Text", "0"&SELECTEDVALUE(Blood[Fancestor]) ),
VAR TokenCount =
PATHLENGTH ( [Text] )+1
RETURN
GENERATESERIES ( 1, TokenCount )
),
"Word", PATHITEM ( [Text], [Value] )
),
"Word",VALUE([Word]))
VAR Table1 =
SELECTCOLUMNS(
ADDCOLUMNS (
GENERATE (
ROW ( "Text", "0"&SELECTEDVALUE(Blood[Mancestor]) ),
VAR TokenCount =
PATHLENGTH ( [Text] )+1
RETURN
GENERATESERIES ( 1, TokenCount )
),
"Word", PATHITEM ( [Text], [Value] )
),
"Word",VALUE([Word]))
RETURN
DIVIDe(
if(SELECTEDVALUE(Blood[Generation]) <> 1,
calculate(sum(Blood[AfricaBlood]), FILTER(ALL(Blood), Blood[ID] in Table0 || Blood[ID] in Table1)),0
),
(POWER(2, SELECTEDVALUE(Blood[Generation])-1)))

Compare similar values within a table in power BI

I have fields within a table with values originally filled in by hand. Even if the same values are entered/meant, there can be "slight" deviations. Now I want to compare whether values in 2 columns within a row are quite similar.
If there is some similarity, I would like to have a True in a new column, otherwise a False. The use case is similar to the fuzzy join when two tables are merged, but the fields are within a table and do not work as a primary key. I have created a table below of what this should look like:
No
​A header
Another header
Calculated Column
1
Zürich, 1.OG Telefonzentrale
Telefonzentrale
True
2
Mittelterrasse 1.OG Raum T190
Mittelterrasse T1
True
1
TM-Raum 225
Bern, Bollwerk 10 / 2.OG
False
2
G7803
91G7803
True
It would be great if someone could help me in this topic.
I dont know if is there a way to do this, but we can try to verify how many word from column1 appear in column2:
CheckIfTrue__ =
VAR SplitByCharacter = " "
VAR Org = SELECTEDVALUE(Sheet3[​A header])
VAR CurrentF = SELECTEDVALUE(Sheet3[Another header] )
VAR Table0 =
SELECTCOLUMNS(
ADDCOLUMNS (
GENERATE (
ROW ( "Text", Org),
VAR TokenCount =
PATHLENGTH ( SUBSTITUTE ( [Text], SplitByCharacter, "|" ) )
RETURN
GENERATESERIES ( 1, MAX(TokenCount,1) )
),
"Word", PATHITEM ( SUBSTITUTE ( [Text], SplitByCharacter, "|" ), [Value] )
),
"Word",[Word])
VAR Table1 =
SELECTCOLUMNS(
ADDCOLUMNS (
GENERATE (
ROW ( "Text", CurrentF),
VAR TokenCount =
PATHLENGTH ( SUBSTITUTE ( [Text], SplitByCharacter, "|" ) )
RETURN
GENERATESERIES ( 1, MAX(TokenCount,1) )
),
"Word", PATHITEM ( SUBSTITUTE ( [Text], SplitByCharacter, "|" ), [Value] )
),
"Word",[Word])
RETURN
COUNTROWS(INTERSECT(Table0, Table1))+0

POWER BI : I want DAX formula to find out the 1st highest and 2nd highest value for a group in my Main Table

Below are my two tables , I want the 1st and 2nd highest amount from my main table through DAX, So that I can get the Desired table with two different column , one having first highest for Sydney and Brisbane and another having 2nd highest for the same cities
Table 2 =
VAR _1 =
ADDCOLUMNS (
_t,
"rank", RANKX ( ALLEXCEPT ( _t, _t[City] ), CALCULATE ( MAX ( _t[val] ) ),, DESC )
)
VAR _2 =
SELECTCOLUMNS (
FILTER ( _1, [rank] = 1 ),
"City", [City] & "",
"firstHighest", [val] + 0
)
VAR _3 =
SELECTCOLUMNS (
FILTER ( _1, [rank] = 2 ),
"City", [City] & "",
"secondHighest", [val] + 0
)
VAR _4 =
NATURALLEFTOUTERJOIN ( _2, _3 )
RETURN
_4