Forum Discussion
Compare treatments exclusively within the same fields to evaluate yield differences
- 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:- 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.
- 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
Hi dear Martin_D,
Thank you once again for your valuable feedback.
I've carefully reviewed your insights, and while I believe I grasp the concept, I'm encountering some difficulty aligning the numbers on both graphs.
To provide you with a clearer picture, I've attached screenshots with annotations within the same Excel file (tab 20240306).
I hope this visual representation better conveys my current situation.
Appreciate your continued assistance.
Kind regards,
Pablo
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:
- 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.
- 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