Forum Discussion

garimajain85's avatar
garimajain85
Frequent Visitor
2 years ago
Solved

Measure not working with Slicers: DAX

The following query works when none of the slicers on teh page are selected, but breaks when anyone of them is selected

Eff.Monthly Avg. Productivity =
IFERROR([Eff.Month Total Productivity]/[Active UA Population],CALCULATE([Eff.Month Total Productivity]/88)).
I am trying to create a measure for the following conditions:
If no  date filters are selected:
                             Divide (Total Sales by Active count of employees)
 If the selected month= any of the past month
                             Divide (Total Sales   by Active count of employees in the respective month)
If the selcted month= future month
                               Divide (Total Sales   by Max(Active count of employees in previous months)
If any other filters is selected example(manager name)
                          Then the above measures should work accordingly. So                        
                                           If the selected month= any of the past month
                                                       Divide (Total Sales for the manager / Active count of employees in his team in the respective month)
 
 
  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi,garimajain85.Hello, danextian, nandic.Thank you very much for your help with this issue.
    And now I want to share my advice with you.
    I am glad to help you.

    Regarding the issue you raised, my solution is as follows:
    1.First on your first question, the reason why metrics are affected when using a slicer is because the slicer changes the context, which may cause some metrics to return an error in that context, here's a link to that:

    Context in DAX Formulas - Microsoft Support

    2.First I have created the following table and the column names and data are the data you have given:


    3. Below are the measure I've created for your needs:

    Measure 2 = SWITCH(TRUE(),
    ISFILTERED('M'[manager]),IF(MONTH(SELECTEDVALUE(MON[1]))=MONTH(TODAY())-1,
    DIVIDE(CALCULATE(SUM('IFERROR'[Active count of employees]),FILTER(ALLSELECTED('IFERROR'),'IFERROR'[manager]=MAX('IFERROR'[manager]))),AVERAGE('IFERROR'[Active count of employees])),
    FALSE()),
    SELECTEDVALUE(MON[1])<=TODAY(),DIVIDE(CALCULATE(SUM('IFERROR'[SALE]),FILTER(ALLSELECTED('IFERROR'),MONTH('IFERROR'[month])=MONTH(SELECTEDVALUE(MON[1])))),
    CALCULATE(SUM('IFERROR'[Active count of employees]),FILTER(ALLSELECTED('IFERROR'),MONTH('IFERROR'[month])=MONTH(SELECTEDVALUE(MON[1]))))),
    SELECTEDVALUE(MON[1])>TODAY(),DIVIDE(SUM('IFERROR'[SALE]),
    CALCULATE(MAX('IFERROR'[Active count of employees]),FILTER(ALLSELECTED('IFERROR'),MONTH('IFERROR'[month])<MONTH(SELECTEDVALUE(MON[1]))))),
    FALSE())
    
    Measure = 
    VAR _a = SELECTEDVALUE('MON'[1])
    RETURN IF(_a =BLANK(),DIVIDE(SUM('IFERROR'[SALE]),SUM('IFERROR'[Active count of employees])), 
    [Measure 2])
    

    4.Here's my final result, which I hope meets your requirements.

    Can you share sample data and sample output in tabular format if I am misunderstanding? Or a sample pbix after removing sensitive data. We can better understand the problem and help you.
    Best Regards,
    Leroy Lu
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

3 Replies

  • nandic's avatar
    nandic
    Icon for Resident Rockstar rankResident Rockstar

    garimajain85 it seems that you are trying to calculate data from multiple tables which have no relationship?
    And then when you select date filter, it breaks because it doesn't have relationship to one of tables.

    In Excel iferror would fix error, but when working with Dax logic, we need to make sure data model is clean and correct.

    So i would start from Calendar table (date slicer) and see to which table it is missing relationship and try to sort that out first.

    Cheers,
    Nemanja Andic

  • hI garimajain85 ,
    When you say breaks what exactly happens?

    It's hard to figure what could be wrong without seeing what  each measure does in response to a slicer selection.

    Can you please share a sanitized copy of your pbix?

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi,garimajain85.Hello, danextian, nandic.Thank you very much for your help with this issue.
    And now I want to share my advice with you.
    I am glad to help you.

    Regarding the issue you raised, my solution is as follows:
    1.First on your first question, the reason why metrics are affected when using a slicer is because the slicer changes the context, which may cause some metrics to return an error in that context, here's a link to that:

    Context in DAX Formulas - Microsoft Support

    2.First I have created the following table and the column names and data are the data you have given:


    3. Below are the measure I've created for your needs:

    Measure 2 = SWITCH(TRUE(),
    ISFILTERED('M'[manager]),IF(MONTH(SELECTEDVALUE(MON[1]))=MONTH(TODAY())-1,
    DIVIDE(CALCULATE(SUM('IFERROR'[Active count of employees]),FILTER(ALLSELECTED('IFERROR'),'IFERROR'[manager]=MAX('IFERROR'[manager]))),AVERAGE('IFERROR'[Active count of employees])),
    FALSE()),
    SELECTEDVALUE(MON[1])<=TODAY(),DIVIDE(CALCULATE(SUM('IFERROR'[SALE]),FILTER(ALLSELECTED('IFERROR'),MONTH('IFERROR'[month])=MONTH(SELECTEDVALUE(MON[1])))),
    CALCULATE(SUM('IFERROR'[Active count of employees]),FILTER(ALLSELECTED('IFERROR'),MONTH('IFERROR'[month])=MONTH(SELECTEDVALUE(MON[1]))))),
    SELECTEDVALUE(MON[1])>TODAY(),DIVIDE(SUM('IFERROR'[SALE]),
    CALCULATE(MAX('IFERROR'[Active count of employees]),FILTER(ALLSELECTED('IFERROR'),MONTH('IFERROR'[month])<MONTH(SELECTEDVALUE(MON[1]))))),
    FALSE())
    
    Measure = 
    VAR _a = SELECTEDVALUE('MON'[1])
    RETURN IF(_a =BLANK(),DIVIDE(SUM('IFERROR'[SALE]),SUM('IFERROR'[Active count of employees])), 
    [Measure 2])
    

    4.Here's my final result, which I hope meets your requirements.

    Can you share sample data and sample output in tabular format if I am misunderstanding? Or a sample pbix after removing sensitive data. We can better understand the problem and help you.
    Best Regards,
    Leroy Lu
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.