I would like to do something like this (tsql):
select saldi
where rek in (4,8)
and rek5 <> 41111
and rek3 not in (a,b,c)
Following MDX, filter (4,8) <> 41111, is not working. If I remove the 41111 filter the measure works fine, just shows 4/8. Adding 41111 as a '<>' filter and the result is blank, not even 0.00.
THIS = IIF(
( [Rekeningen].[Rek n1].CurrentMember = [Rekeningen].[Rek n1].&[4]
or [Rekeningen].[Rek n1].CurrentMember = [Rekeningen].[Rek n1].&[8] )
and [Rekeningen].[Rek n5].CurrentMember <> [Rekeningen].[Rek n5].&[41111]
,
[Measures].[Saldi], 0.00
);
I tried Google, but don't seem to get the keyword right. On social.msdn.microsoft.com a lot of reactions are marked as answer, but not for me this far.
Deploying the ssas cube works fine, no errors.
Can anybody point me in the right direction? I tried Except, but that command doesn't seem to work with multile fields(rek3/rek5).
IS and NOT IS are used when comparing members. That gives us this:
THIS =
IIF
(
(
[Rekeningen].[Rek n1].CurrentMember IS [Rekeningen].[Rek n1].&[4]
OR
[Rekeningen].[Rek n1].CurrentMember is [Rekeningen].[Rek n1].&[8]
)
AND
[Rekeningen].[Rek n5].CurrentMember IS NOT [Rekeningen].[Rek n5].&[41111]
,[Measures].[Saldi]
,0
);
Rather than 0 you should nearly always use null in cube script calcs or you'll kill performance:
THIS =
IIF
(
(
[Rekeningen].[Rek n1].CurrentMember IS [Rekeningen].[Rek n1].&[4]
OR
[Rekeningen].[Rek n1].CurrentMember is [Rekeningen].[Rek n1].&[8]
)
AND
[Rekeningen].[Rek n5].CurrentMember IS NOT [Rekeningen].[Rek n5].&[41111]
,[Measures].[Saldi]
,NULL
);
I'm not convinced about these member names? [Rekeningen].[Rek n1].CurrentMember and [Rekeningen].[Rek n1].&[4]. Let me add the two alternatives that I think they might be:
1.
THIS =
IIF
(
(
[Rekeningen].[Rek n1].CurrentMember IS [Rekeningen].[Rek n1].[Rek n1].&[4]
OR
[Rekeningen].[Rek n1].CurrentMember is [Rekeningen].[Rek n1].[Rek n1].&[8]
)
AND
[Rekeningen].[Rek n5].CurrentMember IS NOT [Rekeningen].[Rek n5].[Rek n5].&[41111]
,[Measures].[Saldi]
,NULL
);
or
2.
THIS =
IIF
(
(
[Rekeningen].CurrentMember IS [Rekeningen].[Rek n1].&[4]
OR
[Rekeningen].CurrentMember is [Rekeningen].[Rek n1].&[8]
)
AND
[Rekeningen].CurrentMember IS NOT [Rekeningen].[Rek n5].&[41111]
,[Measures].[Saldi]
,NULL
);
Related
I have defined the following table :
The following measure does not work as expected :
VAR lt_MAX_DATE =
FILTER(
ALL( dim_CALENDAR[DATE] ) ,
dim_CALENDAR[DATE] = MAX( dim_CALENDAR[DATE] )
)
VAR lv_MAX_YEAR_DAY =
CALCULATE(
VALUES( dim_CALENDAR[YEAR_DAY] ) ,
ALL( dim_CALENDAR ) ,
lt_MAX_DATE
)
RETURN
lv_MAX_YEAR_DAY
as I get :
In my mind, I should get 10 for both rows of the Max Date ALL column, but this is not the case.
My question is : why does a dim_CALENDAR[YEAR] context apply even though I have used the ALL() function?
Thank you for your help.
If you want the DAX measure to return the max Year_day of maxYear by ignoring no matter what year is currently visible in the filter context, you can achieve it in many ways. Two of them are as below.
Measure1 =
VAR mxYr =
CALCULATE ( MAX ( dim_Calendar[Year] ), ALL ( dim_Calendar[Year] ) )
RETURN
CALCULATE (
MAX ( dim_Calendar[Year_day] ),
dim_Calendar[Year]=mxYr
)
Measure2 =
VAR mxYr =
CALCULATE ( MAX ( dim_Calendar[Year] ), ALL ( dim_Calendar[Year] ) )
RETURN
CALCULATE (
MAX ( dim_Calendar[Year_day] ),
TREATAS ( { mxYr }, dim_Calendar[Year] )
)
This expression
VAR lt_MAX_DATE =
FILTER(
ALL( dim_CALENDAR[DATE] ) ,
dim_CALENDAR[DATE] = MAX( dim_CALENDAR[DATE] )
)
is the same as
VAR YEARinMatrixRow = SELECTEDVALUE[dim_CALENDAR[YEAR]]
VAR lt_MAX_DATE =
CALCULATETABLE (
FILTER(
ALL( dim_CALENDAR[DATE] ) ,
dim_CALENDAR[DATE] = MAX( dim_CALENDAR[DATE] )
)
,dim_CALENDAR[YEAR] = YEARinMatrixRow
)
So, the result of the expression is the last date of the selected (in Row) year. You get a Row filtering.
The second expression filters the table by
ALL( dim_CALENDAR )
,lt_MAX_DATE
According to DAX priorities, first works ALL(dim_CALENDAR) , then you apply filter - lt_MAX_DATE
So, you get the dim_CALENDAR table, simply filtered by the last day of the year in a matrix row where you get a single value with the VALUES( dim_CALENDAR[YEAR_DAY] ).
You overite ALL( dim_CALENDAR ) by the - lt_MAX_DATE
Normaly, you will get an error with lv_MAX_YEAR_DAY syntax. Calculate returns scalar value, while VALUES() function returns a table even with 1 cell.
As #Mik says, you are restricting the context with
dim_CALENDAR[DATE] = MAX( dim_CALENDAR[DATE] )
I'm working on a ranking/scoring system and I'm missing the PERCENTRANK.INC function in powerBI. Instead I have worked out below formula which is the closest I can get.
Score =
DIVIDE (
RANKX (
FILTER ( 'Table', NOT ( ISBLANK ( [Sold amounts] ) ) ),
[Sold amounts],
,
ASC
) - 1,
COUNTROWS ( FILTER ( 'Table', NOT ( ISBLANK ( [Sold amounts] ) ) ) ) - 1
)
I really want to have the formula to take the type of "Fruit" into account in my scoring/ranking.
In short each fruit should be scored separately, with a range per fruit sold.
Could this somehow be done with a variable (VAR)?
Example of data:
This should work.
Score =
VAR fruit = 'Table'[Fruit]
VAR filteredTable = FILTER ( 'Table', NOT ( ISBLANK ( [Sold amount] ) ) && 'Table'[Fruit] = fruit)
RETURN
DIVIDE (
RANKX (
filteredTable,
[Sold amount],
,
ASC
) - 1,
COUNTROWS ( filteredTable ) - 1
)
I want to have a column in power bi showing the growth rate of sales. I have a table like
year
count
1395
123
1396
232
1397
23
1398
908
1399
678
1400
34
the growth rate is (this year - previous year)/previous year
could you please guide me how I can do this?
When I use the growth the data is like below
You can add a calculated column like this:
growth =
VAR _currentcount = 'Table'[count]
VAR _currentyear = 'Table'[year]
VAR _previouscount =
CALCULATE (
SELECTEDVALUE ( 'Table'[count] ) ,
ALL ( 'Table' ) ,
'Table'[year] = _currentyear - 1
)
RETURN
IF (
NOT ISBLANK ( _previouscount ) ,
DIVIDE ( _currentcount , _previouscount ) - 1
)
or a measure like this, to be used with your year dimension:
growth_measure =
VAR _currentcount = SELECTEDVALUE ( 'Table'[count] )
VAR _currentyear = SELECTEDVALUE ( 'Table'[year] )
VAR _previouscount =
CALCULATE (
SELECTEDVALUE ( 'Table'[count] ) ,
ALL ( 'Table' ) ,
'Table'[year] = _currentyear - 1
)
RETURN
IF (
NOT ISBLANK ( _previouscount ) ,
DIVIDE ( _currentcount , _previouscount ) - 1
)
Giving this result:
All depending on your needs.
SELECT EMPLID
, LST_ASGN_START_DT
FROM PS_JOB A
WHERE A.EFFDT = (
SELECT MAX(EFFDT)
FROM PS_JOB
WHERE A.EMPLID = EMPLID
AND A.EMPL_RCD = EMPL_RCD
AND EFFDT <= SYSDATE )
AND A.EFFSEQ = (
SELECT MAX(EFFSEQ)
FROM PS_JOB
WHERE A.EMPLID = EMPLID
AND A.EMPL_RCD = EMPL_RCD
AND A.EFFDT = EFFDT )
AND HR_STATUS='A'
When running it directly on the db i get no errors but when i compile and build it on application designer I get an error of "invalid number of column names specified "
I have also tried this but still getting the same error.
SELECT A.EMPLID
, A.LST_ASGN_START_DT
FROM PS_JOB A
WHERE A.EFFDT = (
SELECT MAX(EFFDT)
FROM PS_JOB
WHERE A.EMPLID = EMPLID
AND A.EMPL_RCD = EMPL_RCD
AND EFFDT <= SYSDATE )
AND A.EFFSEQ = (
SELECT MAX(EFFSEQ)
FROM PS_JOB
WHERE A.EMPLID = EMPLID
AND A.EMPL_RCD = EMPL_RCD
AND A.EFFDT = EFFDT )
AND A.HR_STATUS='A'
and this also but nothing..
SELECT A.EMPLID
, A.LST_ASGN_START_DT
FROM PS_JOB A
WHERE A.EFFDT = (
SELECT MAX(EFFDT)
FROM PS_JOB B
WHERE A.EMPLID = B.EMPLID
AND A.EMPL_RCD = B.EMPL_RCD
AND B.EFFDT <= SYSDATE )
AND A.EFFSEQ = (
SELECT MAX(EFFSEQ)
FROM PS_JOB C
WHERE A.EMPLID = C.EMPLID
AND A.EMPL_RCD = C.EMPL_RCD
AND A.EFFDT = C.EFFDT )
AND A.HR_STATUS='A'
You have two columns on your select, and probably have a different number of columns on the record definition.
The sql statement and the record definition should have the same number of columns (but not necessarily the same field names)
Look at this example:
record has 5 fields
and select has 5 fields:
I have a sql query which is performing badly, It is causing table scans / IO spikes. Please check the below script and the execution plan and let me know if it requires any indexes or refactoring of the query itself. Thanks
--Query
SELECT TOP 20
CustomerPrimaryExtID,
Max(POSTimeStamp) AS TransactionDate,
ExtLocationCode,
0 AS RedemptionAmount,
0 AS RedemptionCount,
TerminalNum,
LogixTransNum,
POSTransNum AS TransNum,
0 AS DetailRecords,
CustomerTypeID,
PresentedCustomerID,
PresentedCardTypeID,
HHID,
Replayed,
0 AS TransContext,
ISNULL(TransTotal, 0) AS TransTotal
FROM TransHist AS TH WITH(nolock)
WHERE
(
(
( CustomerPrimaryExtID IN ( '' ) AND HHID IS NULL )
OR HHID = '0000000250000007320' AND CustomerTypeID <> 1
) OR ( CustomerPrimaryExtID = '0000000250000007320' AND CustomerTypeID = 1 )
)
AND NOT EXISTS
(
SELECT
LogixTransNum
FROM TransRedemptionView AS TR2
WHERE
(
( ( CustomerPrimaryExtID IN ( '' ) AND HHID IS NULL ) OR HHID = '0000000250000007320' AND CustomerTypeID <> 1 )
OR ( CustomerPrimaryExtID = '0000000250000007320' AND CustomerTypeID = 1 )
)
AND TH.LogixTransNum = TR2.LogixTransNum
)
GROUP BY
CustomerPrimaryExtID,
HHID,
CustomerTypeID,
PresentedCustomerID,
PresentedCardTypeID,
LogixTransNum,
POSTransNum,
TerminalNum,
ExtLocationCode,
Replayed,
TransTotal
ORDER BY TransactionDate DESC