Search code examples
switch-statementpowerbidaxselectedvalue

Dynamic Title (Card Visual) based on SelectedValue


I try to implement a dynamic title (card visual) based on selectedValues of filters.

I have tried this:

Title = SWITCH( True();
SELECTEDVALUE(Daten[Contact Name]); "Aufgaben von " & SELECTEDVALUE(Daten[Contact Name]);
SELECTEDVALUE(Daten[Buchungs Datum].[Year]); "Aufgaben vom Jahr " & SELECTEDVALUE(Daten[Buchungs Datum].[Year]);
SELECTEDVALUE(Daten[Verkäufer]); "Aufgaben von Verkäufer: " & SELECTEDVALUE(Daten[Verkäufer]);
SELECTEDVALUE(Daten[Contact Name]) & SELECTEDVALUE(Daten[Buchungs Datum].[Year]); "Aufgaben von " &SELECTEDVALUE(Daten[Contact Name]) & " vom Jahr " & SELECTEDVALUE(Daten[Buchungs Datum].[Year]);
SELECTEDVALUE(Daten[Buchungs Datum].[Year]) & SELECTEDVALUE(Daten[Verkäufer]); "Aufgaben vom Jahr " & SELECTEDVALUE(Daten[Buchungs Datum].[Year]) & " vom Verkäufer: " & SELECTEDVALUE(Daten[Verkäufer])
)

I try to catch each possibilities of the applied filters. Bases on the applied filters the title card will dynamically change.

True doesn't work witch cases of type selectedvalue. Which makes sence, but now i dont know really how to get the result i want.

Is there any suggestion how I could implement this measure differently? Thank you.


Solution

  • If I got it right you are trying to return a string, based on the current selections made by the user.

    "SELECTEDVALUE()" returns the column value if there is only one value selected, otherwise an optional default value. The returned value is the column value itself (number/string/date) whatever the column contains, not strictly true/false.

    In order to make it work, you may want to use "HASONEVALUE()" which returns what you need

    Title = 
    SWITCH( True();
        HASONEVALUE(Daten[Contact Name]) && HASONEVALUE(Daten[Buchungs Datum].[Year]); "Aufgaben von " & SELECTEDVALUE(Daten[Contact Name]) & " vom Jahr " & SELECTEDVALUE(Daten[Buchungs Datum].[Year]);
        HASONEVALUE(Daten[Buchungs Datum].[Year]) && HASONEVALUE(Daten[Verkäufer]); "Aufgaben vom Jahr " & SELECTEDVALUE(Daten[Buchungs Datum].[Year]) & " vom Verkäufer: " & SELECTEDVALUE(Daten[Verkäufer]);
        HASONEVALUE(Daten[Contact Name]); "Aufgaben von " & SELECTEDVALUE(Daten[Contact Name]);
        HASONEVALUE(Daten[Buchungs Datum].[Year]); "Aufgaben vom Jahr " & SELECTEDVALUE(Daten[Buchungs Datum].[Year]);
        HASONEVALUE(Daten[Verkäufer]); "Aufgaben von Verkäufer: " & SELECTEDVALUE(Daten[Verkäufer])
    )
    

    Also, the AND operator (used in the last two cases) is "&&", not "&".

    You may improve the readability of this expression by using some variables, especially if the formula will get longer.

    Edit:

    The most restrictive expressions must be at the top, otherwise, a less restrictive condition will be triggered. ie:

    HASONEVALUE(Daten[Contact Name]) && HASONEVALUE(Daten[Buchungs Datum].[Year])
    -- since this a "subset" of the previous one, it will be true in the same moment.
    -- if it's evaluated before the other one (in the switch) then the other will never be evaluated
    HASONEVALUE(Daten[Contact Name])