Forum Discussion
Measures SUMX and REMOVEFILTER
Hi,
I need to make to DAX measures that calculates eraned premium and remaning premium. The calculations are very simple, and I do manage to make the Earned premium behave as I want, but I cant get the remaning premium, which is just "premium" - Earned premium to behave as expected. This is my Earned premium measure:
Earned premium =
VAR SelectedDate = MAX(DateTable[Date]) // Get the selected date from DateTable
RETURN
CALCULATE(
SUMX(
sample_dataset,
VAR StartDate = sample_dataset[StartDate]
VAR Premium = sample_dataset[premium]
VAR DaysDiff = DATEDIFF(StartDate, SelectedDate, DAY)
VAR ValidDaysDiff = IF(DaysDiff < 0 || DaysDiff > 365, 0, DaysDiff) // Ensure it's between 0 and 365
RETURN
IF(NOT(ISBLANK(Premium)) && NOT(ISBLANK(StartDate)),
(ValidDaysDiff * (Premium / 365)),
0
)
),
REMOVEFILTERS(DateTable) // Remove filters only from DateTable
)
The dataset is on an agreement level. When I filter on a selected day I get to see how much premium the agreement has earned up until the selected day (within the 365 days condition).
When I try to make my Remaning premium I would think a masure like this would have the same logic:
Remaning premium =
VAR SelectedDate = MAX(DateTable[Date]) // Get the selected date from DateTable
RETURN
CALCULATE(
SUMX(
sample_dataset,
VAR StartDate = sample_dataset[StartDate]
VAR Premium = sample_dataset[premium]
// Calculate DaysDiff and ValidDaysDiff
VAR DaysDiff = DATEDIFF(StartDate, SelectedDate, DAY)
VAR ValidDaysDiff = IF(DaysDiff < 0 || DaysDiff > 365, 0, DaysDiff) // Ensure it's between 0 and 365
// Calculate OpptjentValue directly
VAR EarnedPremium =
IF(NOT(ISBLANK(Premium)) && NOT(ISBLANK(StartDate)),
(ValidDaysDiff * (Premium / 365)),
0
)
// Check if Premium is not blank and calculate Remaining premium accordingly
RETURN
IF(NOT(ISBLANK(Premium)),
Premium - EarnedPremium,
BLANK()
)
),
REMOVEFILTERS(DateTable) // Remove filters only from DateTable
)
This measure aggregates total premium on each row (with a datapoint), and where Earned premium summarizes on a daily basis the remaning premium subtracts the Earned premium from the total of all premiums. It ignores the ValidDayDiff it seems like, and it should not aggregate the total of the premium.
I will attach one screenshot of an example of the calculation in excel and another of how it looks in the PBI report.
I refuse to belive that this is not possible to do in DAX.
Appreciate any help 🙂 Thanks!
Lotte
Hi, thank you for responding. This is exactly the same measure I wrote. But I did find a workaround with deleting the relationship between the tabels, as the removefilter didnt do what I intended it to 🙂
3 Replies
- Kedar_Pande
Super User
LottePBI You can try:
Remaining Premium =
VAR SelectedDate = MAX(DateTable[Date])
RETURN
CALCULATE(
SUMX(
sample_dataset,
VAR StartDate = sample_dataset[StartDate]
VAR Premium = sample_dataset[premium]
VAR DaysDiff = DATEDIFF(StartDate, SelectedDate, DAY)
VAR ValidDaysDiff = IF(DaysDiff < 0 || DaysDiff > 365, 0, DaysDiff)
VAR EarnedPremium =
IF(
NOT(ISBLANK(Premium)) && NOT(ISBLANK(StartDate)),
ValidDaysDiff * (Premium / 365),
0
)
RETURN
IF(
NOT(ISBLANK(Premium)),
Premium - EarnedPremium,
BLANK()
)
),
REMOVEFILTERS(DateTable)
)If this helped, a Kudos 👍 or Solution mark would be great! 🎉
Cheers,
Kedar
Connect on LinkedIn - AnonymousNot applicable
Hi LottePBI
Has your problem been resolved? If so, could you mark the corresponding reply as the solution so that others with similar issues can benefit from it?
Best Regards,Jayleny
- LottePBIRegular Visitor
Hi, thank you for responding. This is exactly the same measure I wrote. But I did find a workaround with deleting the relationship between the tabels, as the removefilter didnt do what I intended it to 🙂