Unit Test Writer | DAX

Unit Tests for Product Filtering by Color in Different Categories

This DAX script includes unit tests for filtering products by color (red) in clothing, accessories, and footwear categories. It utilizes SWITCH function to categorize products.


Empty image or helper icon

Prompt

EVALUATE
FILTER(
    ADDCOLUMNS(
        'Product',
        "Category",
        SWITCH(
            TRUE(),
            RELATED('ProductCategory'[ProductCategoryName]) = "Clothing", "Apparel",
            RELATED('ProductCategory'[ProductCategoryName]) = "Accessories", "Accessory",
            RELATED('ProductCategory'[ProductCategoryName]) = "Footwear", "Shoes",
            "Other"
        )
    ),
    'Product'[Color] = "Red"
)

Answer

# Unit Test 1: Test for filtering Products with Color Red in Clothing Category
EVALUATE
VAR FilteredProducts =
    FILTER(
        ADDCOLUMNS(
            'Product',
            "Category",
            SWITCH(
                TRUE(),
                RELATED('ProductCategory'[ProductCategoryName]) = "Clothing", "Apparel",
                RELATED('ProductCategory'[ProductCategoryName]) = "Accessories", "Accessory",
                RELATED('ProductCategory'[ProductCategoryName]) = "Footwear", "Shoes",
                "Other"
            )
        ),
        'Product'[Color] = "Red"
    )
RETURN
    COUNTROWS(FilteredProducts) > 0

# Unit Test 2: Test for filtering Products with Color Red in Accessories Category
EVALUATE
VAR FilteredProducts =
    FILTER(
        ADDCOLUMNS(
            'Product',
            "Category",
            SWITCH(
                TRUE(),
                RELATED('ProductCategory'[ProductCategoryName]) = "Clothing", "Apparel",
                RELATED('ProductCategory'[ProductCategoryName]) = "Accessories", "Accessory",
                RELATED('ProductCategory'[ProductCategoryName]) = "Footwear", "Shoes",
                "Other"
            )
        ),
        'Product'[Color] = "Red"
    )
RETURN
    COUNTROWS(FilteredProducts) > 0

# Unit Test 3: Test for filtering Products with Color Red in Footwear Category
EVALUATE
VAR FilteredProducts =
    FILTER(
        ADDCOLUMNS(
            'Product',
            "Category",
            SWITCH(
                TRUE(),
                RELATED('ProductCategory'[ProductCategoryName]) = "Clothing", "Apparel",
                RELATED('ProductCategory'[ProductCategoryName]) = "Accessories", "Accessory",
                RELATED('ProductCategory'[ProductCategoryName]) = "Footwear", "Shoes",
                "Other"
            )
        ),
        'Product'[Color] = "Red"
    )
RETURN
    COUNTROWS(FilteredProducts) > 0

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This DAX script includes unit tests for filtering products by color (red) in clothing, accessories, and footwear categories. It utilizes SWITCH function to categorize products.