Stack Overflow Asked on February 6, 2021
Following are 2 measures:
SUMX ( ALL ( SALES ) , SALES[AMT] )
CALCULATE ( SUMX ( SALES, SALES[AMT] ), ALL (SALES) )
Similarly for the following 2 measures:
SUMX ( FILTER ( SALES, SALES[QTY]>1 ), SALES[QTY] * SALES[AMT] )
CALCULATE ( SUMX ( SALES, SALES[QTY] * SALES[AMT] ), FILTER ( SALES, SALES[QTY]>1 ) )
Both above examples clear the natural filters on the SALES table and perform the aggregation.
I’m trying to understand if I have understood this concept right and what is the significance/usecase of using either approach.
The SUM()
is an aggregator function. It adds up all the values in a single column you specify after applying all filters that will impact the formula. SUM()
has no knowledge of the existence of rows (it can’t do row by row evaluation) – all it can do is add everything in the single-column it is presented with after the filters have been applied. SUMX()
is an iterator function. It works through a table, row by row to complete the evaluation after applying all filters. SUMX()
has awareness of rows in a table, and hence can reference the intersection of each row with any columns in the table. SUMX()
can operate over a single column but can also operate over multiple columns too – because it has the ability to work row by row.
The SUM()
operates over a single column and has no awareness of individual rows in the column (no row by row evaluation). The SUMX()
can operate on multiple columns in a table and can complete row by row evaluation in those columns. The following formula, based on the DAX sample workbook, shows one example of how you can create this calculation by using a filter
:
=SUMX(
FILTER ('ResellerSales_USD', 'ResellerSales_USD'[Quantity] > 5 &&
'ResellerSales_USD'[ProductStandardCost_USD] > 100),
'ResellerSales_USD'[SalesAmt]
)
SUMX
calculates a sum over a table. The second part of the formula, FILTER(table, expression)
,tells SUMX which data to use. SUMX
requires a table or an expression that results in a table. Here, instead of using all the data in a table, you use the FILTER
function to specify which of the rows from the table are used.
When reading a CALCULATE formula, you always start with the second parameter – in this case the FILTER
function. Filter
is an iterator, and in this case it is iterating over the CALENDAR
table AFTER any existing filters on the Calendar table are first removed.
FILTER
works through each row in the Calendar table and checks to see if the date of each row in the Calendar table is greater than the maximum Date Received (in the current filter context) and also if that date is less than the maximum Date Approved (also in the current filter context). The FILTER
function keeps the rows in the Calendar table that pass this test, and then CALCULATE
applies this FILTER
prior to adding up the weekdays. Actually, This FILTER
function is very efficient – a better solution is provided below.
Work Days SUMX=
SUMX(Data,
CALCULATE(
CALCULATE(
SUM('Calendar'[Is Weekday]),
FILTER(
ALL('Calendar'),
'Calendar'[Date] >= MAX(Data[Date Received])
&& 'Calendar'[Date] <= MAX(Data[Date Approved])
)
)
)
)
Please go throw this official documentation you will find the ALL Function of DAX and their difference with examples.
Answered by Raksha Saini on February 6, 2021
In DAX you can achieve the same results from different DAX queries/syntax. So based on my understanding both the DAX provide the same result :
SUMX ( ALL ( SALES ) , SALES[AMT] )
CALCULATE ( SUMX ( SALES, SALES[AMT] ), ALL (SALES) )
And the 1st one is a more concise way to achieve way rather than the 2nd one in all cases/scenarios. Currently when I tested these out with <100 records in a table ; the performance was the same for both the measures.
But ideally the 1st scenario would be quicker then the 2nd one which we can test out by >1 million record through DAX studio.
Can you please share your thoughts on the same?
Answered by Nandan on February 6, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP