Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
rks
Resolver II
Resolver II

Return a Table Statement from IF-condition (for TREATAS)

Hi folks,

 

I receive an error message:

The TREATAS function expects a table expression for argument '1', but a string or numeric expression was used.

 

I have the following measure:

 

 

 

VAR __TableVariable=
    IF(
        ISFILTERED( 'Product'[Color] ),
        VALUES( 'Product'[Color] ),
        { "#all_colors" }
    )
RETURN
    CALCULATE(
        SUM( 'Product Forecast'[Value] ),
        TREATAS ( __TableVariable , 'Product Forecast'[Color] )
)

 

 

 The use cases:
I have a sales table related to the product table ('product'). The product table contains a column called "color".

 

I also have a forecast table that holds data for a given product color. I want to create data lineage between the product-table and the forecast color if the product table is filted. If it is not filtered I want to use the default member "#all_colors". This is because the sum of the forecast value over all rows is not equal the the prediction for "#all_colors". 

 

The error message suggests that the variable is no table expression, however both branches (if and else) of the IF-function are valid table expressions.

 

I tried to reproduce the behavior on dax.do: https://dax.do/ravDLEMzBhp9Mr/

 

Help to avoid the error is much appreciated.

 

Thank you!

Konstantin

1 ACCEPTED SOLUTION
leomureb
Microsoft Employee
Microsoft Employee

Hi,

 

I was having the same issue as you and adding the variable inside {} in the TREATAS function solved my issue.

 

Try this:

VAR __TableVariable=
    IF(
        ISFILTERED( 'Product'[Color] ),
        VALUES( 'Product'[Color] ),
        { "#all_colors" }
    )
RETURN
    CALCULATE(
        SUM( 'Product Forecast'[Value] ),
        TREATAS ( {__TableVariable} , 'Product Forecast'[Color] )
)

View solution in original post

3 REPLIES 3
leomureb
Microsoft Employee
Microsoft Employee

Hi,

 

I was having the same issue as you and adding the variable inside {} in the TREATAS function solved my issue.

 

Try this:

VAR __TableVariable=
    IF(
        ISFILTERED( 'Product'[Color] ),
        VALUES( 'Product'[Color] ),
        { "#all_colors" }
    )
RETURN
    CALCULATE(
        SUM( 'Product Forecast'[Value] ),
        TREATAS ( {__TableVariable} , 'Product Forecast'[Color] )
)
rks
Resolver II
Resolver II

Hi @devesh_gupta  thank you for the reply!

Changing the measure results in the same behavior.

 

However, the businesslogic is also wrong, because I don't want to remove the filters from the column, but instead use the value "#all_colors" which holds the forecast data if no products are selected (which is different from the sum of all values).

devesh_gupta
Impactful Individual
Impactful Individual

@rks Try modifying your measure as follow to make your __TableVariable consistently a table. Try if it works for you:

VAR __TableVariable=
    IF(
        ISFILTERED( 'Product'[Color] ),
        VALUES( 'Product'[Color] ),
        ALL( 'Product'[Color] )
    )
RETURN
    CALCULATE(
        SUM( 'Product Forecast'[Value] ),
        TREATAS ( __TableVariable , 'Product Forecast'[Color] )
)

 

If you find this insightful, please provide a Kudo and accept this as a solution.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.