because of confidential nature of data, I'll try to describe what I'm struggling with using some random examples. Let's say I have a fact table with invoices data in Power BI. I need to count number of distinct product ID's with sales over let's say €50k in last 12 months or to be more precise in 12 months prior selected date. At the same time I need to be able to narrow down results to selected Country, Product group and Product category.
I've started with setting the dates range for DATESBETWEEN like this:
productsCount =
VAR lastDay = IF(MAX('Calendar table'[Date]) > NOW(); NOW(); MAX('Calendar table'[Date]))
VAR firstDay = EDATE(lastDay; -12)
RETURN
But then I got lost:
CALCULATE(
COUNTROWS('Sales');
SUMMARIZE(
'Sales';
'Sales'[ProductID];
"prodSales"; SUM('Sales'[EUR])
);
DATESBETWEEN('Sales'[Date]; firstDay; lastDay);
ALLEXCEPT(
'Sales';
'Sales'[Product group];
'Sales'[Product category];
'Sales'[Country]
);
[prodSales] > 50000
)
The thing is that I need to be able to filter summarized data by sum of sales before I'll count rows.
I haven't tested this but I think something like this might work where you filter after summarizing:
productsCount =
VAR lastDay =
IF (
MAX ( 'Calendar table'[Date] ) > NOW ();
NOW ();
MAX ( 'Calendar table'[Date] )
)
VAR firstDay = EDATE ( lastDay; -12 )
RETURN
COUNTROWS (
FILTER (
CALCULATETABLE (
SUMMARIZE ( 'Sales';
'Sales'[ProductID];
"prodSales"; SUM ( 'Sales'[EUR] )
);
DATESBETWEEN ( 'Sales'[Date]; firstDay; lastDay );
ALLEXCEPT (
'Sales';
'Sales'[Product group];
'Sales'[Product category];
'Sales'[Country]
)
);
[prodSales] > 50000
)
)