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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
smpa01
Super User
Super User

DAX measure to aggregate only when not exists any single row for condition grouped by a level

My sample dataset is following

 

| sn | site  | cond | val  |
|----|-------|------|------|
| 1  | site1 | X    | 100  |
| 2  | site1 | Y    | 200  |
| 3  | site1 | Z    | 300  |
| 1  | site2 | A    | 100  |
| 2  | site2 | B    | 200  |
| 3  | site2 | C    | 300  |
| 1  | site3 | X    | 100  |
| 2  | site3 | P    | 200  |
| 3  | site3 | Q    | 300  |
| 1  | site4 | A    | 100  |
| 2  | site4 | Y    | 200  |
| 3  | site4 | Q    | 300  |
| 1  | site5 | E    | 100  |
| 1  | site5 | E    | 1000 |
| 2  | site5 | F    | 200  |
| 3  | site5 | G    | 300  |

 

I want DAX to check by site if there exists a row where cond='X' or cond ='Y'. if yes, retrun null, if no, sum val.

My desired result is below

 

| sn | site  | cond | revisedTotal |
|----|-------|------|--------------|
| 1  | site1 | X    | NULL         |
| 2  | site1 | Y    | NULL         |
| 3  | site1 | Z    | NULL         |
| 1  | site2 | A    | 100          |
| 2  | site2 | B    | 200          |
| 3  | site2 | C    | 300          |
| 1  | site3 | X    | NULL         |
| 2  | site3 | P    | NULL         |
| 3  | site3 | Q    | NULL         |
| 1  | site4 | A    | NULL         |
| 2  | site4 | Y    | NULL         |
| 3  | site4 | Q    | NULL         |
| 1  | site5 | E    | 1100         |
| 2  | site5 | F    | 200          |
| 3  | site5 | G    | 300          |

 

A TSQL equivalent solution would be following with DDL

 

declare @t1 as table (sn int, site varchar(max), cond varchar(max), val int)
insert into @t1
select * from (values(1,'site1','X',100),(2,'site1','Y',200),(3,'site1','Z',300)
                     , (1,'site2','A',100),(2,'site2','B',200),(3,'site2','C',300)
					 , (1,'site3','X',100),(2,'site3','P',200),(3,'site3','Q',300)
					 , (1,'site4','A',100),(2,'site4','Y',200),(3,'site4','Q',300)
					 , (1,'site5','E',100),(1,'site5','E',1000), (2,'site5','F',200),(3,'site5','G',300)) t (a,b,c,d)


select a.sn, a.site, a.cond, sum(case when a.site=x.site then a.val else null end) as revisedVal
from @t1 a
outer apply (select b.site from (select site from @t1
where cond <>'X' or cond <>'Y'
EXCEPT
select site from @t1
where cond ='X' or cond ='Y'
) b where a.site=b.site) x
group by  a.site, a.sn, a.cond

 

 

Capture.PNG

How can I achieve the same in a DAX measure?

 

Queryable source tbl

sn site cond val
1 site1 X 100
2 site1 Y 200
3 site1 Z 300
1 site2 A 100
2 site2 B 200
3 site2 C 300
1 site3 X 100
2 site3 P 200
3 site3 Q 300
1 site4 A 100
2 site4 Y 200
3 site4 Q 300
1 site5 E 100
1 site5 E 1000
2 site5 F 200
3 site5 G 300

 

I have tried the following DAX measure but it does not return what I was hoping for

 

 

Measure =
CALCULATE (
    SUM ( 'fact'[val] ),
    TREATAS (
        EXCEPT (
            SUMMARIZE (
                FILTER ( 'fact', 'fact'[cond] <> "X" || 'fact'[cond] <> "Y" ),
                'fact'[site]
            ),
            SUMMARIZE (
                FILTER ( 'fact', 'fact'[cond] = "X" || 'fact'[cond] = "Y" ),
                'fact'[site]
            )
        ),
        'fact'[site]
    )
)

 

 

cap2.PNG

I want a performant DAX measure that only returns value for site2 and site5 because there does not exist any row for any of these sites where  cond=X or cond =Y by site

 

Thank you in advance

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

revisedVal = 
SUMX(
    VALUES( table1[site] ),
    IF(
        ISEMPTY(
            CALCULATETABLE(
                table1,
                ALLEXCEPT( table1, table1[site] ),
                table1[cond] IN { "X", "Y" }
            )
        ),
        CALCULATE( SUM( table1[val] ) )
    )
)

Screenshot 2021-09-05 123348.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

5 REPLIES 5
CNENFRNL
Community Champion
Community Champion

revisedVal = 
SUMX(
    VALUES( table1[site] ),
    IF(
        ISEMPTY(
            CALCULATETABLE(
                table1,
                ALLEXCEPT( table1, table1[site] ),
                table1[cond] IN { "X", "Y" }
            )
        ),
        CALCULATE( SUM( table1[val] ) )
    )
)

Screenshot 2021-09-05 123348.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

@CNENFRNL @Fowmy  learnt a lot by analyzing this solution.

The key takeway from this how 

 

'fact'[cond] IN { "X", "Y" }

 

differs from

 

FILTER(fact,fact[cond]="X"||fact[comd]"Y")

 

DAX is beautifully complex and hence, the nuances in syntax makes a world of difference for the meaures even though for table evaluation they return the same result but they are not the same only understood when it comes to use that int he measure.

Also, I learnt the usage of  ISEMPTY with a practical use case. Thanks for that.

Since, I am running the measure on 2M+ row table (constantly compounding) I prefer a solution without iterator, hence after analyzing I rewrote the measure using TREATAS. Also, I wanted to see how can I utilize this inside TREATAS.

 

 

 

noIterator = 
VAR _00 =
    ALLEXCEPT ( 'fact', 'fact'[site] )
VAR _0 =
    SUMMARIZE (
        CALCULATETABLE ( 'fact', 'fact'[cond] IN { "X", "Y" }, _00 ),
        [site]
    )
VAR _1 =
    SUMMARIZE (
        CALCULATETABLE ( 'fact', NOT 'fact'[cond] IN { "X", "Y" }, _00 ),
        [site]
    )
VAR _2 =
    EXCEPT ( _1, _0 )
VAR _3 =
    CALCULATE ( [_sum], TREATAS ( _2, 'fact'[site] ) )
RETURN
    _3

 

 

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Fowmy
Super User
Super User

@smpa01 

Try this meaure:

 

M1 = 
VAR __Site = MAX(Table1[ site  ])
VAR __Cond = CALCULATETABLE( VALUES(Table1[ cond ]) , Table1[ site  ] = __Site , REMOVEFILTERS(Table1))

RETURN
IF( 
    ISEMPTY( INTERSECT( {"X","Y"}, __Cond)),
    SUM(Table1[ val  ])
)

 

 

Fowmy_0-1630827792695.png

 

Use the following measure if you need the grand total also to be evaluated correctly:

M1 = 
SUMX(
    Table1,
    VAR __Site = Table1[ site  ]
    VAR __Cond = CALCULATETABLE( VALUES(Table1[ cond ]) , Table1[ site  ] = __Site , REMOVEFILTERS(Table1))
    RETURN
    IF( 
        ISEMPTY( INTERSECT( {"X","Y"}, __Cond)),
        Table1[ val  ]
    )
)

 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

TomMartens
Super User
Super User

Hey @smpa01 ,

 

please provide sample data, that can easily be imported into Power BI Desktop, or create a pbix that contains sample data, upload the pbix to onedrive or dropbox and share the link.

 

Regards,
Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Hi @TomMartens  it was already provided in the OP

 

cap3.PNG

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.

Top Solution Authors