Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

DAX / PowerBi: How to get the products that are NOT on stock?

I have 3 tables: STOCK, COLORPRICE, PRODUCT
The relationship between them is:
PRODUCT.PRODUCT = COLORPRICE.PRODUCT (1 to many)
COLORPRICE.EAN = STOCK.EAN (1 to many)

Now I would like to get in a PowerBi Visualisation the active products which are not in our stock.
That means which are not available in "STOCKROOM" and/or "EXPEDITION"
AND where PUBLIC = "Y" and ACTIVE = "Y"
From my sample that should deliver PRODUCT 'aaa 111333555777' and 'bbb 222333555666'.

My sample perhaps looks a bit complex, but this is it: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This measure should return "Not In Stock" when the product is missing or BLANK() when it's not active or public or in expedition or stockroom. It can be used in a matrix visual with Product[Product] on the rows, then only the missing product would be listed.

Not In Stock =
IF(
    ISINSCOPE( 'PRODUCT'[PRODUCT] ),
    IF(
        CALCULATE(
            ISEMPTY( STOCK ),
            'PRODUCT'[PUBLIC] = "Y",
            COLORPRICE[ACTIVE] = "Y",
            STOCK[LOCATION] IN { "stockroom", "expedition" }
        ),
        "Not In Stock"
    )
)

Edit:

to make it work at the EAN level, we can iterate over the COLORPRICE table and perform the check at the COLORPRICE row level. To aggregeate the result at the product level we may set a product Non In Stock when at least one of its EAN is out of stock. This is not tested, but I hope it will work :)

Not In Stock =
IF(
    ISINSCOPE( 'PRODUCT'[PRODUCT] ),
    IF(
        SELECTEDVALUE( 'PRODUCT'[PUBLIC] ) = "Y",
        IF(
            SUMX(
                FILTER( COLORPRICE, COLORPRICE[ACTIVE] = "Y" ),
                IF(
                    CALCULATE(
                        ISEMPTY( STOCK ),
                        STOCK[LOCATION] IN { "stockroom", "expedition" }
                    ),
                    1
                )
            ) > 0,
            "Not In Stock"
        )
    )
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...