Forum Discussion
DAX SOLUTION NEEDED
- 8 months ago
Hi Ahmed-Elfeel ,
I had to make a small tweak in your query to make it work. However, thank you for providing a solid base query for the result. Here is the final query:another_try_v2 =
VAR _max_date =
CALCULATE(
MAX(
'DateTable'[Date]
),
ALLSELECTED(
'DateTable'
)
)VAR _filterd_as_of_max =
FILTER(
--ALLSELECTED(
'Policy',
--),
'Policy'[AccountingDate] <= _max_date
)VAR _max_trx_per_policy =
SUMMARIZE(
_filterd_as_of_max,
'Policy'[PolicyNumber],
'Policy'[Term],
"Max_seq", MAX(
'Policy'[TransactionNum]
)
)VAR _valid_policies =
FILTER(
ADDCOLUMNS(
_max_trx_per_policy,
"TrxType",
VAR _PolicyNumber = [PolicyNumber]
VAR _term = [Term]
VAR _max_seq = [Max_seq]
RETURN
CALCULATE(
SELECTEDVALUE(
'Policy'[TransactionType]
),
_filterd_as_of_max,
'Policy'[PolicyNumber] = _PolicyNumber,
'Policy'[Term] = _term,
'Policy'[TransactionNum] = _max_seq
),
"ExpDate",
VAR _PolicyNumber_2 = [PolicyNumber]
VAR _term_2 = [Term]
VAR _max_seq_2 = [Max_seq]
RETURN
CALCULATE(
MAX(
'Policy'[PolicyExpDate]
),
_filterd_as_of_max,
'Policy'[PolicyNumber] = _PolicyNumber_2,
'Policy'[Term] = _term_2,
'Policy'[TransactionNum] = _max_seq_2
)
),
[TrxType] <> "CA" && [ExpDate] >= _max_date
)RETURN
SUMX(
_valid_policies,
VAR _p = [PolicyNumber]
VAR _t = [Term]
RETURN
CALCULATE(
SUM(
'Policy'[WrittenPremium]
),
_filterd_as_of_max,
'Policy'[PolicyNumber] = _p,
'Policy'[Term] = _t
)
)
Written Premium (As Of) :=
VAR MaxDate =
MAX ( 'Date'[Date] )
-- Step 1: Policies visible as of slicer date
VAR PoliciesAsOf =
FILTER (
ALL ( Policy ),
Policy[AccountingDate] <= MaxDate
)
-- Step 2: Latest transaction per Policy + Term
VAR LatestTxn =
ADDCOLUMNS (
SUMMARIZE (
PoliciesAsOf,
Policy[Policy],
Policy[Term]
),
"MaxTxn",
CALCULATE (
MAX ( Policy[Transaction] ),
PoliciesAsOf
)
)
-- Step 3: Retrieve row-level attributes of latest transaction
VAR LatestRows =
NATURALINNERJOIN (
LatestTxn,
SELECTCOLUMNS (
PoliciesAsOf,
"Policy", Policy[Policy],
"Term", Policy[Term],
"Transaction", Policy[Transaction],
"TransactionType", Policy[TransactionType],
"ExpDate", Policy[PolicyExpDate],
"WrittenPremium", Policy[WrittenPremium]
)
)
-- Step 4: Apply business rules
VAR ValidPolicies =
FILTER (
LatestRows,
[TransactionType] <> "CA"
&& [ExpDate] >= MaxDate
)
RETURN
SUMX ( ValidPolicies, [WrittenPremium] )