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
MoeData
Advocate II
Advocate II

How to throw error based on selection of a slicer?

I have a plot that shows 3 different series of the same data that are filteres based on 3 groups of slicers. 
Each series can be activated/deactivated separately, using a slicer on , so I can choose what combination of the 3 series I want to see. 

I have a column of data and slicer in each series called "AgeGroup". Based on that I can see if for example it is legal for the person to work or not (This is a categorical type of data). I call the measures that show this, "Legal?".

Now what I want is to be able to see the existing visualization (a scatter plot) only when ALL THE ACTIVATED series have the same result in their "Legal?" measure. 

For example if I activate Series 2 and 3, I only want to be able to see the plot, if the contents of "Legal? 2" and "Legal? 3" have the same values. If Series 1 and 3 are activated, the comparison should be between "Legal? 1" and "Legal? 3" and so on. 

Link to the sample (and a bit messy) PBIX file: https://tinyurl.com/463n3rzs

@OwenAuger I feel you may have an answer for this familiar file 🙂 


throw error based on slicer selections and measure contents.png

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi again @MoeData 

A bit busier than expected so delayed in replying - thanks for your patience!

 

I have attached a PBIX with a possible solution. A few comments on this:

  1. Having Series as a Calculation Group doesn't work for what we're trying to do. This is because when a Calculation Item is applied, it is impossible to restore the overall selection of the Calculation Group column. This must relate to the way Calculation Items are applied behind the scenes. For this reason, I have changed Series to a regular table (with the same contents).
  2. To detect whether all Legal values are the same, I create a measure Legal Values All Same. This measures takes all selected Series values, evaluates the Legal measure for each, and counts distinct values. If there is 1 distinct value, it returns TRUE.
  3. To replicate the behaviour of the Calculation Group version, I have
    • Created measures Salary Value Base and EducationYear Value Base, which are similar to the original measures, but with the additional check that Legal Values All Same = TRUE.
    • Created a Series Selection measure, that grabs the selected value of Series[Series].
    • Modified Salary Value and EducationYear Value to switch the active relationship based on Series Selection, using the SWITCH function.
  4. I also simplified the code of the Legal measures using SWITCH. By the way, these measures will return blank if more than one AgeGroup is selected - just checking is this what you want?

Otherwise, the setup of the visuals is basically unchanged.

I did tinker a bit with the visuals in my PBIX as I was testing things, so I may have made some slight changes.

 

Here is the udpated DAX code:

 

Legal? 1 = 
SWITCH (
    SELECTEDVALUE ( DimEmployees1[AgeGroup] ),
    "Infant", "No",
    "Normal", "Yes",
    "Fossil", "Yes"
)
// 2 & 3 similar
Legal Values All Same = 
VAR LegalValues =
    SELECTCOLUMNS (
        ALLSELECTED ( Series ), -- Restore overall Series filter
        "@Legal",
        SWITCH (
            Series[Series],
            "Series 1", [Legal? 1],
            "Series 2", [Legal? 2],
            "Series 3", [Legal? 3]
        )
    )
VAR LegalValuesCount =
    COUNTROWS ( DISTINCT ( LegalValues ) )
RETURN
    LegalValuesCount = 1
EducationYear Value Base = 
IF (
    [Legal Values All Same]
      && NOT ISEMPTY ( DimSalary )
      && [Is Salary Count above threshold],
    SELECTEDVALUE ( DimEmployees[EducationYear] )
)
Salary Value Base = 
IF (
    [Legal Values All Same]
      && [Is Salary Count above threshold],
    SELECTEDVALUE ( DimSalary[Salary] )
)
Series Selection =
SELECTEDVALUE ( Series[Series] )
EducationYear Value = 
SWITCH (
    [Series Selection],
    "Series 1",
    CALCULATE (
        [EducationYear Value Base],
        USERELATIONSHIP ( DimEmployees[EmployeeID], DimEmployees1[EmployeeID] )
    ),
    "Series 2",
    CALCULATE (
        [EducationYear Value Base],
        USERELATIONSHIP ( DimEmployees[EmployeeID], DimEmployees2[EmployeeID] )
    ),
    "Series 3",
    CALCULATE (
        [EducationYear Value Base],
        USERELATIONSHIP ( DimEmployees[EmployeeID], DimEmployees3[EmployeeID] )
    )
)
Salary Value = 
SWITCH (
    [Series Selection],
    "Series 1",
    CALCULATE (
        [Salary Value Base],
        USERELATIONSHIP ( DimEmployees[EmployeeID], DimEmployees1[EmployeeID] )
    ),
    "Series 2",
    CALCULATE (
        [Salary Value Base],
        USERELATIONSHIP ( DimEmployees[EmployeeID], DimEmployees2[EmployeeID] )
    ),
    "Series 3",
    CALCULATE (
        [Salary Value Base],
        USERELATIONSHIP ( DimEmployees[EmployeeID], DimEmployees3[EmployeeID] )
    )
)

 

Hopefully that's what you were looking for!

Please test it out, and post back any issues or other thoughts.

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

4 REPLIES 4
OwenAuger
Super User
Super User

Hi again @MoeData 

A bit busier than expected so delayed in replying - thanks for your patience!

 

I have attached a PBIX with a possible solution. A few comments on this:

  1. Having Series as a Calculation Group doesn't work for what we're trying to do. This is because when a Calculation Item is applied, it is impossible to restore the overall selection of the Calculation Group column. This must relate to the way Calculation Items are applied behind the scenes. For this reason, I have changed Series to a regular table (with the same contents).
  2. To detect whether all Legal values are the same, I create a measure Legal Values All Same. This measures takes all selected Series values, evaluates the Legal measure for each, and counts distinct values. If there is 1 distinct value, it returns TRUE.
  3. To replicate the behaviour of the Calculation Group version, I have
    • Created measures Salary Value Base and EducationYear Value Base, which are similar to the original measures, but with the additional check that Legal Values All Same = TRUE.
    • Created a Series Selection measure, that grabs the selected value of Series[Series].
    • Modified Salary Value and EducationYear Value to switch the active relationship based on Series Selection, using the SWITCH function.
  4. I also simplified the code of the Legal measures using SWITCH. By the way, these measures will return blank if more than one AgeGroup is selected - just checking is this what you want?

Otherwise, the setup of the visuals is basically unchanged.

I did tinker a bit with the visuals in my PBIX as I was testing things, so I may have made some slight changes.

 

Here is the udpated DAX code:

 

Legal? 1 = 
SWITCH (
    SELECTEDVALUE ( DimEmployees1[AgeGroup] ),
    "Infant", "No",
    "Normal", "Yes",
    "Fossil", "Yes"
)
// 2 & 3 similar
Legal Values All Same = 
VAR LegalValues =
    SELECTCOLUMNS (
        ALLSELECTED ( Series ), -- Restore overall Series filter
        "@Legal",
        SWITCH (
            Series[Series],
            "Series 1", [Legal? 1],
            "Series 2", [Legal? 2],
            "Series 3", [Legal? 3]
        )
    )
VAR LegalValuesCount =
    COUNTROWS ( DISTINCT ( LegalValues ) )
RETURN
    LegalValuesCount = 1
EducationYear Value Base = 
IF (
    [Legal Values All Same]
      && NOT ISEMPTY ( DimSalary )
      && [Is Salary Count above threshold],
    SELECTEDVALUE ( DimEmployees[EducationYear] )
)
Salary Value Base = 
IF (
    [Legal Values All Same]
      && [Is Salary Count above threshold],
    SELECTEDVALUE ( DimSalary[Salary] )
)
Series Selection =
SELECTEDVALUE ( Series[Series] )
EducationYear Value = 
SWITCH (
    [Series Selection],
    "Series 1",
    CALCULATE (
        [EducationYear Value Base],
        USERELATIONSHIP ( DimEmployees[EmployeeID], DimEmployees1[EmployeeID] )
    ),
    "Series 2",
    CALCULATE (
        [EducationYear Value Base],
        USERELATIONSHIP ( DimEmployees[EmployeeID], DimEmployees2[EmployeeID] )
    ),
    "Series 3",
    CALCULATE (
        [EducationYear Value Base],
        USERELATIONSHIP ( DimEmployees[EmployeeID], DimEmployees3[EmployeeID] )
    )
)
Salary Value = 
SWITCH (
    [Series Selection],
    "Series 1",
    CALCULATE (
        [Salary Value Base],
        USERELATIONSHIP ( DimEmployees[EmployeeID], DimEmployees1[EmployeeID] )
    ),
    "Series 2",
    CALCULATE (
        [Salary Value Base],
        USERELATIONSHIP ( DimEmployees[EmployeeID], DimEmployees2[EmployeeID] )
    ),
    "Series 3",
    CALCULATE (
        [Salary Value Base],
        USERELATIONSHIP ( DimEmployees[EmployeeID], DimEmployees3[EmployeeID] )
    )
)

 

Hopefully that's what you were looking for!

Please test it out, and post back any issues or other thoughts.

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

@OwenAuger 

Hi Owen, 

Super! Yes, that does what I am looking for.
Thanks for the solutions! and also for well-documented and clear explanations 🙂
I really like your approach.


@OwenAuger wrote:
  1. I also simplified the code of the Legal measures using SWITCH. By the way, these measures will return blank if more than one AgeGroup is selected - just checking is this what you want?

Actually, I should make this filter a single-select one, so more than one is not possible to choose.

Best regards,
Moe
  

MoeData
Advocate II
Advocate II

Hi @OwenAuger 

Each of the three [Legal? X] measures depend only on the value of DimEmployeesX[AgeGroup] so you are correct, it is a typo. Thanks 🙂 

Best,
Moe

OwenAuger
Super User
Super User

Hi Moe 🙂

Just working through this - a bit late here so may have to reply in the morning 🙂

 

  1. Just to check, should each of the three [Legal? X] measures depend only on the value of DimEmployeesX[AgeGroup]?
    If so, there looked to be a typo in [Legal? 2] & [Legal? 3] as they also referenced DimEmployees1[AgeGroup], but that's easily corrected. The syntax of those measures can also be simplified a bit
  2. My intended solution would use ALLSELECTED to look at the distinct values of the [Legal? X] measures for all selected Series values. However, there is a technical issue in that ALLSELECTED cannot restore the "overall" filter context of a Calculation Group when it is being applied.
    So either we use an additional table in place of the Series Calculation Group and apply it via another Calculation Group, or do away with Calculation Groups entirely...undecided, but working through that. 

 

Will post back in the morning my time.

 

All the best,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

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