Forum Discussion
Dax query returns unexpected result
- 5 years ago
Hi Usagi_Nakamura ,
Answering to your last question:
Not that I am sure this is Auto-Exist anymore but in the example above, where you exchanged Product[UnitPrice] by Sales[UnitPrice] you were still having all the ingredients to trigger it according to this:
https://docs.microsoft.com/en-us/dax/all-function-dax
"An example where auto-exist and ALL() provide unexpected results is when filtering on two or more columns of the same table (like when using slicers), and there is a measure on that same table that uses ALL()."In your last example ProductSubcategoryKey (ALL), ClassName and BrandName (SLICERS) are still from the same table. Now, while moving ClassName and BrandName to a different table could sort out the issue, we still want to learn why this is happening.
I leave it to the experts, but hoping this is of any help, I reproduced the scenario in PBI with the simplest table possible (just two rows, only one brand/class and no duplicates) to rule out possible causes:
The measure that breaks:
SUMX = VAR CurrentKey = SELECTEDVALUE('Table'[Key]) RETURN SUMX( FILTER( ALL('Table'[Key]); 'Table'[Key]<CurrentKey ); [Sum of UnitPrice] )A measure that works:
With Calculate = VAR CurrentKey = SELECTEDVALUE('Table'[Key]) RETURN CALCULATE( [Sum of UnitPrice]; 'Table'[Key]<CurrentKey )In PBI:
- A Table visualization with only one of the columns used to slice shown:
- Ok to slice by the one not present in the visualization:
- But as soon you slice by both of them the measure with SUMX breaks:
Regards,
Hi AllisonKennedy ,
Thank you very much for your kind suggestions!
Actually I knew that with your query, it work well.
In addition, your query, even if without line #63, can get same result.
In fact, my query was originally matrix visual and SumUnitPrice and AccUnitPrice were calculated measures and BrandName filter was coming from a Slicer visual. The query is made from such visuals, using power bi perfomance analyzer.
Moreover, for my query, I tried to change SUMX to CONCATENATEX and found that with or without line #7, same value is returned in every row. These values should be items to be calculated in SUMX version. But if I change again to Sumx, return values are different between with line #7 and without line #7.
<< With line #7>>
<<Without line #7>>
I tried to find out why it is possible but in vain for three months...
Thanks.
- Payeras_BI5 years ago
Solution Sage
Hi Usagi_Nakamura ,
From your last explanation this behaviour seems to respond to what AlbertoFerrari explains here https://www.sqlbi.com/articles/understanding-dax-auto-exist/
Regards,
- AllisonKennedy5 years ago
Community Champion
Usagi_Nakamura I see your problem now, interesting that CONCATENATEX behaves differently than SUMX. To be honest, this query is multi-layered and not best practice optimised, so I would change it, but I am fascinated to learn from it. It's definitely helping me understand better some of the behaviour, and I have run into scenarios where SUMX doesn't behave as I expected and this might give insight to why.
Just for fun, here are some additional examples to add to your scenario. It seems COUNTROWS behaves as CONCATENATEX, while COUNTX behaves as SUMX. Try also commenting out the TREATAS that uses brand name and note that this also fixes things. The problem arises when you have a filter/relationship on a column that does not also exist in the table, and then try to use aggregate iterators:
1 = SUMMARIZECOLUMNS('Product'[ProductSubcategoryKey], 'Product'[ClassName]--, 'Product'[BrandName],TREATAS({"Contoso"}, 'Product'[BrandName]),treatas({"Regular"}, 'Product'[ClassName]), "SumUnitPrice", CALCULATE(SUM('Product'[UnitPrice])), "AccUnitPrice",VAR CurrentSubKey = VALUES('Product'[ProductSubcategoryKey])RETURNSUMX(FILTER(ALL('Product'[ProductSubcategoryKey]),'Product'[ProductSubcategoryKey]<CurrentSubKey),CALCULATE(SUM('Product'[UnitPrice]))), "ConcateUnitPrice",VAR CurrentSubKey = VALUES('Product'[ProductSubcategoryKey])RETURNCONCATENATEX(FILTER(ALL('Product'[ProductSubcategoryKey]),'Product'[ProductSubcategoryKey]<CurrentSubKey),CALCULATE(SUM('Product'[UnitPrice])), ";"), "SubCategory", VALUES('Product'[ProductSubcategoryKey]), "Count Unit Price",VAR CurrentSubKey = VALUES('Product'[ProductSubcategoryKey])RETURNCOUNTX(FILTER(ALL('Product'[ProductSubcategoryKey]),'Product'[ProductSubcategoryKey]<CurrentSubKey),CALCULATE(SUM('Product'[UnitPrice]))), "CountRows", VAR CurrentSubKey = VALUES('Product'[ProductSubcategoryKey])RETURNCOUNTROWS(FILTER(ALL('Product'[ProductSubcategoryKey]),'Product'[ProductSubcategoryKey]<CurrentSubKey)), "SUMX",VAR CurrentSubKey = SELECTEDVALUE('Product'[ProductSubcategoryKey])RETURNSUMX(FILTER(ALL('Product'[ProductSubcategoryKey]),'Product'[ProductSubcategoryKey]<CurrentSubKey),CALCULATE(SUM('Product'[UnitPrice]))))