Forum Discussion

KristinaLeo's avatar
KristinaLeo
New Member
8 months ago
Solved

DAX Calculation

Hi Everyone, I require a measure that calculates for each customer tier the percentage of total revenue contributed by only products that were active for the entire duration of the current time peri...
  • Ahmed-Elfeel's avatar
    8 months ago

    Hi KristinaLeo,

    I hope you are doing well ☺️❤️

     

    Try this it should work:

    Tier Weighted Stable Product Contribution % =
    VAR SelectedPeriodDates = ALLSELECTED('Date'[Date])
    VAR PeriodStart = MINX(SelectedPeriodDates, [Date])
    VAR PeriodEnd = MAXX(SelectedPeriodDates, [Date])
    
    -- Products active the entire selected period
    VAR StableProducts =
        FILTER(
            ALL(Products),
            Products[LaunchDate] <= PeriodStart &&
            (ISBLANK(Products[DiscontinuedDate]) || Products[DiscontinuedDate] > PeriodEnd)
        )
    
    -- Convert to single column table of ProductIDs for TREATAS
    VAR StableProductIDs =
        SELECTCOLUMNS(StableProducts, "ProductID", Products[ProductID])
    
    -- Customers who signed up before period start
    VAR EstablishedCustomers =
        FILTER(
            ALL(Customers),
            Customers[SignupDate] < PeriodStart
        )
    
    -- Convert to single column table of CustomerIDs for TREATAS
    VAR EstablishedCustomerIDs =
        SELECTCOLUMNS(EstablishedCustomers, "CustomerID", Customers[CustomerID])
    
    -- Weighted revenue from transactions that match both lists
    VAR WeightedQualifiedRevenue =
        CALCULATE(
            SUMX(
                Sales,
                Sales[Revenue] *
                SWITCH(
                    RELATED(Customers[Tier]),
                    "A", 1.2,
                    "B", 1.0,
                    "C", 0.8,
                    1.0
                )
            ),
            -- Apply filters using TREATAS for reliable filtering
            TREATAS(StableProductIDs, Sales[ProductID]),
            TREATAS(EstablishedCustomerIDs, Sales[CustomerID])
        )
    
    -- Total revenue for the same date context (keep Date filters and remove other filters)
    VAR TotalPeriodRevenue =
        CALCULATE(
            SUM(Sales[Revenue]),
            ALL(Products),
            ALL(Customers),
            ALL(Sales[ProductID]),
            ALL(Sales[CustomerID])
            -- Date filters are preserved automatically
        )
    
    RETURN
        DIVIDE(
            WeightedQualifiedRevenue,
            TotalPeriodRevenue,
            BLANK()
        )

     

    if this post helps, then I would appreciate a thumbs up and mark it as the solution to help the other members find it more quickly.