Forum Discussion

AndersDonker's avatar
AndersDonker
Icon for Helper I rankHelper I
2 years ago
Solved

Perform a double aggregate with DAX

Dear forum,   i'm struggling with a solution which I expect to be easy but can't seem to wrap my head around within DAX.   My desired output is to have a dynamic calculation that can be filtered ...
  • AndersDonker's avatar
    AndersDonker
    2 years ago

    Hi Anonymous,

     

    i have added a note and i added the pbix file. 

  • ERD's avatar
    2 years ago

    Hi AndersDonker ,

    The answer to your first question:

    1. You need a new table with categories and a Date table:

    2. Measures:

    products amt = DISTINCTCOUNT(Product2[ProductID])
    customers amt = 
    VAR c_category = SELECTEDVALUE ( Categories[Amt category] )
    VAR t =
        FILTER (
            ADDCOLUMNS (
                VALUES ( Customer1[CustomerID] ),
                "@products", [products amt],
                "@category",
                    VAR amt = [products amt]
                    RETURN
                        CALCULATE (
                            MAX ( Categories[Amt category] ),
                            amt <= Categories[MaxValue],
                            amt >= Categories[MinValue]
                        )
            ),
            [@category] = c_category
        )
    RETURN
        COUNTAX ( t, [CustomerID] )

    The answer for your second question:

    1. You need a new table with possible years:

    2. Measure:

    products taken = 
    VAR customers = SUMMARIZE ( Customer1, Customer1[CustomerID], Customer1[Date_Since] )
    VAR customers_with_dt_from =
        SELECTCOLUMNS (
            GENERATE (
                customers,
                VAR dt = [Date_Since]
                VAR m = MONTH ( dt )
                VAR d = DAY ( dt )
                RETURN
                    FILTER (
                        GENERATESERIES ( dt, DATE ( YEAR ( TODAY () ), m, d ), 1 ),
                        DAY ( [Value] ) = d && MONTH ( [Value] ) = m
                    )
            ),
            "CustomerID", [CustomerID],
            "dt_from", [Value]
        )
    VAR customers_with_date_to =
        ADDCOLUMNS (
            customers_with_dt_from,
            "dt_to",
                VAR dt = [dt_from]
                VAR dates = DATESINPERIOD ( 'Date'[Date], dt, 12, MONTH )
                RETURN
                    MAXX ( dates, [Date] )
        )
    VAR customers_with_products =
        ADDCOLUMNS (
            customers_with_date_to,
            "years_since",
                VAR _customer = [CustomerID]
                VAR min_dt = MINX ( FILTER ( customers_with_date_to, [CustomerID] = _customer ), [dt_from] )
                VAR max_dt = [dt_to]
                RETURN
                    DATEDIFF ( min_dt, max_dt, YEAR ),
            "products",
                VAR c_dt_from = [dt_from]
                VAR c_dt_to = [dt_to]
                RETURN
                    CALCULATE (
                        [products amt],
                        Product2[Product_Date] >= c_dt_from,
                        Product2[Product_Date] <= c_dt_to
                    )
        )
    VAR _years = SELECTEDVALUE( Years[Years] )
    VAR result = SUMX( FILTER( customers_with_products, [years_since] = _years), [products])
    RETURN
        result