I have these two queries:
EVALUATE
FILTER (
SUMMARIZE (
'Sales',
Products[ProductName],
'Calendar'[CalendarYear],
"Total Sales Amount", SUM ( Sales[SalesAmount] ),
"Total Cost", SUM ( 'Sales'[TotalProductCost] )
),
Products[ProductName] = "AWC Logo Cap"
)
ORDER BY
Products[ProductName],
'Calendar'[CalendarYear] ASC
and this:
EVALUATE
SUMMARIZE (
FILTER ( 'Sales', RELATED ( Products[ProductName] ) = "AWC Logo Cap" ),
Products[ProductName],
'Calendar'[CalendarYear],
"Total Sales Amount", SUM ( Sales[SalesAmount] ),
"Total Cost", SUM ( 'Sales'[TotalProductCost] )
)
ORDER BY
Products[ProductName],
'Calendar'[CalendarYear] ASC
Both return the following:
The only difference between the two queries is the positioning of the FILTER function - which is better practice and why?
note
So looking at the two sqlbi articles referenced by Alex we can do either of the following to potentially make things more performant but I'm still unsure if the FILTER function should happen inside or outside the other syntax:
EVALUATE
FILTER (
ADDCOLUMNS (
SUMMARIZE ( 'Sales', Products[ProductName], 'Calendar'[CalendarYear] ),
"Total Sales Amount", CALCULATE ( SUM ( Sales[SalesAmount] ) ),
"Total Cost", CALCULATE ( SUM ( 'Sales'[TotalProductCost] ) )
),
Products[ProductName] = "AWC Logo Cap"
)
ORDER BY
Products[ProductName],
'Calendar'[CalendarYear] ASC
And using the 'SUMMARIZECOLUMNS' function:
EVALUATE
FILTER (
SUMMARIZECOLUMNS (
Products[ProductName],
'Calendar'[CalendarYear],
"Total Sales Amount", SUM ( Sales[SalesAmount] ),
"Total Cost", SUM ( 'Sales'[TotalProductCost] )
),
Products[ProductName] = "AWC Logo Cap"
)
ORDER BY
Products[ProductName],
'Calendar'[CalendarYear] ASC
note2
Looks like SUMMARIZECOLUMNS has a built in FILTER parameter so I'd guess that this is the best way to go to guard against performance issues:
EVALUATE
SUMMARIZECOLUMNS (
Products[ProductName],
'Calendar'[CalendarYear],
FILTER ( 'Products', Products[ProductName] = "AWC Logo Cap" ),
"Total Sales Amount", SUM ( Sales[SalesAmount] ),
"Total Cost", SUM ( 'Sales'[TotalProductCost] )
)
ORDER BY
Products[ProductName],
'Calendar'[CalendarYear] ASC