Forum Discussion

pmunoz's avatar
pmunoz
Frequent Visitor
2 years ago
Solved

Compare treatments exclusively within the same fields to evaluate yield differences

Creating Comparative Graphs for Treatment Efficacy.   Good morning,   I hope this message finds you well. This is my first time reaching out here, so please forgive any missteps on my part.   I...
  • Martin_D's avatar
    Martin_D
    2 years ago

    Hi pmunoz ,


    The reason why you are seeing this result is because neither the measure nor the sumarizing visual provides the trial context, whereas in the other chart the visual provides the trial context on the x-axis. In this case, the the summarzing visual includes per treatment all yield values per treatment if there is at least any yield value in any treatment included in your filters. This includes also yield values from trials that have a yield value in only one of the treatment - simply because the visual doesn't know which yield value comes from which trial.
    There are two ways to solve this:

    1.  Add the trial context to the visual: Very easy, use stacked column chart, use treatment as the x-axis and trial as the legend, and turn on total labels as described earlier.
    2. Add the trial context to the measure.

    There is no right or wrong approach, it depends on your requirements. Use the visual approach if you want to keep the measure universal and potentially use it in other visuals where you want to see the split by something else than trials. Use the measure approch if you want to make sure that the evaluation "yield value for every treatment" shall always be done by trial, even if there is an aggregtion by another category in a visual.
    For the measure solution, this is the code:

     

    Total Yield, Combined Treatments Only, per Trial = 
    
    VAR _CalculationPerTrial =
        ADDCOLUMNS (
            SUMMARIZE ( 
                'data',
                'data'[TRIAL]
            ),
            "@Yield",
    
            // get list of all selected treatments
            VAR _SelectedTreatments = ALLSELECTED ( 'Treatment'[TREATMENT] )
    
            // check for each selected treatment whether there is data in the current context, e.g., a trial
            VAR _TreatmentsAndValues = 
                CALCULATETABLE (
                    ADDCOLUMNS (
                        _SelectedTreatments,
                        "@Value",
                        CALCULATE (
                            [Total Yield]
                        )
                    ),
                    ALLSELECTED ( 'Treatment'[TREATMENT] )
                )
    
            // count number of treatments w/ value
            VAR _NumberOfAppliedTreatments = COUNTX ( _TreatmentsAndValues, [@Value] )
    
            // count number of selected treatments, with or without value
            VAR _NumberOfSelectedTreatments = COUNTROWS ( _SelectedTreatments )
    
            RETURN
    
            IF (
                _NumberOfAppliedTreatments = _NumberOfSelectedTreatments, // only if there are values for all selected treatments in the current context, e.g., a trial,
                [Total Yield]                                             // then show the value.
            )                                                             // otherwise not.
        )
    
    RETURN
    
    SUMX ( _CalculationPerTrial, [@Yield] )

     

    Kind regards,
    Martin

  • pmunoz's avatar
    pmunoz
    2 years ago

    Million thanks, Martin_D! It worked perfectly!