An in-depth explanation of DAX: purchase recommendations and product ABC classification analysis

An in-depth explanation of DAX: purchase recommendations and product ABC classification analysis

Three steps for DAX operation evaluation. First the filter is detected, then the filter function is applied to the underlying table, and finally the result is calculated. Filter functions in DAX are complex and powerful functions. For example, filter functions can be used to manipulate data context to create dynamic calculations.

01,Usage details

In DAX, NOT is used for the negation operation, and IN is used for the OR operation, but it cannot be directly adjacent to NOT IN like in SQL. The following is an application example of using NOT before the IN statement in DAX. The expression is as follows:

M.其他收货人:=
CALCULATE (
    SUM ( '运单'[数量] ),
    NOT (
        '收货'[收货人] IN VALUES ( '收货'[收货人])
   )
)

 The returned value is shown in Figure 1.

■ Figure 1 Combination application of NOT and IN

IN is followed by two or more columns that are allowed to be connected. The list is constructed with {}, and the data of each row is constructed with brackets (). The brackets () represent the tuple, and the tuple contains the data of each row. Data, the data in the tuple is separated by commas. Application examples:

('装货'[包装方式], '装货'[产品]) IN {("桶装","尿素"),("箱装","蛋糕纸")}

The above expressions can also be placed in the CALCULATE() function to participate in filtering applications.

When using iterative functions in DAX, you must pay attention to whether the table of the first parameter comes from one end or multiple ends of the data model to avoid total value errors caused by iterative aggregate functions ignoring row context. Application example, create measurement values ​​M.Total cost 1 and M.Total cost 2, the expressions are as follows:

M.总成本1:= SUMX(
    '合同',
SUMX(
        '运单',
        '运单'[成本]*'运单'[数量]
        )
    )//该表达式返回的总计值有误

M.总成本2 := SUMX(
    '合同',
CALCULATE(
SUMX(
            '运单',
            '运单'[数量]*'运单'[成本])
        )
    )

 Create a pivot table, drag the products of the contract table into the row labels, and check the measurement values ​​M.Total cost 1 and M.Total cost 2. The returned values ​​are as shown in Figure 2.

■ Figure 2 Using aggregate functions on one end of the model

02,Purchase and recommendation analysis

The first parameter of the CALCULATE() function returns a value, which is often used in aggregate functions such as COUNTROWS() and SUM(); the first parameter will be calculated based on the context modified by each subsequent filter parameter. The second parameter can be a table. The expression is as follows:

M.共有产品 :=
CALCULATE (
   SUM('订单'[数量]),
    INTERSECT (
        VALUES ( '订单'[产品] ),
       VALUES ( '运单'[产品] )
    )
)

 Except for the first parameter, all other parameters are tables. Application example, the expression is as follows:

M.共有产品B :=
CALCULATE (
   SUM('运单'[数量]),
    INTERSECT (
       VALUES ( '订单'[产品] ),
        VALUES ( '运单'[产品] )
    ),
    INTERSECT (
      VALUES ( '装货'[包装方式] ),
      VALUES ( '运单'[包装方式] )
    )
)

There are a total of 11 products in the order table (the quantity is 205); there are a total of 10 products in the waybill table (the missing product is packaging rope, the quantity is 2). The returned value is shown in Figure 3.

 In the second parameter of CALCULATE(), use the FILTER() parameter part to expand or reduce the scope of the filter context. The expression is as follows:

M.度量ZF:=
CALCULATE (
    DISTINCTCOUNT ( '运单'[运单编号] ),
    FILTER (
        ADDCOLUMNS (
VALUES ( '运单'[运单编号] ),
"行数", COUNTROWS ( '运单' )
),
        [行数] > 0
    )
)

 The returned value is shown in Figure 4.

■ Figure 4 FILTER() parameters in CALCULATE()

03,Product ABC classification analysis

In DAX, ABC classification analysis is generally completed in three steps. Step 1: Use calculated columns or measurement values ​​to calculate the cumulative value; Step 2: Complete the cumulative proportion based on the cumulative value; Step 3: Classify the proportions into ABC categories. Create measurement values ​​M. Waybill volume, M. Cumulative waybill volume, M. Cumulative proportion, and M.ABC classification. The expression is as follows:

M.运单量:=SUM('运单'[数量])

M.累计运单量 :=
VAR A = [M.运单量]
VAR B =
    ADDCOLUMNS ( ALL ( '运单'[产品] ), "运量", [M.运单量] )
RETURN
    IF (
        HASONEVALUE ( '运单'[产品] ),
        SUMX ( FILTER ( B, [运量] >= A ), [运量] )
    )

M.累计占比 :=
DIVIDE (
    [M.累计运单量],
    CALCULATE ( [M.运单量], ALL ( '运单'[产品] ) )
)

M.ABC分类 :=
IF (
    HASONEVALUE ( '运单'[产品] ),
    SWITCH (
        TRUE (),
        [M.累计占比] <= 0.7, "A",
        [M.累计占比] <= 0.9, "B",
        "C"
    )
)

 Create a pivot table, drag the products in the waybill table into the row area, and check the measurement values ​​M. Waybill Volume, M. Cumulative Waybill Volume, M. Cumulative Proportion, and M. ABC Classification. The returned value is shown in Figure 5.

■ Figure 5 ABC classification analysis of products

04,Product opening and balance analysis

In the DK table, create a calculated column library. Year and month, the expression is as follows:

库.年月:= YEAR('DK'[日期])*100+MONTH('DK'[日期])

 The basic principle of balance analysis: the end of the previous period is equal to the beginning of the current period. Create measurement values ​​M. Incoming quantity, M. Outgoing quantity, M. Beginning quantity, M. Balance quantity. The expressions are as follows:

M.入库量:= SUM(DK[入库])
M.出库量:= SUM(DK[出库])

M.期初量:=
CALCULATE (
    [M.入库量] - [M.出库量],
    FILTER (
        ALL ('DK'[库.年月] ),
        'DK'[库.年月] < MIN ( 'DK'[库.年月] )
    )
)

M.结存量:= [M.期初量]+[M.入库量]-[M.出库量]

 Create a pivot table, drag the library, year and month into the row label, and check the measurement values ​​M. Beginning quantity, M. Incoming quantity, M. Outgoing quantity, M. Balance quantity. The returned value is shown in Figure 6.

 ■ Figure 6 Beginning and balance analysis of incoming and outgoing warehouses

Guess you like

Origin blog.csdn.net/qq_41640218/article/details/133668750