Forum Discussion
DAX using IF (aggregation within another aggregation). I get an incorrect total.
- 7 years ago
Hi RJ
You may refer to below post.
https://community.powerbi.com/t5/Desktop/SUM-Negative-Account-Balances/m-p/544179#M255705
Regards,
Cherie
- 7 years ago
Brilliant
It worked. Thank you
Sumif4 =VAR tableA = summarize(Dates,Dates[MonthYear],"a",CALCULATE([Sales£M] ,USERELATIONSHIP(Header[Order Date] ,Dates[Date] )) )RETURNCALCULATE ( [Sales£M] ,USERELATIONSHIP(Header[Order Date] ,Dates[Date] ) , FILTER ( tableA, [a] > 100000 ) )//-----------------------So a variable is set up called VAR tableAThis generates a temp table summarized by a column Dates[MonthYear]. With the measureCALCULATE([Sales£M] ,USERELATIONSHIP(Header[Order Date] ,Dates[Date] ))So in effect a temporary table is created. This table is called TableA. It can be called any name. This temp table has one dimension >>Dates,Dates[MonthYear] << and one measure called a. It could however be called any name requiredThis temp table is then used to filter the expressionCALCULATE ( [Sales£M] ,USERELATIONSHIP(Header[Order Date] ,Dates[Date] )as requiredNB. Another option is to create a new actual table. SayTableName =SalesMthYrOrdDate = ADDCOLUMNS( summarize(Dates, Dates[MonthYear],Dates[Year]), //load required dimensions"SalesOrdDate", //or whatever name you wish to call the measureCALCULATE([Sales£M] ,USERELATIONSHIP(Header[Order Date] ,Dates[Date] )) ) //measure column called "SalesOrdDate"and then join the MonthYear column to the MonthYear column in the Dates table //link using PBI relationshipsFinal step. Use this expressionSumif7 =CALCULATE ( [Sales£M] ,USERELATIONSHIP(Header[Order Date] ,Dates[Date] ) , FILTER (SalesMthYrOrdDate,SalesMthYrOrdDate[SalesOrdDate] > 100000 )) - 7 years ago
To make it more effecient. Add 'ADDCOLUMN'
Sumif6 =VAR tableB = ADDCOLUMNS( summarize(Dates, Dates[MonthYear],Dates[Year]), "SumMthYr",CALCULATE([Sales£M] ,USERELATIONSHIP(Header[Order Date] ,Dates[Date] )) )RETURN CALCULATE ( [Sales£M] ,USERELATIONSHIP(Header[Order Date] ,Dates[Date] ) , FILTER ( tableB, [SumMthYr] > 100000 ) )or an alternative isSumif6 =VAR tableB = summarizecolumns(Dates, Dates[MonthYear],Dates[Year], "SumMthYr",CALCULATE([Sales£M] ,USERELATIONSHIP(Header[Order Date] ,Dates[Date] )) )RETURN CALCULATE ( [Sales£M] ,USERELATIONSHIP(Header[Order Date] ,Dates[Date] ) , FILTER ( tableB, [SumMthYr] > 100000 ) )
Ill try this tomorrow
DAX Measure with Nested IF Statements#
Re: DAX Measure with Nested IF Statements
[ New ]01-14-2017 08:04 PM - edited 01-14-2017 08:08 PM
@MWinter225If you do want Measures - these should work also! 
MEASURE 1
Total Adj Sales ALT =
SUMX (
'Table',
IF (
'Table'[Adjustment] = "b",
'Table'[Sales] * 0.9,
IF ( 'Table'[Adjustment] = "c", 'Table'[sales] * 0.5, 'Table'[Sales] )
)
)MEASURE 2 - SWITCH is internally converted into nested IFs - one thing I really like is that its much easier to read and write
Total Adj Sales ALT 2 =
SUMX (
'Table',
SWITCH (
TRUE (),
'Table'[Adjustment] = "b", 'Table'[Sales] * 0.9,
'Table'[Adjustment] = "c", 'Table'[sales] * 0.5,
'Table'[Sales]
)
)Now you have 3 options which should all give you the same result!
Good Luck! 
[
- v-cherch-msft7 years ago
Microsoft Employee
Hi RJ
You may refer to below post.
https://community.powerbi.com/t5/Desktop/SUM-Negative-Account-Balances/m-p/544179#M255705
Regards,
Cherie
- RJ7 years ago
Resolver II
Hi Cherie
Thanks
This looks promising.
NegativeSum =
VAR tableA = SUMMARIZE ( Table4, Table4[Client ], "a", CALCULATE ( SUM ( Table4[Amount] ) ) )
RETURN CALCULATE ( SUM ( Table4[Amount] ), FILTER ( tableA, [a] < 0 ) )
- RJ7 years ago
Resolver II
Brilliant
It worked. Thank you
Sumif4 =VAR tableA = summarize(Dates,Dates[MonthYear],"a",CALCULATE([Sales£M] ,USERELATIONSHIP(Header[Order Date] ,Dates[Date] )) )RETURNCALCULATE ( [Sales£M] ,USERELATIONSHIP(Header[Order Date] ,Dates[Date] ) , FILTER ( tableA, [a] > 100000 ) )//-----------------------So a variable is set up called VAR tableAThis generates a temp table summarized by a column Dates[MonthYear]. With the measureCALCULATE([Sales£M] ,USERELATIONSHIP(Header[Order Date] ,Dates[Date] ))So in effect a temporary table is created. This table is called TableA. It can be called any name. This temp table has one dimension >>Dates,Dates[MonthYear] << and one measure called a. It could however be called any name requiredThis temp table is then used to filter the expressionCALCULATE ( [Sales£M] ,USERELATIONSHIP(Header[Order Date] ,Dates[Date] )as requiredNB. Another option is to create a new actual table. SayTableName =SalesMthYrOrdDate = ADDCOLUMNS( summarize(Dates, Dates[MonthYear],Dates[Year]), //load required dimensions"SalesOrdDate", //or whatever name you wish to call the measureCALCULATE([Sales£M] ,USERELATIONSHIP(Header[Order Date] ,Dates[Date] )) ) //measure column called "SalesOrdDate"and then join the MonthYear column to the MonthYear column in the Dates table //link using PBI relationshipsFinal step. Use this expressionSumif7 =CALCULATE ( [Sales£M] ,USERELATIONSHIP(Header[Order Date] ,Dates[Date] ) , FILTER (SalesMthYrOrdDate,SalesMthYrOrdDate[SalesOrdDate] > 100000 ))- RJ7 years ago
Resolver II
To make it more effecient. Add 'ADDCOLUMN'
Sumif6 =VAR tableB = ADDCOLUMNS( summarize(Dates, Dates[MonthYear],Dates[Year]), "SumMthYr",CALCULATE([Sales£M] ,USERELATIONSHIP(Header[Order Date] ,Dates[Date] )) )RETURN CALCULATE ( [Sales£M] ,USERELATIONSHIP(Header[Order Date] ,Dates[Date] ) , FILTER ( tableB, [SumMthYr] > 100000 ) )or an alternative isSumif6 =VAR tableB = summarizecolumns(Dates, Dates[MonthYear],Dates[Year], "SumMthYr",CALCULATE([Sales£M] ,USERELATIONSHIP(Header[Order Date] ,Dates[Date] )) )RETURN CALCULATE ( [Sales£M] ,USERELATIONSHIP(Header[Order Date] ,Dates[Date] ) , FILTER ( tableB, [SumMthYr] > 100000 ) )