Forum Discussion
Perform a double aggregate with DAX
- 2 years ago
Hi Anonymous,
i have added a note and i added the pbix file.
- 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
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
- AndersDonker2 years ago
Helper I
Amazing! many thanks for your elaborate solution to the problem! I now understand why I was unable to produce the results myself. I need to increase my understanding of DAX to a more indepth level to grasp these type of solutions!
Thanks!
- AndersDonker2 years ago
Helper I
Hi ERD, now that i am trying to implement your solution regarding the customer amt into my existing model I seem to have an issue with the VALUES() part of the solution. I would like to (pre)filter the customers based on their date_since. Is there an option to extend the solution with this feature?
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] )On line 6 you use the VALUES ( Customer1[CustomerID] ) solution. Is it possible to extend this part of the code to filter the customers we process in the measure, based on date?
For example: if i choose the year 2021, I would like to see that the measure returns just 2 customers (A and B) and for both produce a 1 as a result in the category 1 to 3 products. I don't want to see C D E F G as customers with 0 products.I am thinking of something like:
FILTER(VALUES(Customer1[CustomerID]), with date between min and maxdate of the year) but that obiously does not work. 🙂