当前位置:网站首页>Bi skill - judge 0 and null

Bi skill - judge 0 and null

2022-06-25 01:12:00 Powerbi white tea

BOSS: Who is that ! White tea , Come here, come here , I have a need to tell you !
White tea :( Black question mark ?) Yo ,BOSS?
BOSS: That's true , We are during the national day , Sold some goods , But some products are special , Part of the sales amount is empty , When we calculate the gross profit, we need to multiply the cost by 1.5; The sales amount of some commodities is 0, The calculation of gross profit is uniformly displayed as 0.01, Can handle no ?
White tea :(????)BOSS, It is possible to , What is the algorithm ?
BOSS: Don't ask too much , Asking is the business of the company !
White tea : Oh , understand !

Under normal circumstances , When we measure data quality , They tend to care about data with numerical values , But in real business , Especially in the financial algorithm ,0 And empty sometimes is also an important calculation basis .

Let's take a look at the case data in this issue :

The data is relatively simple , Only 6 Column , From the screenshot, we can see that the sales amount of some data is 0, Part is empty , But there are costs , Have quantity .

Under normal circumstances, we only need to consider the data that the sales amount is not empty , However, there may be transfer inventory in the actual business 、 Status of complimentary sales .

Therefore, sometimes we prefer to apply financial scenarios , We need to consider this situation , stay PowerBI How to distinguish between 0 And empty cloth ?

We are PowerBI Pass through Filter Function to filter and see .

Code :

 Screening 0 =
FILTER ( 'Data', 'Data'[SalesAmt] = 0 )

You can see , Though we pass Filter Function 0 Screening , But the actual result is still empty . Let's continue to filter the null values to see .

Code ;

 Filter empty  =
FILTER ( 'Data', 'Data'[SalesAmt] = BLANK() )

The same as above , There are still no other changes .

You can think about it , How to solve this problem ?
3
2
1

There are two solutions : Strictly equal to ISBLANK.

Strictly equal :

Usually we use the equality operator , In this case, we can use strict equality to distinguish 0 And emptiness .

Code :

 Strict screening 0 =
FILTER ( 'Data', 'Data'[SalesAmt] == 0 )

Code :

 Strictly filter empty  =
FILTER ( 'Data', 'Data'[SalesAmt] == BLANK() )

ISBLANK:

ISBLANK Can identify empty .

Code :

ISBLANK Screening 0 =
FILTER ( 'Data', NOT ISBLANK ( 'Data'[SalesAmt] ) && 'Data'[SalesAmt] = 0 )

Code :

ISBLANK Filter empty  =
FILTER ( 'Data', ISBLANK ( 'Data'[SalesAmt] ) )

therefore , We can do this through these two methods BOSS The requirements mentioned at the beginning .

Part of the sales amount is empty , When we calculate the gross profit, we need to multiply the cost by 1.5;
The sales amount of some commodities is 0, The calculation of gross profit is uniformly displayed as 0.01.

gross profit 1:

   gross profit 1 =
VAR AMT =
    SUM ( 'Data'[SalesAmt] )
VAR COST =
    SUMX ( 'Data', [Cost] * [Quantity] )
VAR Result =
    SWITCH (
        TRUE (),
        AMT == BLANK (),
            AMT - COST * 1.5,
        AMT == 0, 0.01,
        AMT - COST
    )
RETURN
    Result

give the result as follows :

gross profit 2:

  gross profit 2 =
VAR AMT =
    SUM ( 'Data'[SalesAmt] )
VAR COST =
    SUMX ( 'Data', [Cost] * [Quantity] )
VAR Result =
    SWITCH ( TRUE (), ISBLANK ( AMT ), AMT - COST * 1.5, AMT = 0, 0.01, AMT - COST )
RETURN
    Result

give the result as follows :

friends GET Why? ?
(BOSS: Can you can !)

This is white tea , One PowerBI Beginners .

原网站

版权声明
本文为[Powerbi white tea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210542594077.html