Forum Discussion
calculation item from calculation group not working correctly
hello
i have a problem with the correct dax formula for the calculation item that i need;
i have a source tableA; this table contains records of actuals and budget (RecordType); i have 6 other columns in that table: date, budgetCode, BudgetModel,PeriodType, Measure1, Measure2;
records with recordType=Actuals have hard coded values for budgetCode and BudgetModel (-2); Records with recordType=Budget have hard coded values for PeriodType (-2); BudgetModel should be hardcoded in the calculation item to 'Final'
in the model i created a measure Measure1/Measure2;
I created a calculation item with the below logic:
Forecast=
VAR SelectedBC = SELECTEDVALUE('Budget Code'[Budget Code])
VAR R39 = CALCULATE(
SELECTEDMEASURE ()
, 'Date'[Month nr] IN {1,2,3}
, 'TableA'[Record Type] ="Actuals"
, 'Period Type'[Period Type] ) <> "Closing"
, REMOVEFILTERS ( 'Budget Code' )
, REMOVEFILTERS ( 'Budget Model' )
)
VAR R66 = CALCULATE(
SELECTEDMEASURE ()
, 'Date'[Month nr] IN {1,2,3,4,5,6}
, 'Table'[Record Type] ="Actuals"
, 'Period Type'[Period Type] ) <> "Closing"
, REMOVEFILTERS ( 'Budget Code' )
, REMOVEFILTERS ( 'Budget Model' )
)
VAR R93 = CALCULATE(
SELECTEDMEASURE (),
'Date'[Month nr] IN {1,2,3,4,5,6,7,8,9},
'TableA'[Record Type] ="Actuals"
, 'Period Type'[Period Type] ) <> "Closing"
, REMOVEFILTERS ( 'Budget Code' )
, REMOVEFILTERS ( 'Budget Model' )
)
VAR SelectedActual =
SWITCH(
SelectedBC,
"Reforecast 3+9", R39,
"Reforecast 6+6", R66,
"Reforecast 9+3", R93,
0
)
VAR SelectedBudget = CALCULATE(
SELECTEDMEASURE ()
,'Budget Code'[Budget Code] = SelectedBC
, REMOVEFILTERS ( 'Budget Model' )
,'Budget Model'[Budget Model Code] = "Final"
, REMOVEFILTERS('Fiscal Period Type')
,'TableA'[Record Type] ="Budget"
)
VAR Forecast = SelectedActual+SelectedBudget
Return Forecast
this give the correct result if i just drag Measure1 and Measure2 into the visual; but it dont work correctly for Measure1/Measure2;
Instead of (Actuals measure1 + budget measure1)/(actuals measure 2 + budget measure2) it gives me actuals measure1/measure2 + budget measure1/measure2
i understand the issue is in the calculate being used separately for two different data sets;
so i was trying to combine the records under 1 data set with combined filters for both actuals and budget;
however im unsuccessful in getting the right code
i tried many different versions of the code, here is one of the last trials:
version0
VAR SelectedBC = SELECTEDVALUE('Budget Code'[Budget Code])
VAR NMonths =
SWITCH(
SelectedBC,
"3+9", 3,
"6+6", 6,
"9+3", 9,
0
)
VAR MonthsForActuals = GENERATESERIES(1, NMonths, 1)
VAR Forecast =
CALCULATE (
SELECTEDMEASURE(),
// Remove external page/slicer filters that were killing your rows
REMOVEFILTERS ( 'Budget Code' ),
REMOVEFILTERS ( 'Budget Model' ),
REMOVEFILTERS ( 'Period Type' ),
// Apply your OR logic as a single table filter with row context
KEEPFILTERS (
FILTER (
'TableA',
(
'TableA'[Record Type] = "Actuals"
&& RELATED ( 'Date'[Month nr] ) IN SELECTCOLUMNS ( MonthsForActuals, "Value", [Value] )
&& RELATED ( 'Period Type'[Period Type] ) <> "Closing"
&& 'TableA'[BudgetCode] = -2
&& 'TableA'[BudgetModel] = -2
)
||
(
'TableB'[Record Type] = "Budget"
&& RELATED ( 'Budget Code'[Budget Code] ) = SelectedBC
&& RELATED ( 'Budget Model'[Budget Model Code] ) = "Final"
&& 'TableA'[PeriodType] = -2
)
)
)
)
RETURN
Forecast
another trial was this:
version1
VAR SelectedBC = SELECTEDVALUE('Budget Code'[Budget Code])
VAR NMonths =
SWITCH(
SelectedBC,
"Reforecast 3+9", 3,
"Reforecast 6+6", 6,
"Reforecast 9+3", 9,
0
)
VAR MonthsForActuals = GENERATESERIES(1, NMonths, 1)
var CombinedFilterTable =
FILTER(
'TableA',
(
'TableA'[Record Type] = "Actuals"
&& RELATED('Date'[Month nr]) IN SELECTCOLUMNS(MonthsForActuals, "Value", [Value])
&& RELATED('Period Type'[Period Type]) <> "Closing"
)
||
(
'TableA'[Record Type] = "Budget"
&& RELATED('Budget Code'[Budget Code]) = SelectedBC
&& RELATED('Budget Model'[Budget Model Code]) = "Final"
)
)
var Forecast = CALCULATE(
SELECTEDMEASURE(),
REMOVEFILTERS('Budget Code'),
REMOVEFILTERS('Budget Model'),
REMOVEFILTERS('Period Type'),
KEEPFILTERS(CombinedFilterTable)
)
Return Forecast
but none of these versions are working - in both versions 0 and 1 actuals part are missing from the final result; in the report itself i need to be able to add the filter on budget code (this is determining how many months of actuals the measure will cover) and any filter on budget model or period type should be ignored by the measure (these are hard coded in the measures too); any other filters like e.g. on date should be applicable though (e.g. on year)
i would appreciate any tips on how to get this done;
thanks!
rgds
hello, thank you for your answer; what do you mean with the 'never apply calc items directly to ratio logic'? it seems to me that your forecast measures is exactly as my first attept of the calculation item ; i already have base measures measure1 and measure2; where exactly do i create now the ratio then measure1/measure2? if i create a calculation item with your definition (which i think is ~ my original definition: 2 separate calculates for selectedmeasurs) then if i create a ratio measure measure1/measure2 and drag this to visual under this calculation item it give the issue i described in my post: instead of doing measure1 actual + budget / measrue 2 actual + budget, it does actual measure1/measure2 + budget measure1/measure2; i was playing around with this a bit and i came to conclution the reason for this is because the ratio is being passed on into the calculation group; so the only way to work around this is to have 1 calculate selectedmeasure in my calculation item; after 1 milion trials i came up with this code:
VAR SelectedBC = SELECTEDVALUE('Budget Code'[Budget Code])
VAR NMonths =
SWITCH(
SelectedBC,
"Reforecast 3+9", 3,
"Reforecast 6+6", 6,
"Reforecast 9+3", 9,
0
)
VAR MonthsForActuals = GENERATESERIES(1, NMonths, 1)
VAR CombinedFilterTable =
CALCULATETABLE (
FILTER (
'TableA',
(
'TableA'[Record Type] = "Actuals"
&& RELATED ( 'Date'[Month nr] ) IN SELECTCOLUMNS ( MonthsForActuals, "Value", [Value] )
&& RELATED ( 'Period Type'[Period Type] ) <> "Closing"
)
||
(
'TableA'[Record Type] = "Budget"
&& RELATED ( 'Budget Code'[Budget Code] ) = SelectedBC
&& RELATED ( 'Budget Model'[Budget Model Code] ) = "Final"
)
),
REMOVEFILTERS ( 'Budget Code' ),
REMOVEFILTERS ( 'Budget Model' ),
REMOVEFILTERS ( 'Period Type' )
)
VAR Forecast =
CALCULATE (
SELECTEDMEASURE(),
KEEPFILTERS ( CombinedFilterTable ),
REMOVEFILTERS ( 'Budget Code' ),
REMOVEFILTERS ( 'Budget Model' ),
REMOVEFILTERS ( 'Period Type' )
)
Return Forecast
this seem to be working correctly both on absolute values (i.e. measure 1 forecast and measure2 forecast) as well as on the ratio (measure1 forecast/measure2 forecast);
5 Replies
- Kedar_PandeSuper User
Num Calc Item:
Forecast Num =
VAR SelectedBC = SELECTEDVALUE('Budget Code'[Budget Code])
VAR NMonths = SWITCH(SelectedBC, "Reforecast 3+9", 3, "ReforecastIf this answer helped, please click 👍 or Accept as Solution.
-Kedar
LinkedIn: https://www.linkedin.com/in/kedar-pande - v-dineshyaCommunity Support
Hi MamaHani2 ,
Thank you for reaching out to the Microsoft Community Forum.
Please try below steps.
1. Create base measures.
[Measure1 Base] = SUM ( 'TableA'[Measure1] )
[Measure2 Base] = SUM ( 'TableA'[Measure2] )
2. Create the calculation item to work on additive measures only.Forecast =
VAR SelectedBC = SELECTEDVALUE ( 'Budget Code'[Budget Code] )VAR NMonths =
SWITCH (
SelectedBC,
"Reforecast 3+9", 3,
"Reforecast 6+6", 6,
"Reforecast 9+3", 9
)VAR ActualsPart =
CALCULATE (
SELECTEDMEASURE (),
'TableA'[Record Type] = "Actuals",
'Date'[Month nr] <= NMonths,
'Period Type'[Period Type] <> "Closing",
REMOVEFILTERS ( 'Budget Code' ),
REMOVEFILTERS ( 'Budget Model' )
)VAR BudgetPart =
CALCULATE (
SELECTEDMEASURE (),
'TableA'[Record Type] = "Budget",
'Budget Model'[Budget Model Code] = "Final",
'Budget Code'[Budget Code] = SelectedBC,
REMOVEFILTERS ( 'Period Type' )
)RETURN
ActualsPart + BudgetPart
3. Create the ratio outside the calculation group.[Forecast Measure1 / Measure2] =
DIVIDE (
[Measure1 Base],
[Measure2 Base]
)Note: Never apply calc items directly to ratio logic.
I hope this information helps. Please do let us know if you have any further queries.
Regards,
Dinesh
- MamaHani2Frequent Visitor
hello, thank you for your answer; what do you mean with the 'never apply calc items directly to ratio logic'? it seems to me that your forecast measures is exactly as my first attept of the calculation item ; i already have base measures measure1 and measure2; where exactly do i create now the ratio then measure1/measure2? if i create a calculation item with your definition (which i think is ~ my original definition: 2 separate calculates for selectedmeasurs) then if i create a ratio measure measure1/measure2 and drag this to visual under this calculation item it give the issue i described in my post: instead of doing measure1 actual + budget / measrue 2 actual + budget, it does actual measure1/measure2 + budget measure1/measure2; i was playing around with this a bit and i came to conclution the reason for this is because the ratio is being passed on into the calculation group; so the only way to work around this is to have 1 calculate selectedmeasure in my calculation item; after 1 milion trials i came up with this code:
VAR SelectedBC = SELECTEDVALUE('Budget Code'[Budget Code])
VAR NMonths =
SWITCH(
SelectedBC,
"Reforecast 3+9", 3,
"Reforecast 6+6", 6,
"Reforecast 9+3", 9,
0
)
VAR MonthsForActuals = GENERATESERIES(1, NMonths, 1)
VAR CombinedFilterTable =
CALCULATETABLE (
FILTER (
'TableA',
(
'TableA'[Record Type] = "Actuals"
&& RELATED ( 'Date'[Month nr] ) IN SELECTCOLUMNS ( MonthsForActuals, "Value", [Value] )
&& RELATED ( 'Period Type'[Period Type] ) <> "Closing"
)
||
(
'TableA'[Record Type] = "Budget"
&& RELATED ( 'Budget Code'[Budget Code] ) = SelectedBC
&& RELATED ( 'Budget Model'[Budget Model Code] ) = "Final"
)
),
REMOVEFILTERS ( 'Budget Code' ),
REMOVEFILTERS ( 'Budget Model' ),
REMOVEFILTERS ( 'Period Type' )
)
VAR Forecast =
CALCULATE (
SELECTEDMEASURE(),
KEEPFILTERS ( CombinedFilterTable ),
REMOVEFILTERS ( 'Budget Code' ),
REMOVEFILTERS ( 'Budget Model' ),
REMOVEFILTERS ( 'Period Type' )
)
Return Forecast
this seem to be working correctly both on absolute values (i.e. measure 1 forecast and measure2 forecast) as well as on the ratio (measure1 forecast/measure2 forecast);- v-dineshyaCommunity Support
Hi MamaHani2 ,
Thanks for the update. We are happy to hear that you have resolved the issue. Thanks for sharing the details here.
"Never apply calc items directly to ratio logic" means you can’t put a ratio measure in a visual with a calculation group. A calculation item that uses multiple CALCULATE(SELECTEDMEASURE()) calls will ALWAYS break ratios. calculation group wraps the entire expression of the measure.
If the measure is:
[Ratio] = [Measure1] / [Measure2]
then inside the calc item, SELECTEDMEASURE() is:([Measure1] / [Measure2])
So below code:
CALCULATE( SELECTEDMEASURE(), FilterA )
+
CALCULATE( SELECTEDMEASURE(), FilterB )becomes:
([M1A]/[M2A]) + ([M1B]/[M2B])
It's mathematically wrong for forecasting.
Note: Your new code have only ONE CALCULATE(SELECTEDMEASURE()), And Actuals + Budget are combined at the row set level. And Ratio is evaluated after aggregation. That's the reason you got the expected result.
Regards,
Dinesh