Data Structures & Algorithms

Power BI LOOKUPVALUE DAX Function

Are you looking to retrieve specific values from related tables in Power BI? Interestingly, Microsoft’s DAX language provides various functions that can enable you to retrieve specific values from related tables, and one such function is LOOKUPVALUE.

The LOOKUPVALUE function looks up a specific value in a column from a related table based on one or more search criteria. The result of this function is a single value from the specified column that matches the given conditions.

This article explores the ins and outs of the LOOKUPVALUE DAX function, its syntax, parameters, use cases, and the examples to harness its full potential.

Syntax and Parameters:

The syntax for the LOOKUPVALUE function is as follows:

LOOKUPVALUE(result_column, search_column1, search_value1, [search_column2, search_value2, ...])

Its parameters are:

  • result_column: The column from which the function returns a value.
  • search_column1, search_column2, …: The columns in which the function searches for the specified values.
  • search_value1, search_value2, …: The values to search for in the specified columns.

Examples and Use Cases

The following are some of the examples and use cases of the Power BI LOOKUPVALUE DAX function.

Note: The sample codes provided in the examples assume that the required relationships between tables are setup correctly in the data model. If the relationships are not defined, the LOOKUPVALUE function may not return the expected results.

Use Case 1: Retrieving Related Information

In many scenarios, you may have the data spread across multiple tables, and you need to extract related information for a particular analysis or report. The LOOKUPVALUE function can help you access the data from one table based on conditions that are defined in another table.

Example:

Suppose you have two tables: “Sales” and “Products”. The “Sales” table contains the sales data with the “ProductID”, “Date”, and “Revenue” columns. The “Products” table contains the product details with the “ProductID” and “Product Name” columns. You want to create a report that shows the product name alongside the sales data.

Here’s a code snippet for this case use:

SalesDataWithProductName =

ADDCOLUMNS (

Sales,

"ProductName", LOOKUPVALUE(Products[Product Name], Products[ProductID], Sales[ProductID])

)

The given example uses the ADDCOLUMNS function to add a new calculated “ProductName” column to the “Sales” table. The LOOKUPVALUE function looks for the corresponding “Product Name” in the “Products” table based on the “ProductID” in the “Sales” table.

Use Case 2: Handling the Parent-Child Relationships

In hierarchical data models such as a parent-child relationship, you may need to access the attributes from the parent table and use them in calculations for the child table. The LOOKUPVALUE function can be valuable in such cases.

Example:

Let’s consider a scenario where you have a table called “Employees” that contains the data about employees and their respective managers. The “Employees” table has columns like “EmployeeID”, “EmployeeName”, and “ManagerID”. You want to create a report that shows the names of both employees and their managers.

This code snippet will help:

EmployeeWithManager =

ADDCOLUMNS (

Employees,

"ManagerName", LOOKUPVALUE(Employees[EmployeeName], Employees[EmployeeID], Employees[ManagerID])

)

In this example, we use the ADDCOLUMNS function again to add a new calculated column which is “ManagerName” to the “Employees” table. The LOOKUPVALUE function fetches the “EmployeeName” from the “Employees” table where the “EmployeeID” matches the “ManagerID.”

Use Case 3: Conditional Calculations

The LOOKUPVALUE function can be used in combination with the other DAX functions to perform conditional calculations based on values from related tables.

Example:

Consider a scenario where you have a “Sales” table with the “ProductID”, “Date”, and “Revenue” columns and a “Discounts” table with the “ProductID” and “DiscountPercentage” columns. You want to calculate the discounted revenue for each sale based on the applicable discount percentage.

Refer to the following sample code:

DiscountedRevenue =

Sales[Revenue] * (1 - LOOKUPVALUE(Discounts[DiscountPercentage], Discounts[ProductID], Sales[ProductID]))

In this example, we multiply the “Revenue” column in the “Sales” table with (1 – DiscountPercentage) from the “Discounts” table, looked up based on the matching “ProductID.”

Use Case 4: Handling Missing Values

The LOOKUPVALUE function can be used to handle the missing values gracefully in your data.

Example:

Suppose you have a “Customer” table with the “CustomerID” and “CustomerCountry” columns. Some customers might have missing country information. You want to create a report where the customers with missing country information are labeled as “Unknown.”

Sample code:

CustomerWithCountry =

ADDCOLUMNS (

Customer,

"Country", IF(ISBLANK(Customer[CustomerCountry]), "Unknown", LOOKUPVALUE(Customer[CustomerCountry], Customer[CustomerID], Customer[CustomerID]))

)

This example uses the IF function along with the LOOKUPVALUE function to check if the “CustomerCountry” is blank. If it is, we label the customer as “Unknown”. Otherwise, we use the LOOKUPVALUE function to retrieve the actual “CustomerCountry” based on the “CustomerID.”

Conclusion

The Power BI LOOKUPVALUE DAX function is a versatile tool that enables you to retrieve specific values from related tables, handle the parent-child relationships, perform conditional calculations, and handle the missing values gracefully. Understanding these use cases and applying them in your Power BI reports will inevitably enhance your data analysis and create more insightful visualizations.

About the author

Kennedy Brian

Brian is a computer scientist with a bias for software development, programming, and technical content development. He has been in the profession since 2015. He reads novels, jogs, or plays table tennis whenever not on gadgets. He is an expert in Python, SQL, Java, and data and network security.