Forum Discussion
Issue with Calculated table with User's Selection
You can get the functionality you want by using a calculation group.
First, create a table of all the financial years for use in the slicer,
Years = DISTINCT( Sheet1[Financial Year] )
and add this column to the slicer.
Create measures which sum the columns you are interested in, e.g.
Net Sales Measure = SUM( Sheet1[Net Sales] )
Next create a calculation group and a calculation item like
By Net Sales =
VAR SelectedYears = VALUES( 'Years'[Financial Year] )
VAR NumSelectedYears = COUNTROWS( SelectedYears )
VAR SummaryTable = ADDCOLUMNS(
CALCULATETABLE(
SUMMARIZE( Sheet1, Sheet1[Financial Year], Sheet1[VAT Number] ),
TREATAS( SelectedYears, Sheet1[Financial Year] ),
REMOVEFILTERS()
),
"@value", [Net Sales Measure]
)
VAR FilteredTable = GROUPBY(
FILTER(
SummaryTable,
[@value] <> 0
),
Sheet1[VAT Number],
"@num", SUMX( CURRENTGROUP(), 1)
)
VAR ValidNums = SELECTCOLUMNS(
FILTER(
FilteredTable,
[@num] = NumSelectedYears
),
Sheet1[VAT Number]
)
VAR Result = CALCULATE(
SELECTEDMEASURE(),
KEEPFILTERS( ValidNums )
)
RETURN Result
This code calculates the number of years chosen in the slicer, works out the net sales for each VAT number in each of those years, filters out those years where the net sales is 0 or blank and then counts the number of years for each VAT number, retaining those where the number of valid years matches the number of years selected in the slicer.
If you want to allow the user to choose a measure different from net sales for doing the comparison, create another calculation item and replace the [Net Sales Measure] with the appropriate measure.