Forum Discussion

bijaymaharjan's avatar
8 months ago
Solved

DAX SOLUTION NEEDED

Hi all,  Below is my requirement and I keep getting incorrect results. Hoping someone can help me out.  I have 2 tables, Policy table has PolicyNumber, Term, TransactionNum, TransactionType, Policy...
  • bijaymaharjan's avatar
    bijaymaharjan
    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
    )
    )