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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
cyberirbis
Frequent Visitor

Dynamic calculation based on filter and grouping

Hi all

 

I have measure formula like

divide(SUM('Table1'[Col1]),SUM('Table1'[Col2])+SUM('Table2'[Col3]) ,0)

 

Problem is with 'Table2'[Col3] field - it should be excluded from formula in case if some attribute, Let's say 'Table3'[Attr1] are added to grouping or filtered. 
I tried using SELECTEDVALUE():

VAR compValue = SELECTEDVALUE(Table3'[Attr1],"Total")

RETURN
SWITCH(
compValue,
"Total", divide(SUM('Table1'[Col1]),SUM('Table1'[Col2])+SUM('Table2'[Col3]) ,0),
divide(SUM('Table1'[Col1]),SUM('Table1'[Col2]) ,0)
)

but it works well for grouping - when only one or several values of 'Table3'[Attr1] selected.
But in case if there are no grouping by 'Table3'[Attr1]  and data are filtered by this attribute it doesn't work well. Because according to requirements it also should calculate as divide(SUM('Table1'[Col1]),SUM('Table1'[Col2]) ,0) , but it doesn't since there are selected more than one Table3'[Attr1] values .

Is there any way to detect, that there are any filter by some attribute added to report?

2 REPLIES 2
amitchandak
Super User
Super User

@cyberirbis , Try like

 

VAR compValue = maxx(filter(allselcted('Table3'),'Table3'[Attr1]= "Total"),'Table3'[Attr1])

RETURN
SWITCH(
compValue,
"Total", divide(SUM('Table1'[Col1]),SUM('Table1'[Col2])+SUM('Table2'[Col3]) ,0),
divide(SUM('Table1'[Col1]),SUM('Table1'[Col2]) ,0)
)

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 

sorry,  I don't get what this expression for:
VAR compValue = maxx(filter(allselcted('Table3'),'Table3'[Attr1]= "Total"),'Table3'[Attr1])

This will not work at all since 'Table3'[Attr1] is integer

In my example
VAR compValue = SELECTEDVALUE(Table3'[Attr1],"Total")
"Total" it's not value of 'Table3'[Attr1] field, it's just value to identify total row with several 'Table3'[Attr1] values  for further using in switch function



Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

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