Pär Adeen on 07 Oct 2021 08:32:44
Using external tools like DAX Studio we are using EVALUATE to execute and DEFINE to define Variables, Columns, Measures, and Tables. When defining measures within Power BI, this is not possible.
With the possibility to utilize DEFINE MEASURE within our code, the code can be make much easier to read and to re-use.
As an example, the code from SQL BI below are using the measure [Sales Amount] multiple times. If we instead could re-write as the second attached query below, the code will be more portable and easier to read.
P.S. Adding MEASURE, we might also consider adding the COLUMN and TABLE statements, although I don't see the same need and I guess there's nothing to gain in this since we can store table functions in variables
Sales Amt =
VAR SalesOfAll =
CALCULATE (
[Sales Amount],
REMOVEFILTERS ( 'Product Names' )
)
RETURN
IF (
NOT ISINSCOPE ( 'Product Names'[Product Name] ),
-- Calculation for a group of products
SalesOfAll,
-- Calculation for one product name
VAR ProductsToRank = [TopN Value]
VAR SalesOfCurrentProduct = [Sales Amount]
VAR IsOtherSelected =
SELECTEDVALUE ( 'Product Names'[Product Name] ) = "Others"
RETURN
IF (
NOT IsOtherSelected,
-- Calculation for a regular product
SalesOfCurrentProduct,
-- Calculation for Others
VAR VisibleProducts =
CALCULATETABLE (
VALUES ( 'Product' ),
ALLSELECTED ( 'Product Names'[Product Name] )
)
VAR ProductsWithSales =
ADDCOLUMNS (
VisibleProducts,
"@SalesAmount", [Sales Amount]
)
VAR SalesOfTopProducts =
SUMX (
TOPN (
ProductsToRank,
ProductsWithSales,
[@SalesAmount]
),
[@SalesAmount]
)
VAR SalesOthers =
SalesOfAll - SalesOfTopProducts
RETURN
SalesOthers
)
)
---------------------------------------
Suggested possibility below.
Not that the code given below is written in a way that it's possible to execute using DAX Studio, i.e. using the DEFINE and EVALUATE statements. Using the Code in Power BI, These statements can be skipped and we can instead use the RETURN statement as we do for the VAR statement already
---------------------------------------
DEFINE
MEASURE Sales[@M] = [Sales Amount]
MEASURE Sales[Amt] =
VAR SalesOfAll =
CALCULATE (
[@M],
REMOVEFILTERS ( 'Product Names' )
)
RETURN
IF (
NOT ISINSCOPE ( 'Product Names'[Product Name] ),
-- Calculation for a group of products
SalesOfAll,
-- Calculation for one product name
VAR ProductsToRank = [TopN Value]
VAR SalesOfCurrentProduct = [@M]
VAR IsOtherSelected =
SELECTEDVALUE ( 'Product Names'[Product Name] ) = "Others"
RETURN
IF (
NOT IsOtherSelected,
-- Calculation for a regular product
SalesOfCurrentProduct,
-- Calculation for Others
VAR VisibleProducts =
CALCULATETABLE (
VALUES ( 'Product' ),
ALLSELECTED ( 'Product Names'[Product Name] )
)
VAR ProductsWithSales =
ADDCOLUMNS (
VisibleProducts,
"@SalesAmount", [@M]
)
VAR SalesOfTopProducts =
SUMX (
TOPN (
ProductsToRank,
ProductsWithSales,
[@SalesAmount]
),
[@SalesAmount]
)
VAR SalesOthers =
SalesOfAll - SalesOfTopProducts
RETURN
SalesOthers
)
)
EVALUATE {[Sales Amt]}
- Comments (1)
RE: Allowing DEFINE MEASURE in Power BI Measures
The formatting of above shows up as crap, looks like everything is truncated to one line.
I can see that others have better formatting and sometimes even pictures. What shall one do when submitting and Idea to get better formatting, and can some admin clean this up?