is it possible to store a table into a DAX variable conditionally - dynamic

I'd like to store a table in a variable, but based on conditions of the visual.
e.g.
VAR ColumnValues = values( SpecificTable[SpecificColumn] )
works fine, but what I'd like to do is:
VAR ColumnValues = if([some condition T/F], values( SpecificTable1[SpecificColumn1] ) , SpecificTable2[SpecificColumn2] )
For reference, this question is in exploration of workarounds to solve question: Dynamic measure that responds to dynamic dimension which I marked as answered prematurely. I still do not have a solution to dynamically work with column values in DAX.
I've not been able to work out a syntax that allows this. Switch only returns scalar strings, and IF seems to only allow for a scalar result, not a table. Any other options I'm not thinking of?

Was not explicitly using any condition, but the condition that I was checking for, that I was able to get the desired result with the following:
Create Field Parameter (name it "_Dimension"), selecting the columns that need to be in play in the DAX
DAX looks like this:
VAR SelectedDim = SELECTEDVALUE( _Dimension[_Dimension Fields] ) //fully qualified - created by field parameter
//stage the values in each of the columns available
VAR Dim1Values = ADDCOLUMNS( VALUES( Dim1[Column1] ) , "RowValue" , Dim1[Column1] , "ColumnName" , "'Dim1'[Column1]" )
VAR Dim2Values = ADDCOLUMNS( VALUES( Dim1[Column2] ) , "RowValue" , Dim1[Column2] , "ColumnName" , "'Dim1'[Column2]" )
//... same pattern, as many column as needed
VAR SelectedDimValues = FILTER( UNION( Dim1Values, Dim2Values ) , [RowValue] = SelectedDim ) //return the values just for the selected column
SelectedDimValues is a Variable that contains a table with the rows from my selected dimension.

Related

How to use NVL or COALESCE in native query for list of values in Spring Data JPA

I am using native query as follows which fetches data from multiple tables.
I also want to perform filtering on it based on users input. User can select either none, one or multiple checkboxes in filter menu based on which my query should return the record.
I have tried both the approaches like:
src_region.region_id NVL(:regionIds,src_region.region_id )
src_region.region_id in(:regionIds) OR COALESCE (:regionIds)IS NULL)
But for both of them I am getting errors as I am sending List values.
public interface dbRepository
{
public static final String dbQuery = "select * from
(select src_data.*,
trg_data.*
from
(select reg.region_id,reg.region_name,
ctry.country_id,ctry.country_name
from src_region reg,src_ctry ctry
where reg.region_id=ctry.region_id
AND src_region.region_id in(:regionIds) OR COALESCE (:regionIds)IS NULL) src_data,
(select md.module_id,reg.module_name,
ctry.country_id,ctry.country_name
from trg_module md,trg_ctry ctry
where md.module_id=ctry.country_id
AND ctry.country_id in(:countryIds) OR COALESCE (:countryIds)IS NULL) trg_data
ORDER BY src_data.region_id ";
#Query( value = dbQuery, nativeQuery = true )
List<Object[]> findBySorceOrTarget( #Param( "regionIds" ) List<Long> regionIds, #Param( "countryIds" ) List<Long> countryIds );
}
User may on may not send values. Also, rather than sending multiple values, they can also send only one value. What is the better way to pass list of values to parameters in native query?
Generally speaking, that should be
AND (src_region.region_id = :regionIds or :regionIds IS NULL)
However, if you can select multiple values, then = won't be OK - you'll have to switch to in (as code you posted suggests). Though, you can't do it that way as you'll get nothing as a result - you'll have to split those values into rows.
Something like this (presuming that values in :regionIds are comma-separated, such as 1,2,5:
and (src_region.region_id in (select regexp_substr(:regionIds, '[^,]+', 1, level)
from dual
connect by level <= regexp_count(:regionIds, ',') + 1
)
or :regionIds is null)

DAX SUMX: Storing a filtered table in a VAR and reference its columns later on in the expression

In a measure (SUMX) I am filtering a table and storing it in a variable.
var currency = factFx[ALPHABETIC_CURRENCY_1]
var fxRates = FILTER(
factMarketDataExchangeRates;
factMarketDataExchangeRates[FX_CURRENCY] = currency
)
Then I need to do calcs that include further filtering fxRates
var exchangeRateOnTradeDate = CALCULATE(
[Measure];
FILTER(
fxRates;
fxRates[CURVE_DATE] = tradeDate
)
)
This throws an error in SSDT Cannot find table fxRates
Also It appears intellisense is not working. But each of the following does work. But is this expected behaviour?
Without table prefix:
var exchangeRateOnTradeDate = CALCULATE(
[Measure];
FILTER(
fxRates;
[CURVE_DATE] = tradeDate
)
)
With the underlying table's prefix:
var exchangeRateOnTradeDate = CALCULATE(
[Measure];
FILTER(
fxRates;
factMarketDataExchangeRates[CURVE_DATE] = tradeDate
)
)
Yes, this is the expected behavior. You can only use the table[COLUMN] syntax for tables in your data model.
Both of your working versions are equivalent to substituting in the definition of fxRates.
var currency = factFx[ALPHABETIC_CURRENCY_1]
var exchangeRateOnTradeDate =
CALCULATE (
[Measure];
FILTER (
FILTER (
factMarketDataExchangeRates;
factMarketDataExchangeRates[FX_CURRENCY] = currency
);
factMarketDataExchangeRates[CURVE_DATE] = tradeDate
)
)
Since [CURVE_DATE] ultimately derives from factMarketDataExchangeRates, using that table prefix is really what's happening under the hood, but you are allowed to use the other version where that table is abstracted away and doesn't clutter your code.
The important thing to remember is that the fxRates variable isn't actually a table in this case, but rather a semantic trick to write code more legibly.

Linq Query in VB

Good Day,
I am querying my database using Linq and I have run into a problem, the query searched a column for a search phrase and based on if the column has the phrase, it then returns the results, The query is below,
Dim pdb = New ProductDataContext()
Dim query =
From a In pdb.tblUSSeries
Join b In pdb.tblSizes_ On a.Series Equals b.Series
Where
a.Series.ToString().Equals(searchString) Or
b.Description.Contains(searchString) Or Not b.Description.Contains(Nothing)
Order By b.Series, b.OrderCode Ascending
Select New CustomSearch With
{
.Series = a.Series,
.SeriesDescription= a.Description,
.Coolant = a.Coolant,
.Material = a.Material,
.Standard = a.Standard,
.Surface = a.Surface,
.Type = a.Type,
.PointAngle = a.PointAngle,
.DiaRange = a.DiaRange,
.Shank = b.Shank,
.Flutes = b.Flutes,
.EDPNum = b.EDPNum,
.SizesDescription = b.Description,
.OrderCode = b.OrderCode
}
Return query
I think the problem is that, in the table certain rows are NULL, so when it is checking the column for the phrase and it encounters a row that is null it, breaks and returns this error,
The cast to value type 'System.Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
I have ran this query against another column that has all the rows populated with data and it returns the results ok.
So my question is how can I write it in VB to query the db with the supplied searchstring and return the results, when some of the rows in the columns have null values.
Any help would be great.
The exception occurs when you make the projection (i.e. select new CustomSearch)
And yes your trying to assign Null to some int property
(Not sure which one of your properties that is)
one of 2 choices :
1) Use nullalbe types for your properties (or just that one property).
2) project with an inline If ( ?? in C#) , I don't know VB so don't catch me on the syntax.
Taking Series just as an example i don't know if it's an int or if that's the problematic property
Select New CustomSearch With
{
.Series = If(a.Series Is Nothing,0, CInt(a.Series))
}
In C#
Select new CustomSearch
{
Series = a.Series ?? 0;
}

Using condition in Calculatetable ()

I've a problem on Table filtering while using CALCULATETABLE()
I tried to use the script with condition for CALCULATETABLE():
XeroInvoices[AmountPaid] < XeroInvoices[AmountDue]
EVALUATE
SUMMARIZE(
CALCULATETABLE(XeroInvoices,
XeroInvoices[Status] = "AUTHORISED",
XeroInvoices[DueDate] <= TODAY(),
XeroInvoices[AmountPaid] < XeroInvoices[AmountDue]
),
XeroInvoices[Number],
XeroInvoices[Reference],
XeroInvoices[Status],
XeroInvoices[Date],
XeroInvoices[DueDate],
XeroInvoices[AmountPaid],
XeroInvoices[AmountDue]
)
but the error that i get in DAX Studio is as following:
Query (6, 30) The expression contains multiple columns, but only a single column can be used in a True/False expression that is used as a table filter expression.
I managed only to kinda achieve that I wanted only like this -- crating new column within SUMMARIZE() syntax and later filtering it in Excel:
EVALUATE
SUMMARIZE(
CALCULATETABLE(XeroInvoices,
XeroInvoices[Status] = "AUTHORISED",
XeroInvoices[DueDate] <= TODAY()
),
XeroInvoices[Number],
XeroInvoices[Reference],
XeroInvoices[Status],
XeroInvoices[Date],
XeroInvoices[DueDate],
XeroInvoices[AmountPaid],
XeroInvoices[AmountDue],
"AmPaid<AmDue",XeroInvoices[AmountPaid]< XeroInvoices[AmountDue]
)
Does anyone know what might be the reason for this Err in CALCULATETABLE() and what might be a proposed solution?
Thanks!!
Check this
To filter by multiple columns you have to explicitly specify the "FILTER"
CALCULATETABLE (
Product,
FILTER (
Product,
OR ( Product[Color] = "Red", Product[Weight] > 1000 )
)
)

NHibernate native SQL in WHERE clause

I have a Geography column in a table in SQL Server and would like to filter rows with a specific geometry type, e.g. all records where geometry type is 'Point'
The SQL query would look like
select * from GeometryTable g where g.Geography.STGeometryType() = 'Point'
How can I create a criteria for that? The criteria is going to be used with other criterias
criteria.Add(Restrictions.Add(<Geography.STGeometryType()>, some.Value)
Thanks
Use this syntax:
var criteria = session.CreateCriteria<Geometry>();
criteria.Add
(
Expression.Sql(" {alias}.[Geography].STGeometryType() = ? "
, "Point" // a place for your parameter
, NHibernate.NHibernateUtil.String)
);
var list = criteria.List<Geometry>();