Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

FILTER /w string variable

Hi all,

 

I'm facing a puzzle which i haven't been able to solve / google for 2 days now even though I feel it should be easy to accomplish. 

 

Quick explanation: im trying to train my DAX skills by creating a calculator for a computergame (Anno 1800), so please forgive the stupidness of the data in the tables :). The game is based on production chains. For instance: Rum is made out of wood and sugar cane. What i want to accomplish is that when I use a slicer to select "Rum", i get a table which shows me not only the row for rum, but also wood and sugar cane

 

Im at the point where i get the desired table when hardcoding the items, but of course i prefer to use a variable, as i dont want to hardcode 100+ items. Here's where things get tricky: The FILTER doesn't seem to accept any variables. I cant call them out directly, or by first "pulling them in" with a VAR. The [ING1] & [ING2] are textstrings based on a LOOKUPVALUE. I've added screenshots for reference.

 

So. In short. The variables are calculated correctly when added to a card, for example. But the same variables are not being used in my filter context as such. Is what i want even possible? What am I missing?


Thanks for your help in advance!


Ramon

 

 

create variable (textstring)create variable (textstring)

Desired result, hardcodedDesired result, hardcodedTrying variables in FILTER, no resultTrying variables in FILTER, no result

1 ACCEPTED SOLUTION

Hi @Anonymous ,

 

As far as I know Power BI doesn't support us to create calcualted table to show dynamic results. Here I have a workaround. I suggest you to create a 'Goods' table with all items and ingredients in 'Product' table.

Item and Ingredient = 
VAR _UNION =
    UNION (
        VALUES ( Products[Item] ),
        VALUES ( Products[Ingredient 1] ),
        VALUES ( Products[Alternative ingr 2] ),
        VALUES ( Products[Alternative ingr 3] )
    )
VAR _FILTER =
    SUMMARIZE ( FILTER ( _UNION, [Item] <> BLANK () ), [Item] )
RETURN
    _FILTER

Then create a measure to filter this table.

Filter = 
VAR _ITEM =
    SELECTEDVALUE ( Products[Item] )
VAR _Ingredient1 =
    SELECTEDVALUE ( Products[Ingredient 1] )
VAR _Ingredient2 =
    SELECTEDVALUE ( Products[Ingredient 2] )
VAR _Ingredient3 =
    SELECTEDVALUE ( Products[Ingredient 3] )
VAR _CURRENTVALUE =
    MAX ( 'Item and Ingredient'[Goods] )
RETURN
    IF (
        _CURRENTVALUE = _ITEM
            || _CURRENTVALUE = _Ingredient1
            || _CURRENTVALUE = _Ingredient2
            || _CURRENTVALUE = _Ingredient3,
        1,
        0
    )

Add this measure into visual level filter and set it to show items when value = 1.

Result is as below. When I select Rum in slicer, we can see Rum/Wood and Suger cane in table.

RicoZhou_0-1653982250881.png

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Hi Whitewater, 

 

thanks for your reply. I'm afraid it's a bit more complicated than that. If it was component(s) into item it would be fairly easy, despite a lot of manual labor, to create a table such as the first one you're suggesting. Unfortunately, there's a three-layer system ánd there is the ability to overwrite one of the ingredients for any other.

 

The three-layer setup means that the base table would be extremly vast, and I dont feel like writing it for hundreds of entries. The overwriting of any ingredient into any other makes it completely impossible. 

 

Thats the reason why i'm looking to use references for the filter context, but unfortunately that just doesn't seem to work. 

 

Does anyone know whether referencing to variable string texts in filter context is actually technically possible? If so, how?

 

I've linked the .pbix (dont mind the random tables created - i've just been trying things) to give you all a better idea of the base table


Thanks in advance!

Hi @Anonymous ,

 

As far as I know Power BI doesn't support us to create calcualted table to show dynamic results. Here I have a workaround. I suggest you to create a 'Goods' table with all items and ingredients in 'Product' table.

Item and Ingredient = 
VAR _UNION =
    UNION (
        VALUES ( Products[Item] ),
        VALUES ( Products[Ingredient 1] ),
        VALUES ( Products[Alternative ingr 2] ),
        VALUES ( Products[Alternative ingr 3] )
    )
VAR _FILTER =
    SUMMARIZE ( FILTER ( _UNION, [Item] <> BLANK () ), [Item] )
RETURN
    _FILTER

Then create a measure to filter this table.

Filter = 
VAR _ITEM =
    SELECTEDVALUE ( Products[Item] )
VAR _Ingredient1 =
    SELECTEDVALUE ( Products[Ingredient 1] )
VAR _Ingredient2 =
    SELECTEDVALUE ( Products[Ingredient 2] )
VAR _Ingredient3 =
    SELECTEDVALUE ( Products[Ingredient 3] )
VAR _CURRENTVALUE =
    MAX ( 'Item and Ingredient'[Goods] )
RETURN
    IF (
        _CURRENTVALUE = _ITEM
            || _CURRENTVALUE = _Ingredient1
            || _CURRENTVALUE = _Ingredient2
            || _CURRENTVALUE = _Ingredient3,
        1,
        0
    )

Add this measure into visual level filter and set it to show items when value = 1.

Result is as below. When I select Rum in slicer, we can see Rum/Wood and Suger cane in table.

RicoZhou_0-1653982250881.png

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Hi Rico,

 

thanks again for the help. Setting up a shadowtable was the obvious choice. I didnt even have to create one with join, as all ingredients and alternative ingredients are represented in the original ITEM list. Just a VALUES(item) as shadowtable, and then filtering as you suggested, did the trick perfectly.

 

Thanks for the help!

Whitewater100
Solution Sage
Solution Sage

Hi Ramon:

Do you have a way to set up some tables to help with your question? I'll paste below one way to lay out the model foundation to make the job easier:

Dim_Components     
Prduct_IDProductComponentShareCost(Optional)  
1RumWood0.1   
1RumSugar0.4   
1RumWater0.5   
2BreadWheat0.25   
2BreadFlour0.25   
2BreadYeast0.25   
2BreadMilk0.25   
3OtherA0.33   
3OtherB0.33   
3OtherC0.33   
       
       
Dim_Products      
Prod_IDNameSell_Price    
1Rum20    
2Bread3    
3Other10    
       
       
Fact Sales      
Cust_IDProd_IDTrans DateQty   
       
other potential Dim Tables - "Dates" & "Customers"  
       

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors