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
RickBickens
Helper I
Helper I

DAX table creation depending on boolean evaluation

Hi there,
I am trying to create a table in PowerBI that is automatically generated depending on the information of a column in another table.
If category has more values, I want to summarize the information of the Master table by category, but if subcategory as more values, I want to use subcategory to summarize the table. 
My DAX code to attempt this table generation is this:

Test Group Dim Lookup = 
var GroupByCat = IF(DISTINCTCOUNT(Item_Master[CATEGORY]) > DISTINCTCOUNT(Item_Master[SUBCATEGORY]), 0, 1)

 
Return
IF(GroupByCat = 0, 
    FILTER(SUMMARIZE(Item_Master,[CATEGORY],"Avg Unit L",AVERAGE(Item_Master[UNIT L]),"Avg Unit W",AVERAGE(Item_Master[UNIT W]),"Avg Unit H", AVERAGE(Item_Master[UNIT H]),"Avg Unit Weight", AVERAGE(Item_Master[UNIT WEIGHT]),"Avg Case L",AVERAGE(Item_Master[MASTER CASE L]),"Avg Case W",AVERAGE(Item_Master[MASTER CASE W]),"Avg Case H",AVERAGE(Item_Master[MASTER CASE H]), "Avg Case Weight",AVERAGE(Item_Master[MASTER CASE WEIGHT]),"Avg Case Qty",ROUNDUP(AVERAGE(Item_Master[UNITS PER CASE]),0)),NOT(ISBLANK([CATEGORY]))), 
    FILTER(SUMMARIZE(Item_Master,[SUBCATEGORY],"Avg Unit L",AVERAGE(Item_Master[UNIT L]),"Avg Unit W",AVERAGE(Item_Master[UNIT W]),"Avg Unit H", AVERAGE(Item_Master[UNIT H]),"Avg Unit Weight", AVERAGE(Item_Master[UNIT WEIGHT]),"Avg Case L",AVERAGE(Item_Master[MASTER CASE L]),"Avg Case W",AVERAGE(Item_Master[MASTER CASE W]),"Avg Case H",AVERAGE(Item_Master[MASTER CASE H]), "Avg Case Weight",AVERAGE(Item_Master[MASTER CASE WEIGHT]),"Avg Case Qty",ROUNDUP(AVERAGE(Item_Master[UNITS PER CASE]),0)),NOT(ISBLANK([SUBCATEGORY]))))
        
   

Unfortunately, both IF and SWITCH only return scalar values and cannot return tables, so that is where I am stuck. With either SWITCH or IF, I get the error"The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value."

Is there a way to have my new table generated differently based on a boolean evaluation?

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

Instead of using an IF, you can use a UNION and FILTER like this:

Test Group Dim Lookup =
VAR GroupByCat =
    IF (
        DISTINCTCOUNT ( Item_Master[CATEGORY] )
            > DISTINCTCOUNT ( Item_Master[SUBCATEGORY] ),
        0,
        1
    )
VAR _Union_ =
    UNION (
        FILTER (
            SUMMARIZE (
                Item_Master,
                [CATEGORY],
                "Boole", 0,
                "Avg Unit L", AVERAGE ( Item_Master[UNIT L] ),
                "Avg Unit W", AVERAGE ( Item_Master[UNIT W] ),
                "Avg Unit H", AVERAGE ( Item_Master[UNIT H] )
            ),
            NOT ( ISBLANK ( [CATEGORY] ) )
        ),
        FILTER (
            SUMMARIZE (
                Item_Master,
                [SUBCATEGORY],
                "Boole", 1,
                "Avg Unit L", AVERAGE ( Item_Master[UNIT L] ),
                "Avg Unit W", AVERAGE ( Item_Master[UNIT W] ),
                "Avg Unit H", AVERAGE ( Item_Master[UNIT H] )
            ),
            NOT ( ISBLANK ( [SUBCATEGORY] ) )
        )
    )
RETURN
    FILTER ( _Union_, [Boole] = GroupByCat )

 

I didn't invent this technique myself, but I don't remember where I learned it.

View solution in original post

5 REPLIES 5
AlexisOlson
Super User
Super User

Instead of using an IF, you can use a UNION and FILTER like this:

Test Group Dim Lookup =
VAR GroupByCat =
    IF (
        DISTINCTCOUNT ( Item_Master[CATEGORY] )
            > DISTINCTCOUNT ( Item_Master[SUBCATEGORY] ),
        0,
        1
    )
VAR _Union_ =
    UNION (
        FILTER (
            SUMMARIZE (
                Item_Master,
                [CATEGORY],
                "Boole", 0,
                "Avg Unit L", AVERAGE ( Item_Master[UNIT L] ),
                "Avg Unit W", AVERAGE ( Item_Master[UNIT W] ),
                "Avg Unit H", AVERAGE ( Item_Master[UNIT H] )
            ),
            NOT ( ISBLANK ( [CATEGORY] ) )
        ),
        FILTER (
            SUMMARIZE (
                Item_Master,
                [SUBCATEGORY],
                "Boole", 1,
                "Avg Unit L", AVERAGE ( Item_Master[UNIT L] ),
                "Avg Unit W", AVERAGE ( Item_Master[UNIT W] ),
                "Avg Unit H", AVERAGE ( Item_Master[UNIT H] )
            ),
            NOT ( ISBLANK ( [SUBCATEGORY] ) )
        )
    )
RETURN
    FILTER ( _Union_, [Boole] = GroupByCat )

 

I didn't invent this technique myself, but I don't remember where I learned it.

amitchandak
Super User
Super User

@RickBickens ,
Can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data.

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hi @amitchandak ,

Would screenshots of the tables in question work?
This is the Item_Master table I am pulling information from:

RickBickens_0-1664903731653.png

And the Table I am trying to produce from this Item_Master is a lookup table of the average L/W/H for a given Category or Subcategory:

RickBickens_1-1664903811744.png

The difference being that I want the output table to automatically generate and summarize based on Category OR Subcategory depending on which one has more resolution. The Item_Masters are always different so having the output table intelligently generate would be ideal.

@RickBickens , Please share some sample data. Need to work on the file.

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

@amitchandak I've created a stripped down version of the data to share, but I cannot attach .csv/.xlsx files. I can't share my pbix. file as it has some sensative informaiton in it. Did you want me to just create a table within my reply populated with some sample data?

 

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!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.