Welcome
back to this week’s edition of the Power BI blog series. This week, we look at a new DAX function
available in Power BI: MATCHBY.
There is a new DAX function in town: MATCHBY.
When used within any window function,
this function defines the columns that are used to determine how to match data
and identify the current row. (A window
function performs a calculation across a set of table rows that are somehow
related to the current row. This is
comparable to the type of calculation that can be done with an aggregate
function.)
For example, below is a query that
returns FactInternetSales with an added column, which indicates, for
each sale, the previous sales amount in descending order of sales from the same
product. Using MATCHBY, we’re
able to indicate that current sales should be identified by the SalesOrderNumber and SalesOrderLineNumber. Without MATCHBY, the query would return an error since there are no key columns
in FactInternetSales table.
EVALUATE
ADDCOLUMNS (
FactInternetSales,
“Previous Sales Amount”,
SELECTCOLUMNS (
OFFSET (
-1,
FactInternetSales,
ORDERBY ( FactInternetSales[SalesAmount], DESC ),
PARTITIONBY ( FactInternetSales[ProductKey] ),
MATCHBY( FactInternetSales[SalesOrderNumber],
FactInternetSales[SalesOrderLineNumber] )
),
FactInternetSales[SalesAmount]
)
)
Be the first to comment