How to add a "Date" where clause in an expression? - qlikview

I have a basic question as I am new to qlikview. I have the following expression which works.
=num((SUM({$<[SL Level]={'Test'}>}acceptable)/
(
SUM({$<[SL Level]={'Test'}>}callsoffered )
-SUM({$<[SL Level]={'Test'}>}outflowcalls)
-SUM({$<[SL Level]={'Test'}>}dequecalls)
-SUM({$<[SL Level]={'Test'}>}abncalls1)
-SUM({$<[SL Level]={'Test'}>}abncalls2)
-SUM({$<[SL Level]={'Test'}>}abncalls3)
-SUM({$<[SL Level]={'Test'}>}abncalls4)
)),'#.%')
I would to only return the result of the expression for Today()-1. Any suggestions on the syntax to retrieve the desired result?

if your date field is Date then the following expression should work:
num(
SUM({$<[SL Level]={'Test'},Date={$(=today()-1)}>}acceptable) /
SUM({$<[SL Level]={'Test'},Date={$(=today()-1)}>} rangesum(callsoffered,-outflowcalls,-dequecalls,-abncalls1,-abncalls2,-abncalls3,-abncalls4))
,'#.%')
or even shorter:
num(
SUM({$<
[SL Level]={'Test'},
Date={$(=today()-1)}>}
acceptable /
rangesum(callsoffered,-outflowcalls,-dequecalls,-abncalls1,-abncalls2,-abncalls3,-abncalls4)
)
,'#.%')

Related

SSAS Tabular - How to use FORMAT function in DAX summarize function

I have one Date column in my fact table, and due to some client api requirement I need to format this column as string while grouping data using SUMMARIZE function. Below is the sample query, which I am using:
EVALUATE(
CALCULATETABLE(
ADDCOLUMNS(
SUMMARIZE(
'BreakTable',
'BreakTable'[Column1],
'BreakTable'[Column2],
'BreakTable'[DateColumn1], --This needs to be formatted
),
"BreakCount",FORMAT('BreakTable'[BreakCount],"#,##0")
)
))
I have tried using FORMAT function in SUMMARIZE, and that does not work by default. I can not add new column to FactTable, so need to solve this while querying itself.
Is there any other way to achieve this? Any help is appreciated.
As per suggestion, adding more information.
We are using Sql Server 2014.
You could use the SELECTCOLUMNS() function. This function works similarly to the ADDCOLUMNS() function, except that it only returns the columns you specify.
Here is an example of how you can alter your existing query:
EVALUATE(
SELECTCOLUMNS(
SUMMARIZE(
'BreakTable',
'BreakTable'[Column1],
'BreakTable'[Column2],
'BreakTable'[DateColumn1],
),
"Column1", [Column1],
"Column2", [Column2],
"DateColumn1", FORMAT([DateColumn1],"YourFormatHere"), --Format your DateColumn here
"BreakCount",FORMAT('BreakTable'[BreakCount],"#,##0")
)
)
EDIT:
Please note that the SELECTCOLUMNS() function is only available from SQL Server 2016 and up.

The expression specified in the EVALUATE statement is not a valid table expression

I'm working on a Tabular cube in Visual Studio.
I have a DAX formula in the cube that works fine:
SUMX(FILTER(factFHA, factFHA[EventCd]="D"), [LoanCount])
When I run it in SSMS as:
evaluate(
SUMX(FILTER(factFHA, factFHA[EventCd]="D"), [LoanCount])
)
it fails with following error:
Query (1, 1) The expression specified in the EVALUATE statement is not a valid table expression.
I have 2 other formulas that both work fine:
evaluate(factFHA)
evaluate(filter('factFHA', [EventCd] = "D"))
I can't figure out what is wrong with the SUMX with FILTER
Please advise. Thank you.
EVALUATE function only works if you pass a table or an expression that returns a table, you are passing a SUMX function which return a scalar value (Decimal).
The syntax to write queries using DAX, is as follows:
[DEFINE { MEASURE <tableName>[<name>] = <expression> } -> Define a session (optional) measure
EVALUATE <table> --> Generate a table using your measures or creating calculated columns
[ORDER BY {<expression> [{ASC | DESC}]}[, …] --> Order the returned table by a passed column or expression
[START AT {<value>|<parameter>} [, …]]] --> This is an ORDER BY Sub-clause to define from which the query results will start.
Define your measure then use then use it inside the EVALUATE clause using a expression that evaluates to a table.
DEFINE
MEASURE factFHA[MyMeasure] =
SUMX ( FILTER ( factFHA, factFHA[EventCd] = "D" ), [LoanCount] )
EVALUATE
( SUMMARIZE ( FILTER ( factFHA, factFHA[EventCd] = "D" ), factFHA[AnyColumnToGroup]
, "Sum of MyMeasure", SUM ( factFHA[MyMeasure] ) ) )
Let me know if this helps.

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 )
)
)

multiplying modified colums to form new column

I want a query like:
select tp.package_rate, sum(sell) as sold_quantity , select(tp.package_rate * sold_quantity ) as sell_amount from tbl_ticket_package tp where tp.event_id=1001
here the system firing error while doing the multiplication as
sold_quantity is invalid column
another problem is that in multiplication I want to use package_rate which got by select query from tp.package_rate but it multiplying with all package_rate of the table but I want only specific package_rate which was output of select query
What would you suggest? I want to bind this query in gridview . is there any way to do it using ASP.net gridview?
Your problem is that you are referring to sold_quantity here :
select(tp.package_rate * sold_quantity )
The alias is not recognized at this point.You will have to replace it with sum(sales). You will also have to group by tp.package_rate.
Your query should ideally be like :
select tp.package_rate, sum(sell) as sold_quantity ,
(tp.package_rate * sum(sell) ) as sell_amount from tbl_ticket_package tp
where tp.event_id=1001 group by tp.package_rate;
I am guessing that tp.package_rate is unique for a given event_id, from latter part of your question. If that's not the case, the sql you have written makes no sense.

MDX Date Formatting

Can any one please tell me how to format date in MDX queries? We dont use SSRS to generate report ,we have our own customised reporting tool built on SSAS.Date filter sends date in yyyy/mm/dd format . As of now we dont have a date dimension. My date member looks like:
[CNB_DimSampleInfo].[COAReleasedON].&[2013-01-02T03:20:00].
How can I format date in STRTOmemeber? I have tried doing this. My question is how will the value coming from user suit my member format as below. I know ssrs does it easily but we are not using SSRS. Below is my Code
my code
SELECT
[Measures].[Result] ON COLUMNS
,NON EMPTY
{
[CNB_DimProduct].[ProductUcode].[ProductUcode].ALLMEMBERS*
[CNB_DimProduct].[ProductDesc].[ProductDesc].ALLMEMBERS*
[CNB_DimTest].[TestUcode].[TestUcode].ALLMEMBERS*
[CNB_DimTest].[TestName].[TestName].ALLMEMBERS*
[CNB_DimSampleInfo].[LotNo].[LotNo].ALLMEMBERS*
[CNB_DimSampleInfo].[BatchNo].[BatchNo].ALLMEMBERS*
[CNB_DimSampleInfo].[COAReleasedBy].[COAReleasedBy].ALLMEMBERS*
[CNB_DimSampleInfo].[COAReleasedON].[COAReleasedON].ALLMEMBERS*
[CNB_DimSampleInfo].[SampleReferenceNo].[SampleReferenceNo].ALLMEMBERS*
[CNB_DimSampleInfo].[AnalysedBy].[AnalysedBy].ALLMEMBERS*
[CNB_DimSampleInfo].[AnalysedOn].[AnalysedOn].ALLMEMBERS
} ON ROWS
FROM
(
SELECT
StrToMember
(
"[CNB_DimSampleInfo].[COAReleasedON].[" + Format("2013-01-02","yyyy MM")
+ "]:STRTOMember([CNB_DimSampleInfo].[COAReleasedON].["
+
Format
("2013-01-02"
,"yyyy MM"
)
+ "]"
) ON COLUMNS
FROM Cube001
);
There is also StrToSet which is better in your circumstance as you're using the : operator which returns a set:
...
...
FROM
(
SELECT
StrToSet
(
"[CNB_DimSampleInfo].[COAReleasedON].[" + Format("2013-01-02","yyyy MM")
+ "]:[CNB_DimSampleInfo].[COAReleasedON].["
+
Format
("2013-01-02"
,"yyyy MM"
)
+ "]"
,CONSTRAINED
) ON COLUMNS
FROM Cube001
);
does the following definitely return the date formatted the same as your key values?
Format("2013-01-02","yyyy MM")
Do your key values for dates look like this?...
[CNB_DimSampleInfo].[COAReleasedON].[2013 01]
Your date member
[CNB_DimSampleInfo].[COAReleasedON].&[2013-01-02T03:20:00]
looks like a bad candidate for a user date parameter, as it's precise down to the minute (perhaps the second). Unless the user exactly matches the time, they'll get nothing. But perhaps you're enforcing exact matches by using a LimitToList select list in the UI.
To get this member name, you can format the input string like this:
format([Your Input Parameter],'yyyy-MM-ddThh:mm:ss')
I have tried out using filter as below
SELECT
[Measures].[Result] ON COLUMNS
,NON EMPTY
{
filter([CNB_DimSampleInfo].[COAReleasedON].members,instr([CNB_DimSampleInfo].[COAReleasedON].currentmember.member_caption,"2013-01-02")>0 or instr([CNB_DimSampleInfo].[COAReleasedON].currentmember.member_caption, "2013-04-01")>0)
*[CNB_DimProduct].[ProductUcode].[ProductUcode].ALLMEMBERS*
[CNB_DimProduct].[ProductDesc].[ProductDesc].ALLMEMBERS*
[CNB_DimTest].[TestUcode].[TestUcode].ALLMEMBERS*
[CNB_DimTest].[TestName].[TestName].ALLMEMBERS
} ON ROWS
FROM Cube002