Skip to main content

Applycomparison advancd filter with a select statement

ApplyComparison ("#0 >= (select max(Hour)-1 from FACTTABLE where DAYDATE = current_date)", Hour@ID)
MicroStrategy’s strongest feature is it’s SQL Engine. The ability to define object relationships and then allow MicroStrategy to generate all of the appropriate SQL as you manipulate and drill is the core of the product. But of course, it can’t handle every since situation and you may sometimes need to help it along in certain instances. To extend this flexibility to you, MicroStrategy offers 5 functions which allow you to directly supply the SQL you want for a specific piece of the Query: ApplySimpleApplyAggApplyComparisonApplyOLAP and ApplyLogical. Today, I’ll talk about ApplyComparison, which allows you to provide custom SQL in the WHERE clause of the query.
Example Scenario

Say that you have a report that should compare Units Sold for Today vs Yesterday by Hour. If you were to build this report by simply adding the Hour attribute and two Conditional Metrics for Today Units Sold and Yesterday Units Sold, you’d end up with a graph like this:
external image tvy1.gifFictitious Data
It’s a nice picture of how the days are comparing, but it’s a little ugly since we don’t have data for 7am and ahead for today yet. Ideally, we’d like to filter off the data for Today that we haven’t received, but how do we know where we are? Assuming that we aren’t strictly real time and can’t assume based on the current time, we’d want to check the max Hour that exists in the table, and then go up to that minus 1. We could probably build that with some different types of metrics or a Report as Filter, but I’ll choose to do this using an ApplyComparison.
Before you start

First of all, you can only build this kind of filter in Desktop (so, not Web). You also need to enable the feature in Desktop, as it’s not available be default. Go to Tools->My Preferences->Filters and check the box for Show Advanced Qualifications. This will enable the option to use ApplyComparison’s in Filters.
Back to the Report

Double click on your Report Filter and you’ll now have the option to add an Advanced Qualification:
external image tvy4.gif
You can then provide the code for the ApplyComparison. The code you provide will go into the WHERE clause, and you can pass attribute values using the #n syntax, where n is the zero based number of parameters you’re passing. In my example, I’m passing a single parameter, so it’s #0. I could pass additional values by also including #1, #2, etc in my code if I needed them. Also note that when you provide the value at the end of the statement, it’s in the format Hour@ID, not just Hour. MicroStrategy changes the display of it slightly in my screenshot to Hour (ID), but the actual code you’ll be using looks like this:
ApplyComparison ("#0 >= (select max(Hour)-1 from FACTTABLE where DAYDATE = current_date)", Hour@ID)
external image tvy3.gif
And now the graph looks nice and clean:

external image tvy2-300x170.pngFictitious Data
BONUS TIP

Another way to accomplish this using ApplySimple instead of ApplyComparison would be to drag the Hour attribute to the Report Filter like normal, choose ID as the form and Greater Than or Equal To as the operator, and choose Custom from the drop down box and provide the ApplySimple code:
external image tvy5.gif
ApplySimple("(select max(Hour)-1 from FACTTABLE where DAYDATE = current_date)", 0)

Since we’re not including any parameters for this query, we can use 0 as the placeholder at the end to satisfy the function’s parameters.
This will generate the same SQL and results, but without needing to enable the Advanced Qualification option which could be confusing to some.

ApplyComparison("#0<=(select to_number(to_char(add_months(sysdate(),-1), 'yyyymm'))) ",[Instance Month]@ID)

Worked example:
select distinct a11.month_sid
from mstr_datamart.date_dim a11
where a11.date_sid<=(select to_number(to_char(add_months(sysdate(),-1), 'yyyymmDD'))) and a11.date_sid>=(select min(utc_date_sid) from mstr_datamart.ox_transaction_sum_daily_fact)
order by 1 desc

ApplyComparison("#0<=(select to_number(to_char(add_months(sysdate(),-1), 'yyyymmDD'))) and #0>=(select min(utc_date_sid) from mstr_datamart.ox_transaction_sum_daily_fact order by 1 desc)",[UTC Date]@ID)

Comments

Popular posts from this blog

Case functions Microstrategy

Ca se functions Microstrategy Case functions return specified data in a SQL query based on the evaluation of user-defined conditions. In general, a user specifies a list of conditions and corresponding return values. Case This function evaluates multiple expressions until a condition is determined to be true, then returns a corresponding value. If all conditions are false, a default value is returned.  Case  can be used for categorizing data based on multiple conditions. This is a single-value function. Syntax Case ( Condition1 ,  ReturnValue1 ,  Condition2 , ReturnValue2 ,...,  DefaultValue ) Example Case(([Total Revenue] < 300000), 0, ([Total Revenue] < 600000), 1, 2) sum(Case (Day@DESC in (“Sat”,”Sun”), Sales, 0) {~+} Sum(Case(Category@DESC In("Books","Electronics"),Revenue,0)){~+} CaseV (case vector) CaseV  evaluates a single metric and returns different values according to the results. It can be used to perform transf

Transaction Services - Configure Transactions

Configure Transactions in MSTR Web Transaction Services-enabled document displayed on an iPhone, iPad, or Android device can allow users to insert/update/delete data in to the database, using the options in the Configure Transactions Editor. To do so, you must link a Transaction Services report to a grid or to text fields in a panel stack. If the document is being displayed on an iOS device, you can link the report to the cells of a transaction table. Data from the input objects defined in the Transaction Services report is displayed in the grid, text fields, or cells for users to edit. Prerequisites:        Ø   You must have the Web Configure Transaction privilege assigned by MSTR user admin. Ø   Create the Transaction Services report (usually a grid report) you want to link to the grid, text fields, or transaction table cells. Make sure that the Transaction Services report must contain the input object for each value you want to allow users to change.  Ø   Ma
Microstrategy Release Types Platform release Interval:  Annually every twelve (12) months in December Who:  Entire customer base What:  Focus on production level security, stability, and performance defect fixes for all customers. Expectation:  Customer has chosen platform path and wants product stability without new enhancements. Support:  Three (3) years, patches for approved P1 defects, and regular hotfix cadence addresses critical defects. Feature Release Interval:  Quarterly every three (3) months Who:  Customers with specific feature requirements. What:  New functionality developed in close collaboration with customers and customer council. Expectation:  Customer has chosen feature path, will consume further feature releases. Support:  Six (6) months patch support for approved P1 defects and (eighteen) 18 months troubleshooting. Customers upgrade to next feature release for roll-up fixes. Why has MicroStrategy introduced “Platform” and “Feature

Sending an email in MSTR where the results of a report are in the email body as HTML content and a different report/document is an attachment to the same email in MicroStrategy

Is it possible to send an email using Distribution Services where the results of a report are in the email body as HTML content and a different report/document in MSTR? ANSWER: It is currently not possible to send an email using Distribution Services where the results of a report are in the email body as HTML content and a different report/document is an attachment to the same email in MicroStrategy 9.x. An enhancement request has been logged for this feature. ACTION: Contact Microstrategy Technical Support for an update on the enhancement, I have contacted but nobody knows where the request is  

Update the data on an Intelligent Cube without having to republish the entire cube in MicroStrategy

Update the data on an Intelligent Cube without having to republish the entire cube in MicroStrategy MicroStrategy has introduced a feature known as, Incremental Refresh Options, which allow Intelligent Cubes to be updated based on one or more attributes, by setting up incremental refresh settings to update the Intelligent Cube with only new data. This can reduce the time and system resources necessary to update the Intelligent Cube periodically versus a full republish. For example, if a user has an Intelligent Cube that contains weekly sales data, the user may want this Intelligent Cube to be updated at the end of every week with the sales data for that week. By setting up incremental refresh settings, he can make it so that only data for one week is added to the Intelligent Cube, without affecting the existing data and without having to reload all existing data. Users can select two types of objects for the incremental fetch: a report or

Multi-Table Data Import(MTDI) from one or more supported data sources

Multi-Table Data Import(MTDI) from one or more supported data sources In MicroStrategy Analytics Enterprise Web 10 onewards, users can now simultaneously import two or more tables from one or more supported data sources, this feature is called Multi-Table Data Import (MTDI) which has been renamed as Super Cubes in MSTR 2019 (Does it sound like multisourcing for all the users without admin help?) Currently, all connectors in MicroStrategy Web 10 except " OLAP " and " Search Engine Indices " support Multi-Table Data Import. Users are able to add multiple tables/files when doing data import from single connector, as shown below: Users are also able to combine multiple tables/files from different sources and store them into one single Intelligent Cube, as shown below:

Control the display of null and zero metric values

Show   Control the display of null and zero metric values in a grid report You can determine how to display or hide rows and columns in a grid report that consist only of null or zero metric values. You can have MicroStrategy hide the rows and columns in the following ways: Hide rows and columns that consist only of null metric values Hide rows and columns that consist only of zero metric values Hide rows and columns that consist only of null or zero metric values (default) Once you have defined how MicroStrategy hides null and zero metric values in the grid, you can quickly show or hide the grid using the Hide Nulls/Zeros option in the Data menu, as described below, or by clicking the  Hide Nulls/Zeros  icon  in the Data toolbar. To determine how null and zero metric values are displayed or hidden in a grid report Open the report in Edit mode. From the  Tools  menu, select  Report Options . The Report Options dialog box opens. To determine how

Stop a Report Services Document subscription from sending if no data is returned in MicroStrategy Web

Trick to Stop a Report Services Document subscription from sending if no data is returned in MicroStrategy Web The following steps are for stopping a Report Services Document subscription from sending if no data is returned: In MicroStrategy Web, edit or execute a report. Right-click on the metric header to apply the condition or threshold and select " Alerts ". Specify the condition " Is Not Null " to the metric for the delivery to be triggered in the filter editor as shown below. Expand the "Delivery Settings" section. Specify the desired delivery options including recipient address, subscription name, delivery format, compression options and the schedule to run the report and check the condition.  The subscription will be sent on the defined schedule only when data is returned in the Report Services Document.

Export a Report Services document to Excel with formatting using URL API

Export a Report Services document to Excel with formatting using URL API in MSTR Web In order to export a document in excel format using the URL API, the executionMode must be set to 4.  If excutionMode is not provided in the URL, by default PDF will be used as executionMode.   Below are the list of parameters that the URL must contain in order to execute correctly.   evt= 3069 src= Main.aspx.3069 executionMode= 4 documentID= 7E1644CA424F482DA811569FCE8127FF( Replace the document Id with your document ID)   Sample URL for .NET environment: http://WebServerName/MicroStrategy/asp/Main.aspx?evt=3069&src=Main.aspx. 3069 &executionMode= 4 &documentID= 7E1644CA424F482DA811569FCE8127FF    

Personalizing file locations, email and file subscriptions using macros in Microstrategy

Personalizing file locations MSTr allows to dynamically specify the  File Location  and  Backup File Location  in a file device using macros.  For example, if you specify the  File Location  as  C:\Reports\{&RecipientName}\ ,  all subscriptions using that file device are delivered to subfolders of  C:\Reports\ . Subscribed reports or documents for each recipient are delivered to a subfolder with that recipient’s name, such as  C:\Reports\Jane Smith\  or  C:\Reports\Hiro Protagonist\ . The table below lists the macros that can be used in the  File Location  and  Backup File Location  fields in a file device: Description Macro Date on which the subscription is sent {&Date} Time at which the subscription is sent {&Time} Name of the recipient {&RecipientName} User ID (32-character GUID) of the recipient {&RecipientID} Distribution Services address that the subscription is delivered to {&AddressName} File path that a