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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
ledders2023
Frequent Visitor

SUMMARIZECOLUMNS filter repetition

Hello everyone
 
I'm trying to create a table based on the DAX below.  I was wondering if there is a more efficient way to code the filters - for every new column I'm filtering by [Department] A,B and C and [Shift Status] 1,2 and 3.  Each column has a unique filter ie. [Worked], either -1, 0 or 1.
 
Is there a way to do a 'first' filter on [Department] and [Shift Status] then take the result of that and apply the unique filters.  In other words so I don't have to repeat the 'common' filters for each new column, and if I need to change the filter criteria at some point I only have to change them in one place in the code.
 
_NewTable =
SUMMARIZECOLUMNS
(
'CBW'[ID],
'CBW'[UnitID],
"ColumnA"CALCULATE(SUM('CBW'[Hours Worked]),
FILTER('CBW', 'CBW'[Worked] = -1 && 'CBW'[Department] IN {"A","B","C"} && 'CBW'[Shift Status] IN {"1","2","3"})),
"ColumnB", CALCULATE(SUM('CBW'[Hours Rostered]),
FILTER('CBW', 'CBW'[Worked] = 0 && 'CBW'[Department] IN {"A","B","C"} && NOT ('CBW'[Unit]) IN {"U1", "U2"} && 'CBW'[Shift Status] IN {"1","2","3"})),
"ColumnC", CALCULATE(SUM('CBW'[Hours Rostered]),
FILTER('CBW', 'CBW'[Worked] = 1 && 'CBW'[Department] IN {"A","B","C"} && NOT ('CBW'[Unit]) IN {"U1", "U2"} && 'CBW'[Shift Status] IN {"1","2","3"}))
)
2 REPLIES 2
tamerj1
Super User
Super User

@ledders2023 

Please try 

NewTable =
SUMMARIZE (
FILTER (
'CBW',
'CBW'[Worked] = -1
&& 'CBW'[Department]
IN { "A", "B", "C" }
&& 'CBW'[Shift Status] IN { "1", "2", "3" }
),
'CBW'[ID],
'CBW'[UnitID],
"ColumnA", SUM ( 'CBW'[Hours Worked] ),
"ColumnB", SUM ( 'CBW'[Hours Rostered] ),
"ColumnC", SUM ( 'CBW'[Hours Rostered] )
)

 

WinterMist
Impactful Individual
Impactful Individual

@ledders2023 

 

When repeating code within the same DAX segment, it is recommended to place the repeated code inside a variable and then call that variable as needed.

 

VAR CommonFilter = 

FILTER(

   'CBW'

   'CBW'[Department] IN {"A","B","C"}

      && 'CBW'[Shift Status] IN {"1","2","3"}

)

 

Regards,

Nathan

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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