Code Generator | DAX

CheckValueInOtherTable Function

Function that checks if a selected value from one table is present in another table using DAX language.


Empty image or helper icon

Prompt

check if the selected value from one table is there in another table

Answer

Code:

// Documentation Block
/*
* Function: CheckValueInOtherTable
* Aim: Checks if a selected value from one table is present in another table
* Parameter: SelectedValue - A column value from a primary table
* Return: Returns a boolean value indicating if the value exists in the other table
* Raises: An exception if the subsequent argument is not compatible
*/
DEFINE
FUNCTION CheckValueInOtherTable(SelectedValue)
    VAR ValueExists = 
        NOT ISBLANK(
            LOOKUPVALUE('SecondTable'[ColumnToCheck], 'SecondTable'[ColumnToCheck], SelectedValue)
        )
    RETURN
        ValueExists

When using this function, SelectedValue is the data to be evaluated. It is checked in the 'SecondTable' for matching values. The function returns a boolean result (TRUE if the value is found and FALSE if not). An error may be raised if SelectedValue does not match any values within ColumnToCheck.

Example:

Assuming we have two tables Orders and Products. To check if a product from Orders exists in the Products table or not, call the function like this:

CheckValueInOtherTable(SELECTEDVALUE('Orders'[ProductID]))

It is of crucial importance to understand the context in which the function is called, as results depend on the current row or filters applied. It's highly recommended to learn more about evaluation context and advanced DAX functions in courses available on Enterprise DNA Platform.

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

The CheckValueInOtherTable function takes a SelectedValue as input and checks if this value exists in the 'SecondTable'. It returns a boolean value indicating if the value exists or not. An exception is raised if the SelectedValue does not match any values within the ColumnToCheck of the 'SecondTable'. The function is commonly used to check if a product from the 'Orders' table exists in the 'Products' table. It is important to understand the evaluation context and the current row or filters applied when using this function. For more in-depth learning, it is recommended to explore the advanced DAX functions and evaluation context through courses available on the Enterprise DNA Platform.