Forum Discussion
Calculate one Rowbased DAX Measure from two virtual Tables
- Anonymous7 years ago
Hi there.
I had to create a quick model and changed the table names so please "undo" them to fit your tables' names. Here's the code and it does work now.
My Measure = VAR __onePitASelected= HASONEFILTER( 'Calendar A'[date] ) VAR __onePitBSelected = HASONEFILTER( 'Calendar B'[Date] ) var __shouldCalculate = __onePitASelected && __onePitBSelected VAR __pitAProducts = VALUES( Products[Product] ) VAR __pitBProducts = CALCULATETABLE( VALUES( Products[Product] ), USERELATIONSHIP( Products[Date], 'Calendar B'[Date] ),
-- We have to take off any filters that exist
-- on 'Calendar A' because these are two different
-- calendars and activating the second relationship
-- does NOT deactivate the first one because the
-- relationships refer to TWO DIFFERENT TABLES, not
-- the same one. ALL( 'Calendar A' ) ) var __productsInBoth = INTERSECT( __pitAProducts, __pitBProducts ) var __avgAcrossProducts = AVERAGEX( __productsInBoth, var __priceA = CALCULATE( MAX( Products[Price] ) ) var __priceB = CALCULATE( MAX( Products[Price] ), USERELATIONSHIP( Products[Date], 'Calendar B'[Date] ), ALL( 'Calendar A' ) ) return __priceB - __priceA ) return if( __shouldCalculate, __avgAcrossProducts )The lesson to take from this is that when you want your question answered quickly and correctly it's best to supply a file with some example data and formulas so that others have something to work with and can see a tangible and concrete model.
By the way, is it necessary to have one of the relationships disabled? You could leave it as active and the model would still work OK (but you'd need to make some small changes to the code above, of course).
Best
Darek
[Your Measure] := VAR __onePitASeleced= HASONEVALUE( TimeA[date]) ) VAR __onePitBSelected = HASONEVALUE( TimeB[date]) ) var __shouldCalculate = __onePitASelected && __onePitBSelected VAR __pitAProducts = VALUES( Basic[product] ) VAR __pitBProducts = CALCULATETable( VALUES( Basic[product] ), USERELATIONSHIP( Basic[date], TimeB[date] ) ) var __productsInBoth = INTERSECT( __pitAProducts, __pitBProducts ) var __avgAcrossProducts = AVERAGEX( __productsInBothPits, var __priceA = CALCULATE( MAX( Basic[price] ) ) var __priceB = CALCULATE( MAX( Basic[price] ), USERELATIONSHIP( Basic[date], TimeB[date] ) ) return __priceB - __priceA ) return if( __shouldCalculate, __avgAcrossProducts )
Thanks for your responce. I tried your code but i don't get any results on the Front End, even if i select two perfect matching days.
I modified your skript a litte because i got some Syntax errors.
test = VAR __onePitASeleced= HASONEVALUE( TimeA[date]) VAR __onePitBSelected = HASONEVALUE( TimeB[date]) var __shouldCalculate = __onePitASeleced && __onePitBSelected VAR __pitAProducts = VALUES( Basic[productname] ) VAR __pitBProducts = CALCULATETable( VALUES( Basic[productname] ); USERELATIONSHIP( Basic[date]; TimeB[date] ) ) var __productsInBoth = INTERSECT( __pitAProducts; __pitBProducts ) var __avgAcrossProducts = AVERAGEX( __productsInBoth; var __priceA = CALCULATE( MAX( Basic[price] ) ) var __priceB = CALCULATE( MAX( Basic[price] ); USERELATIONSHIP( Basic[date]; TimeB[date] ) ) return __priceB - __priceA ) return if( __shouldCalculate; __avgAcrossProducts )
Is this probably because __pitBProducts don't get any valid data?
- Anonymous7 years agoNot applicable
Hi there.
I had to create a quick model and changed the table names so please "undo" them to fit your tables' names. Here's the code and it does work now.
My Measure = VAR __onePitASelected= HASONEFILTER( 'Calendar A'[date] ) VAR __onePitBSelected = HASONEFILTER( 'Calendar B'[Date] ) var __shouldCalculate = __onePitASelected && __onePitBSelected VAR __pitAProducts = VALUES( Products[Product] ) VAR __pitBProducts = CALCULATETABLE( VALUES( Products[Product] ), USERELATIONSHIP( Products[Date], 'Calendar B'[Date] ),
-- We have to take off any filters that exist
-- on 'Calendar A' because these are two different
-- calendars and activating the second relationship
-- does NOT deactivate the first one because the
-- relationships refer to TWO DIFFERENT TABLES, not
-- the same one. ALL( 'Calendar A' ) ) var __productsInBoth = INTERSECT( __pitAProducts, __pitBProducts ) var __avgAcrossProducts = AVERAGEX( __productsInBoth, var __priceA = CALCULATE( MAX( Products[Price] ) ) var __priceB = CALCULATE( MAX( Products[Price] ), USERELATIONSHIP( Products[Date], 'Calendar B'[Date] ), ALL( 'Calendar A' ) ) return __priceB - __priceA ) return if( __shouldCalculate, __avgAcrossProducts )The lesson to take from this is that when you want your question answered quickly and correctly it's best to supply a file with some example data and formulas so that others have something to work with and can see a tangible and concrete model.
By the way, is it necessary to have one of the relationships disabled? You could leave it as active and the model would still work OK (but you'd need to make some small changes to the code above, of course).
Best
Darek
- Anonymous7 years agoNot applicable
Please send me a link to your .pbix file with the model and data. You can share it via GoogleDrive, OneDrive or Dropbox... or whatever. I authored the measure without any model, just wrote it and have not actually executed it.
Once I have the file, I'll be able to give you a fully working solution.
Thanks.
Best
Darek