Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Wiki Markup
{style}#com-atlassian-confluence .wiki-content td {
border: none;
}
{style}
Wiki Markup
{align:justified}
If you have not yet read them then please start with [overview of Standard-BI concepts|http://services.burti.lv/confluence/display/BI/1.+Overview] and [analyze and create reports tutorial|http://services.burti.lv/confluence/display/BI/2.+Analyze+and+create+reports]. This tutorial will explain how to define new calculated members using MDX calculation formulas.

Each cube *dimension* contains one or more *hierarchies* with one or more hierarchy *levels*. And each hierarchy level contains dimension *members*. There are _normal_ members which are imported from source files or applications as well as you can define *calculated members* using calculation formulas. Calculation formulas are defined using [MDX query language|http://mondrian.pentaho.com/documentation/mdx.php].

There is special dimension *Measures* which contains all imported *measures*. Most frequently calculated members are defined in *Measures* dimension and then they are typically called as *calculated measures*.
{align}

Define new calculated member

...

Wiki Markup
{align:justified}If you would like to define new calculated member then expand corresponding dimension (e.g. _Measures_) and expand *Calculated members* section:{align}

...

 

...

 

...

Image Removed

...

 

...

 

...

 

...

Wiki Markup
{align:justified}If you would like to define new calculated member then click on _Define new_ link, if you would like to edit (or delete) existing calculated member then click on _edit_ link. If you do not have rights to edit calculated members in current account then you will see just _show_ link to see definition of existing calculated members. When you define  new or edit existing calculated member you will see the following  calculated member definition form: {align}

...

 

...

 

...

 

...

Image Removed

...

 

...

 

...

 

...

Wiki Markup
{align:justified}Each calculated member should have name (unique within dimension) and  calculation formula (the rest of tutorial will teach how to write  calculation formulas). In addition you can specify how calculated member  value should be formatted (e.g. as integer, decimal, date or using  default formatting). From the right sidebar you can quickly select other  members, dimensions, operators or functions to insert them in  calculation formula. \\
After defining calculated member formula press *Update* to save it or *Delete* to delete existing calculated members or *Cancel* to discard any changes. If calculated member formula will be invalid then corresponding error message will be displayed. \\
*Do not modify or delete pre-defined calculated members that were created by source application import* \- their original definition will be recreated next time you will  perform source application import. If you need to create modified  version of existing pre-defined calculated member then copy its  calculation formula and create new calculated member with different name  and modify copied calculation formula for the new calculated member. \\
Let's start writing some calculation formulas\! Following examples will use [Sales demo cube|https://eazybi.com/accounts/1/cubes/Sales] dimensions and measures to illustrate creation of calculation formulas. {align}

...

 

...

 

...

 

Dimension, hierarchy, level and member names

Wiki Markup
{align:justified}When writing calculation formulas you will need to reference  dimensions, dimension hierarchies, hierarchy levels and other existing  dimension members. In MDX query language all names are enclosed in  square brackets \[   \]. \\
To reference *dimension* you just enclose its name in square brackets, e.g. [Customers], [Time] or [Measures]. \\
If dimension has just one *hierarchy* then you can reference the primary hierarchy in the same way as dimension, e.g. [Customers] or [Measures]. When you import _Time_ dimension then it automatically will create main hierarchy (with year, quarter, month and day levels) as well as _Weekly_ hierarchy (with year, week and day levels). [Time] will reference _Time_ dimension main hierarchy but [Time.Weekly] will reference _Time_ dimension _Weekly_ hierarchy. \\
When you expand specific dimension and expand *All hierarchy level members* then you see names of all main *hierarchy levels*. You can reference particular hierarchy level with [dimension or hierarchy name].[level name]. E.g. [Customers].[City] references _City_ level of main hierarchy of _Customers_ dimension. \\
In case of Time dimension use [Time].[Year], [Time].[Quarter], [Time].[Month], [Time].[Day] to reference main hierarchy levels and [Time.Weekly].[Year], [Time.Weekly].[Week], [Time.Weekly].[Day] to reference weekly hierarchy levels. \\
Each dimension typically will have default *{_}All{_}* *member* which can be used to get totals of measures per this dimension. If _Customers_ dimension has default _All_ member named _All Customers_ then you can reference it with [Customers].[All Customers]. As default _All_ member can be renamed it is safer to use [Customers].DefaultMember to get the same result. In case of Time dimension there are two default _All_ members for each hierarchy - [Time].DefaultMember and [Time.Weekly].DefaultMember. \\
When you want to reference *top level dimension members* then you use [dimension or hierarchy name].[member name]. E.g. [Customers].[USA] will reference USA member from top level _Country_ level. To reference _detailed level dimension members_\* you need to specify full "hierarchy path" to this member, e.g. [Customers].[USA].[CA] to reference _CA_ member in _State Province_ level under _USA_ parent member. Or [Customers].[USA].[CA].[San Francisco] to reference city etc. \\
All *measures* are in top level of _Measures_ dimension and you can reference them with e.g. [Measures].[Store Sales], [Measures].[Store Cost] etc.{align}

 

Simple arithmetic calculations

Wiki Markup
{align:justified}Now that you know how to reference other existing measures and other  dimension members we can create simple arithmetic calculations. \\
If we have [Measures].[Store Sales] and [Measures].[Store Cost] measures then we can define new calculated measure Profit (which will have full name [Measures].[Profit]) with formula:[Measures].[Store Sales] \- [Measures].[Store Cost]\\
When you will use this new measure Profit in your Standard-BI reports then for any combination with other dimension values this formula will calculate difference between Store Sales measure value and Store Cost measure value. \\
You can use defined calculated measures also in other calculated measures that you define later (_it  is important to define them in right sequence - you cannot use  calculated member that you will define later than current calculated  member_). For example, you can now define [Measures].[Margin %|Margin %] with formula[Measures].[Profit] / [Measures].[Store Sales]\\
and it will calculate margin as number from 0 to 1. If you would like to display results as percentage value then change _Formatting_ of this calculated measure to use _integer_ or _decimal percentage_ formatting. \\
You can perform arithmetic calculations also for calculated members in other dimension. For example, you could define [Customers].[West coast] calculated member in _Customers_ dimension with formula[Customers].[USA].[CA] + [Customers].[USA].[OR] + [Customers].[USA].[WA]\\
and now when you will combine in reports [Customers].[West coast] with [Measures].[Store Sales] you will get total sales for all these three states together. If you will combine it with [Measures].[Profit] you will get total profit for all these three states.{align}

 

Moving across dimension levels

Wiki Markup
{align:justified}When you use [Measures].[Store Sales] then it will calculate Store Sales measure value for other corresponding dimension members for each row /  column in report. If in your calculation formula you would like to  override some other dimension value then you need to use _tuples_ which is combination of members from different dimensions. MDX syntax for tuples is (member_1, member_2, ..., member_n). For example, if you would like to get Store Sales measure value for all customers then you would use tuple ([Measures].[Store Sales], [Customers].DefaultMember. \\
Let's define calculated measure [Measures].\[\] with formula (and percentage formatting)[Measures].[Store Sales] / ([Measures].[Store Sales], [Customers].DefaultMember) \\
When in report you will combine [Measures].\[\] with e.g. [Customers].[USA].[CA] then you will see percentage of California sales from total customer sales. \\
Maybe you don't want to see percentage from total sales but  percentage from customer hierarchy direct parent sales (e.g. for city  level show percentage from corresponding state sales). In this case you  can use *CurrentMember* dimension or hierarchy property  to access current member of corresponding dimension for which expression  currently is evaluated. E.g. [Customers].CurrentMember will return current member in _Customers_ hierarchy for which this expression is evaluated. Or in other words if _Customers_ dimension is placed on report rows then by using [Customers].CurrentMember we know for which row this formula is currently evaluated. \\
If we have current member then we can navigate to other dimension members relative from this member. [Customers].CurrentMember.Parent will return parent member in _Customers_ hierarchy for current member. Ancestor([Customers].CurrentMember, [Customers].[Country]) will move from current member up to "ancestor" in _Country_ level. \\
So we can define calculated measure [Measures].\[\] as[Measures].[Store Sales] / ([Measures].[Store Sales], [Customers].CurrentMember.Parent) \\ \\
 {align}

...

 

Moving in time

Wiki Markup
{align:justified}As you can move to different dimension hierarchy levels you can also  move to different dimension members in the same dimension. It is  typically used in _Time_ dimension when you want to compare measures between different time periods or aggregate time period range. \\
For example, calculated measure [Measures].[Sales monthly growth] with formula[Measures].[Store Sales] \- ([Measures].[Store Sales], [Time].CurrentMember.PrevMember) \\
will calculate [Measures].[Store Sales] growth comparing to previous _Time_ dimension member period. If this calculated measure will be combined in report with month in _Time_ dimension it will show growth comparing to previous month, if it will  be combined with year then it will show growth comparing to previous  year. \\
There are several functions that help you "move in time":
* [Time].CurrentMember.PrevMember returns previous member in the same hierarchy level (it will return empty member for the first member)
* [Time].CurrentMember.NextMember will return next member
* [Time].CurrentMember.Lag(2) will return previous member with distance 2 (use any number for argument, Lag(1) is the same as PrevMember)
* [Time].CurrentMember.Lead(2) will return next member with distance 2 (Lead(1) is the same as NextMember)
* ParallelPeriod([Time].[Year], 1, [Time].CurrentMember) will return "parallel" Time member one year ago (e.g. for day level member Jan 01 2012 it will be day Jan 01 2011 but for month level member Jan 2012 it will be month Jan 2011)
* OpeningPeriod([Time].[Day], [Time].CurrentMember) will return first descendant of current _Time_ member at _Day_ level (first day of year, quarter or month which is defined in _Time_ dimension)
* ClosingPeriod([Time].[Day], [Time].CurrentMember) will return last descendant of current _Time_ member at _Day_ level (last day of year, quarter or month which is defined in _Time_ dimension)Sometimes you would like to test if your time navigation expression  is working as you expect (before using it in further calculations). Then  you can use .Name member property to get member name as calculation formula result. For example, define calculated measure [Measures].[test opening day] with formulaOpeningPeriod([Time].[Day], [Time].CurrentMember).Name \\
and use it in report together with _Time_ dimension members and see if you get expected result for _Time_ dimension members at different levels. \\\\\\\\
 {align}

 

Sets

Wiki Markup
{align:justified}Now you know you to navigate to individual dimension members. But quite often you would like to perform operations on *set of dimension members*, for example, find sum or average value of some measure over selected set of dimension members. \\
The easiest way how to use sets in calculation formulas is to use list of members enclosed in curly braces, e.g.\{[Customers].[USA].[CA], [Customers].[USA].[OR], [Customers].[USA].[WA]\} \\
\{[Time].[2011], [Time].[2012]\} \\
If you would like to select set as range of sequential dimension  level members then you can specify first and last member and use : between them. For example, this will create a set of dates from Jan 01 2012 to Jan 15 2012:[Time].[2012].[Q1 2012].[Jan 2012].[Jan 01 2012]:[Time].[2012].[Q1 2012].[Jan 2012].[Jan 15 2012]\\
Quite often you don't want to specify exact range of members but  would like to get all dimension hierarchy level members. You can do it  with Members method, for example, to get all months in Time dimension:[Time].[Month].Members \\
When you want to test which members will be returned by some set expression then you can use SetToStr functions to create string of concatenated member full names. For example, define calculated measure [Measures].[test all months] with formulaSetToStr([Time].[Month].Members) \\
There are several other useful functions for working with sets, here are some examples how to use them:
* [Customers].[USA].[CA].Children returns set of children members using dimension hierarchy (in this example all cities in California)
* Descendants([Customers].[USA], [Customers].[City]) returns set of member descendants at specified hierarchy level (in this example all cities in USA)
* [Customers].[USA].[CA].[San Francisco].Siblings returns  all members which have the same parent as this member (in this example  all cities in California), it is the same as using [Customers].[USA].[CA].[San Francisco].Parent.ChildrenThere are additional methods that you can use to get just first or last members of these sets - FirstChild, LastChild, FirstSibling, LastSibling. \\\\\\\\\\\\\\\\\\\\\\\\\\
 {align}

 

Sets of time periods

Wiki Markup
{align:justified}There are additional useful set selection methods for Time periods
* PeriodsToDate([Time].[Year], Time.CurrentMember) returns all periods from beginning of current member in specified level  (in this example from beginning of current year) until specified member.
* YTD([Time].CurrentMember) is shorter version of the same function (abbreviation from Year-to-date)
* QTD([Time].CurrentMember) returns quarter-to-date set of members
* MTD([Time].CurrentMember) returns month-to-date set of members
* WTD([Time].CurrentMember) returns week-to-date set of membersIf you have just time dimension in your cube then you can use even shorter expressions YTD(), QTD(), MTD() and WTD() as by default they will receive as argument current member of time dimension [Time].CurrentMember.  But sometimes you need to pass different argument to these functions.  For example, if you would like to compare current year-to-date  aggregates with year-to-date aggregates year ago you can use the  following expression to get year-to-date set for corresponding _Time_ dimension member a year ago:YTD(ParallelPeriod([Time].[Year], 1, [Time].CurrentMember)) \\
And if you like shorter expression you can omit current member argument here as well YTD(ParallelPeriod([Time].[Year], 1)). Most of time related functions will use [Time].CurrentMember as default member argument. \\
If you want to get number of last periods from _Time_ dimension then you can use LastPeriods function. For exampleLastPeriods(3, [Time].CurrentMember) \\
will return set with current _Time_ member and two previous members. If you would like to get current and next two _Time_ periods then use negative value \-3 instead of 3. \\\\\\\\\\\\\\\\\\\\\\\\\\
 {align}

 

Set operations

Wiki Markup
{align:justified}There are several operations that you can perform on sets:
* \{set1, set2, ..., setn\} returns union of two or more sets
* Except(set1, set2) returns _set1_ members but removes any member that is in _set2_
* Head(set, number) returns set with first _number_ of members from original set (if _number_ is not specified then set from the first set member is returned)
* Tail(set, number) returns last _number_ of members from set
* set.Item(position) returns one member from set with specified _position_ (starting from zero). So if you would like to get first member of set you can use expression Head(set).Item(0)Quite frequently you would like to filter set members using some condition. You can do this with Filter(set, condition). For example in this way you can filter all cities with sales larger than 1000:Filter([Customers].[City].Members, [Measures].[Store Sales] > 1000) \\
Within condition expression \[Customers\].CurrentMember references  current set member for which condition is evaluated. For example, this  will return all cities which name starts with _San_ (using _MATCHES_ operator with [regular expression|http://en.wikipedia.org/wiki/Regular_expression])Filter([Customers].[City].Members, [Customers].CurrentMember.Name MATCHES 'San .*') \\
Other typical function that is used in conditions is IsEmpty. This expression will return all cities which have non-empty sales amount:Filter([Customers].[City].Members, NOT IsEmpty([Measures].[Store Sales])) \\
Previously simple set to string function SetToStr was  mentioned that is useful for expression testing purposes. But if you  would like to format set results in customized way then you can use Generate(set, string_expression, separator_string). For example the following expression will return city names concatenated using comma where there is no sales amount recorded:Generate( \\
Filter([Customers].[City].Members, IsEmpty([Measures].[Store Sales])), \\
[Customers].CurrentMember.Name, ', ') \\\\\\\\\\\\\\\\\\\\\\\\\\
 {align}

 

Aggregates

Wiki Markup
{align:justified}Now that you know how to select different sets of dimension members  you can calculate different aggregated values from these sets:
* Sum(set, numeric_expression) calculates _numeric_expression_ for each _set_ member and returns sum of all these results. For example, Sum(LastPeriods(3). [Measures].[Store Sales]) will calculate total sales for last three _Time_ periods starting from current _Time_ dimension member.
* Count(set) returns count of set members. Count(set, ExcludeEmpty) will return count of set members for which corresponding measure values are not empty.
* Avg(set, numeric_expression) calculates average of _numeric_expression_ in _set_
* Max(set, numeric_expression) returns maximum value of _numeric_expression_ in _set_
* Min(set, numeric_expression) returns minimum value of _numeric_expression_ in _set_ \\\\\\\\\\\\\\
 {align}

 

Conditions

Wiki Markup
{align:justified}You can write formulas with conditions and different results based on condition evaluation using IIF and CASE functions. For simple IF / THEN / ELSE type of conditions use IIF(condition, if_true_expression, if_false_expression) (notice that there are two I letters in IIF), for exampleIIF([Measures].[Profit] > 0, 'Profit', 'Loss') \\
If there are many conditions then it is easier to use CASE function, for exampleCASE \\
WHEN [Measures].[Profit] > 1000 \\
THEN 'Big profit' \\
WHEN [Measures].[Profit] > 0 \\
THEN 'Small profit' \\
ELSE 'Loss' \\
END \\
If all conditions are comparisons of the same expression to different expected values then other CASE form can be used, for exampleCASE [Time].CurrentMember.Level.Name \\
WHEN 'Month' \\
THEN Sum(LastPeriods(3), [Measures].[Store Sales]) \\
WHEN 'Day' \\
THEN Sum(LastPeriods(90), [Measures].[Store Sales]) \\
END \\
In IIF and CASE conditions standard comparison operators can be used (=, <, <=, <>, >, >=) as well as AND, OR and NOT operators as well as several specific operators:
* IS returns whether two objects are the same, for example, [Customers].CurrentMember IS [Customers].DefaultMember (which will be true if _Customers_ current member is default _All Customers_ member)
* IN and NOT IN returns whether member is in set, for example [Customers].CurrentMember IN [Customers].[USA].[CA].Children \\\\\\\\\\\\\\\\\\\\\\\\\\
 {align}

 

Member properties

Wiki Markup
{align:justified}Dimension members has some default properties (like .Name)  as well as they can have additional custom properties. Standard-BI source  application import (e.g. from Basecamp, Highrise or JIRA) are also  importing additional dimension fields from source systems. MDX has  standard Properties function to access member properties but Standard-BI defines improved getProperty function (which will return empty result instead of exception if no property is defined for current dimension level). \\
For example, Highrise import adds _Created date_ property for all imported deals. The following expression returns _Created date_ property value for _Deals_ dimension current member[Deals].CurrentMember.getProperty('Created date') {align}

 

Date type conversion

Wiki Markup
{align:justified}Sometimes you might need to convert string expression to integer,  decimal or date expression (for example, to convert Highrise custom  field string value to corresponding type to be able to use it in further  calculations). There are several functions available for data type  conversions:
* CInt(value) returns value converted to integer
* CDbl(value) returns value converted to _double floating number_ type (should be used when results should be decimal)
* Standard-BI defines additional function DateParse(value) which will try to convert value to date value using different date formats (for example both 2012-01-31 and Jan 31 2012 will be converted to correct date). There is MDX standard CDate function but it supports less date types. \\\\\\\\\\\\\\
 {align}

 

Time difference calculations

Wiki Markup
{align:justified}Standard-BI defines additional function DateDiffDays(from_date, to_date) which will return difference in days between two dates. It can be used together with Now() function (which returns current time) to get distance in days between  selected time dimension member and current date, for exampleDateDiffDays(DateParse([Time].CurrentMember.Name), Now()) \\
In addition Standard-BI defines function DateAddDays(date, number_of_days) which will return new date in the past (if _number{_}{_}of{_}{_}days_ is negative) or in future (if _number{_}{_}of{_}{_}days_ is positive). For example, this will return date which is 5 days from current _Time_ dimension member date.DateAddDays(DateParse([Time].CurrentMember.Name), 5) \\
Sometimes it is useful to get _Time_ dimension member which  corresponds to actual current date. This can be done with Standard-BI  specific dimension hierarchy level property CurrentDateMember. For example, these expressions will return month of current date and week of current date[Time].[Month].CurrentDateMember \\
[Time.Weekly].[Week].CurrentDateMember \\
If there is no _Time_ dimension level that corresponds to current date (e.g. if data for current month or week are not yet imported) then CurrentDateMember will return the last period before current date which is present in _Time_ dimension corresponding level. {align}

 

Default values

Wiki Markup
{align:justified}Sometimes you might want some default value if some measure or function will return empty value. In these cases you can use CoalesceEmpty(expression, default_value) function, for exampleCoalesceEmpty([Measures].[Store Sales], 0) {align}

 

Comments

Wiki Markup
{align:justified}It is possible to write comments in calculation formulas. Use  comments either to describe some non-obvious complex calculations or  also commenting is valuable during writing and testing calculation  formulas - when something is not working as expected then comment all  formula lines and leave uncommented just some part of formula that you  would like to debug.-\- one line comment \\
expression -- comment until end of line \\
/\* multi line \\
comment \\
\*/ {align}

 

List of all MDX functions

...

Wiki Markup
{align:justified}This tutorial covered most frequently used MDX functions and examples  how to use them. If you didn't find what you need then take a look at [list of all MDX functions|http://mondrian.pentaho.com/documentation/mdx.php]. \\
If you have any unclear questions or issues when writing calculation formulas then [contact Standard-BI support|mailto:atbalsts@burti.lv]. {align}

...

Ja vēl neesat iepazinušies ar flex.bi vispārējo pārskatu, datu analīzes un jaunu atskaišu izveidošanas funkcionalitātes aprakstiem, lūdzu, iepazīstieties ar tiem. Šī instrukcija apraksta kā definēt jaunus aprēķināmos rādītājus izmantojot MDX vaicājumu valodas kalkulāciju formulas.

Rādītāju (mērījumu) veidi

Katra kuba dimensija sastāv no vienas vai vairākām hierarhijām ar vienu vai vairākiem hierarhijas līmeņiem. Katru hierarhijas līmeni savukārt veido dimensiju rādītāji. Eksistē normālie rādītāji, kuri ir importēti no datubāzes failiem vai aplikācijām, taču papildus tiem ir iespējams definēt arī Aprēķināmos rādītājus ( calculated members ) izmantojot kalkulāciju formulas. Kalkulāciju formulas tiek definētas izmantojot  MDX vaicājumu valodu. 

Eksistē speciāla dimensija Rādītāji ( Measures ), kura satur visus ieimportētos rādītājus ( piemērām : Apgrozījums ( Item base amount) , Atlaides lielums( Discount amount) , Nodokļu lielums( tax amount) u.c ). Aprēķināmie rādītāji  ( Calculated members ) visbiežāk tiek definēti tieši Rādītāju dimensijā (Measures dimension) ,  un tie tiek saukti par Aprēķināmajiem rādītājiem ( Calculated members) .

Kā definēt jaunu Aprēķināmo rādītāju ?

 Ja vēlaties definēt jaunu aprēķināmo rādītāju, tad paplašiniet attiecīgo dimensiju, piemēram, Rādītāji  (Measures ) un paplašiniet arī Aprēķināmo rādītāju ( Calculated members )  sadaļu.

 Ja vēlaties izveidot jaunu aprēķināmo rādītāju, tad klikšķiniet uz Veidot jaunu ( Define New ), ja vēlaties labot vai izdzēst eksistējošu aprēķināmo rādītāju uzklikšķiniet uz Edit . Ja jūsu lietotāja kontam nav piešķirtas tiesības rediģēt aprēķināmos rādītājus, tad jūs redzēsiet tikai iespēju Parādīt ( Show ) un varēsiet apskatīt tikai jau eksistējošu Aprēķināmo rādītāju  definīcijas un formulas. Kad Jūs definēsiet jaunu vai labosiet eksistējošu aprēķināmo rādītāju, jūs redzēsiet sekojošu logu :

Image Added

Katram aprēķināmajam rādītājam ir nosaukums ( unikāls dimensijas ietvaros) un aprēķinu formula ( pārējā apmācības daļa mācīs, kā rakstīt aprēķinu formulas ). Turklāt jūs varat norādīt, kā aprēķinātā rādītāja vērtība ir jāformatē ( piemēram, kā skaitlim , decimāldaļai, datumam vai izmantojot noklusējuma formatējamu ) . Labajā sānu joslā, jūs varat ātri izvēlēties citus rādītājus, dimensijas, operācijas vai funkcijas, lai ievietotu tos aprēķina formulā .

Pēc tam, kad esat definējis aprēķināmā rādītāja formulu, spiediet Atjaunot ( Update) lai saglabātu to, vai Izdzēst ( Delete), lai izdzēstu konkrēto aprēķināto rādītāju, Atcelt (Cancel), lai atceltu izmaiņas. 

Ja aprēķināmā rādītāja formula būs sastādīta nepareizi, ekrānā parādīsies kļūdas paziņojums.
Nemēģiniet labot Aprēķināmos rādītājus , kas ir sistēmas vai sākotnējā datu importa definēti - to oriģinālā formulas definīcija tiks atjaunota pie nākamā datu importa.  Ja jums nepieciešams labot jau eksistējoša aprēķināmā rādītāja formulu, tad nokopējiet esošo formulu, izveidojiet jaunu aprēķināmo rādītāju ar citu nosaukumu, iekopējiet jūs interesējoša rādītāja formulu un veiciet labojumus.

Ķersimies klāt pie formulu rakstīšanas!  Sekojošie formulu piemēri tiks veidoti uz demo konta Pārdošanas datu kuba dimensijām un rādītājiem.

Dimensijas, hierarhijas, līmeņi un rādītāju nosaukumi

Kad rakstīsiet formulas jums būs jāizmanto dimensijas, dimensiju hierarhijas, hierarhiju līmeņi un citi eksistējošu dimensiju rādītāji. MDX vaicājumu valodā visi nosaukumi tiek likti kvadrātiekavās [ ].

 Lai atsauktos un izmantotu dimensiju, vienkārši ievietojat formulā tās nosaukumu kvadrātiekavās, piem. Klienti [Customers],Laiks [Time] vai Rādītāji [ Measures ] . Ja dimensijai ir tikai viena hierarhija, ta jūs varat ievietot galveno hierarhiju tieši tāpat kā dimensiju, piem. Klienti [Customers], vai Rādītāji [ Measures ].

 Kad jūs importējat Laika ( Time ) dimensiju , tad automātiski tiks izveidota arī Galvenā hierarhija ( ar gadu, ceturkšniem, mēnešiem un dienu līmeņiem), kā arī Nedēļas hierarhija ( ar gadu, nedēļu un  dienu līmeņiem). Līdz ar to Laiks (Time ) definēs laika dimensiju, bet Laiks.Nedēļas ( Time.Weekly) definēs laika diemnsiju Nedēļas ( Weekly ) hierarhiju.

 Kad jūs paplašinat un apskatat kādu dimensiju un visus tās hierarhijas rādītājus ( All hierarchy level members ), tad jūs redzēsiet nosaukumus visiem hierarhiju līmeņiem. Jūs varat atsaukties uz kādu konkrētu hierarhijas līmeni rakstot dimensija vai hierarhijas līmenis.līmeņa nosaukums ( dimension or hierarchy name.level name ). Piem. Klienti.Pilsēta   ( Customers.City) atsaucas uz Pilsētu (City) līmeni galvenajai hierarhijai Klienta ( Customer) dimensijā.   

 Gadījumā ja izmantosiet Laika ( Time) dimensiju tad attiecīgi tie būs Laiks.Gads (Time.Year ), Laiks.Ceturksnis (Time.Quarter), Laiks.Mēnesis (Time.Month), Laiks.Diena (Time.Day) lai atsauktos uz galveno hierarhijas līmeni un Laiks.Nedēļas.Gads ( Time.Weekly.Year), Laiks.Nedēļas.Nedēļa (Time.Weekly.Week), Laiks.Nedēļas.Diena (Time.Weekly.Day), lai atsauktosuz Nedēļas hierarhijas līmeņiem.

 Katrai dimensijai parasti būs arī Visi rādītājs ( All member), kas var tikt izmantoti, lai iegūtu rādītāju summas dimensijā.  Ja Klientu (Customers ) dimensijai ir noklusētais Visi rādītājs ar nosaukumu Visi Klienti ( All Customers), tad Jūs varat atsaukties uz to rakstot Klienti.Visi Klienti( Customers.All Customers). Visi rāditāju ( All member ) ir iespējams pārdēvēt un ir drošāk izmantot  Klienti. Noklusētais rādītājs (Customers.DefaultMember), lai iegūtu to pašu rezultātu.  Laika ( Time) dimensijas gadījumā ir divi noklusētie All rādītāji katrai hierarhijai  - Time.DefaultMember and Time.Weekly.DefaultMember.

 Ja vēlaties atsaukties uz augstākā līmeņa dimensiju rādītājiem (top level dimension members), tad izmantojiet dimensijas vai hierarhijas nosaukumu.rādītāja nosaukumu  (dimension or hierarchy name.member name) Piem. Klienti.USA (Customers.USA) atsauksies uz USA rādītāju austākajā līmenī Valsts ( Country) . Lai atsauktos uz detalizēto līmeņu dimensiju rādītājiem * ( detailed level dimension members*) jums ir jānorāda pilns "hierarhijas ceļš " uz šo te rādītāju piem. Klienti.USA.CA (Customers.USA.CA) lai atsauktos uz CA rādītāju Valts reģions (State Province) līmenī zem USA rādītāja. Vai arī piemKlienti.USA.CA.San.Francisko ( Customers.USA.CA.San Francisco) lai atsauktos uz pilsētu utt.
Visiem rādītājiem, kas ir augšējā līmenī rādītāju dimensijai Jūs varat piekļūt šādi :  Measures.Store Sales, Measures.Store Cost utt.

Vienkāršas aritmētiskas kalkulācijas

Tagad, kad Jūs zināt, kā piekļūt citiem eksistējošiem rādītājiem un citu dimensiju lielumiem mēs varam sākt veidot vienkāršas aritmētiskas kalkulācijas .

 Ja mums ir ir lielumi Rādītāji.Pārdošana ( Measures.Store Sales )  un Rādītāji. Pārdošanas Izmaksas ( Measures.Store Cost), tad mēs varam definēt jaunu aprēķināmo rādītāju Peļņa ( Profit), kura pilnais nosaukums būs Rādītāji.Peļņa ( Measures.Profit) ar formulu : Measures.Store Sales - Measures.Store Cost.

 Kad Jūs izmantosiet lielumu Peļņa ( Profit ) veidojot StandardBI atskaites, tad jebkurai kombinācijai ar citām dimensiju vērtībām ši formula kalkulēs starpību starp Pārdošanas ( Store Sales) vērtību un Izmaksas ( Store Cost ) vērtību. 

 Jūs varat izmantot jau izveidotos Aprēķināmos rādītājus rakstot formulas jauniem Aprēķināmajiem rādītājiem, taču  jāievēro pareiza definēšanas secība. Piemēram, jūs varat definēt tagad Rādītāji. Robeža % ( Measures.Margin % ) ar formulu Rādītāji. Peļņa / Rādītāji.pārdošana ( Measures.Profit / Measures.Store Sales) un StandardBI izrēķinās robežu izteiktu skaitlī un 0 - 1. Ja Jūs vēlaties attēlot rezultātus kā procentus, tad nomainiet  rezultāta Formātu  (Formating ) Vesels skaitlis ( Integer)  vai Decimālprocents ( Decimal percentage )
Jūs varat veikt aritmētiskas kalkulācijas arī citas dimensijas Aprēķinātajiem rāditājiem. Piemēram, jūs varat definēt Klienti.West coast ( Customers.West coast ) aprēķināmo rādītāju Klienti dimensijā ar formulu Klienti.USA.CA + Klienti.USA.OR + Customers.USA.WA (Customers.USA.CA + Customers.USA.OR + Customers.USA.WAun tagad, ja jūs kombinēsiet atskaitēs Klienti.West coast ( Customers.West coast ) ar Rādītāji.Pārdošana (Measures.Store Sales ), jūs iegūsiet  pārdošanas rezultātus visiem trīs šiem štatiem kopā. Ja vienā atskaitē kombinēsiet Rādītāji.Peļņa (Measures.Profit), jūs iegūsiet peļņu viesiem šiem trīs štatiem kopā. 

Pārvietošanās dimensiju līmeņos

Kad jūs izmantosiet Rādītāji.Pārdošana (Measures.Store Sales), tad StandardBI aprēkinās Pārdošanas lielumu katrai atbilstošās dimensijas rādītāja rindiņai/ kolonnai atskaitē.  Ja jūsu kalkulāciju formulā jūs gribat ignorēt kādas citas dimensijas vērtību, tad jums ir jāizmanto tuples, kas ir dažādu rādītāju kombinācija no dažādam dimensijām. MDX sintakse priekš tuples is  (member_1, member_2, ..., member_n). Piemēram, ja jūs velētos iegūt Pārdošanas (Store Sales) vērtību visiem klientiem tad jūs izmantotu tuple Rādītāji.Pārdošana, Klienti.Noklusētais rādītājs (Measures.Store Sales, Customers.DefaultMember ) .

 Tagad ķersimies klāt aprēķināmo rādītāju definēšanai ( Measures.[] ) ar formulu ( un procentu formatējamu)  : Rādītāji. Pārdošana / Rādītāji.Pārdošana, Klienti.Noklusētais rādītājs (Measures.Store Sales / (Measures.Store Sales, Customers.DefaultMember).

 Kad atskaitē jūs kombinēsiet  (Measures.[] ) ar piemēram Klienti.USA.CA (Customers.USA.CA), tad jūs iegūsiet Californijas pārdošanas rezultātu no kopējās Pārdošanas procentuāli.
Iespējams jūs nevēlaties redzēt procentu no kopējās pārdošanas bet gan procentu no Klientu hierrhijas augtāka līmeņa pārdošanas rezultātiem (piem. pilsētas pārdošanas rezultātus pret atbilstošā štata pārdošanas rezultātiem). Šajā gadījumā jūs varat izmantot CurrentMember dimensiju vai hierarhiju, lai piekļūtu jūsu izvēlētajam rādītājam izvēlētajā dimensijā, kurš šobrīd tiek rēķināts. Piem. Klienti.CurrentMember ( Customers.CurrentMember ) atgriezīs rezultātā to CurrentMember Klientu hierarhijas rādītāju, kas šobrīd tiek rēķināts. Jeb citiem vārdien  - ja Klientu (Customers) dimensija ir atskaites rindās, tad izmantojot Klienti.CurrentMember ( Customers.CurrentMember) mēs zināsim, kurai rindiņai formula šobrīd tiek rēķināta. 

 Ja mums ir tekoša rādītājs (Current member ), tad mēs varam pārvietoties arī uz citiem dimensijas rādītājiem, kas ir saistīti ar šo rādītāju ( Customers.CurrentMember).  Ancestor(Customers.CurrentMember, Customers.Country) pārvietosies uz "vecāku" jeb vienu līmeni augstāku rādītāju Valsts ( Country ) līmenī.

 Tātad mēs varam definēt Aprēkināmo rādītāju ( Measures.[]) kā  (Measures.Store Sales / (Measures.Store Sales, Customers.CurrentMember.Parent)

Pārvietošanās laikā

Tā kā jūs tagad protat pārvietoties uz citu dimensiju hierarhijas līmeņiem, tad jūs arī varat pārvietoties uz citas dimensiju rādītājiem tekošajā dimensijā. Parasti to izmanto Laika dimensijā , kad jūs vēlaties salīdzināt rādītājus dažādos laika periodos vai apvienotu laika periodu kopumos.

Piemēram, ja aprēkinātais rādītājs  Rādītāji.Mēneša pieaugums (Measures.Sales monthly growth) ar formulu ( Measures.Store Sales - (Measures.Store Sales, Time.CurrentMember.PrevMember) aprēķinās Rādītāji.Pārdošana ( Measures.Store Sales) pieaugumu salīdzinājumā ar iepriekšejo Laika dimensijas rādītāju periodu. Ja šis aprēķinātais rādītājs tiks kombinēts atskaitē ar Mēnesi ( Month) Laika (Time ) dimensijā tad tiks izvadīts pieaugums salīdzinājumā ar iepriekšējo mēnesi, ja tiks izmantots gads ( Year) , tad pieaugumu salīdzinājumā ar iepriekšējo gadu.

Ir vairākas funkcijas, kuras palīdzēs jums " pārvietoties laikā " .

  • Time.CurrentMember.PrevMember atgriež iepriekšējā rādītāja vērtību tajā pašā Hierarhijas līmenī  ( tiks atgriezts tukšs rādītājs pirmajam rādītājam)
  • Time.CurrentMember.NextMember atgriezīs nākamo rādītāju.
  • Time.CurrentMember.Lag(2) atgriezīs iepriekšejo rādītāju ar "attālumu"  2 ( var izmantot jebkuru skaitli,  Lag(1) būs tas pats kā PrevMember)
  • Time.CurrentMember.Lead(2) atgriezīs nākamā rādītāju ar " attālumu " 2, ( Lead(1) is tas pats, kas NextMember)
  • ParallelPeriod(Time.Year, 1, Time.CurrentMember) atgriezīs "paralēlā"  Laika ( Time ) rādītāju vienu gadu atpakaļ ( one year ago)  (piem. dienas līmeņa rādītājam Jan 01 2012   tas būs  Jan 01 2011, bet mēnešu līmenī rādītājam Jan 2012  tas būs Jan 2011)
  • OpeningPeriod(Time.Day, Time.CurrentMember) rezultātā atgriezīs pirmo eksistējošo rādītāju tekošajam Laika ( Time ) rādītājam  līmenī  Dienas ( Day ) ( pirmo gada dienu , ceturksni vai   mēnesi), kas ir definēts Laika ( Time ) dimensijā.
  • ClosingPeriod(Time.Day, Time.CurrentMember) rezultātā atgriezīs pēdējo eksistējošo rādītāju tekošajam Laika ( Time ) rādītājam  līmenī  Dienas ( Day ) ( pēdējo gada dienu , ceturksni vai mēnesi), kas ir definēts Laika ( Time ) dimensijā. 

Dažrez jums var būt nepieciešams pārbaudīt, kā strādā Jūsu definētā " Pārvietošanās laikā " pirms Jūs to izmantojat tālākajās kalkulācijās. Tad jūs varat izmantot  .Name rādītāja iezīmi ( member property ), lai iegūtu rādītāja nosaukumu kā kalkulāciju formulas rezultātu.  Piemēram definējiet Aprēkināmo lielumu  Measures.test opening day with formulaOpeningPeriod(Time.Day, Time.CurrentMember).Name  un izmantojiet to atskaitē kopā ar Laika ( Time) dimensijas rādītājiem, tad jūs varēsies uzskatāmi salīdzināt, vai jūs iegūstat vēlamo rezultātu laika dimensijas dažādu līmeņu rādītājiem.

Kopas

Tagad jūs zināt, kā pārvietoties uz atsevišķiem rādītājiem kādā no dimensijām, taču bieži vien ir vajadzība veikt darbības ar kādu dimensiju rādītāju kopu (set of dimension members). Piemēram, atrast summu (Sum) vai vidējo rādītāju vērtību (Average) kādas noteiktas dimensijas rādītāju kopas ietvaros.  

Vienkāršākais veids kā izmantot kopas kalkulāciju formulās ir izmantojot figūriekavas { } ( curly braces),

Piemēram:

{Customers.USA.CA, Customers.USA.OR, Customers.USA.WA}   –  Kopa : gan Oregona, gan Vašingtona

{Time.2011, Time.2012}  – Kopa : gan 2011 gads, gan 2012 gads

Ja Jūs vēlēties izveidot paši savu kopu ar secīgiem dimensiju līmeņa rādītājiem, tad jūs varat norādot pirmo un pēdējo rādītāju, jūs varat izmantot kolu ":" starp tiem, tādā veidā norādot kopu " no"  -  "līdz". Piemēram, šādi var definēt kopu no Jan 01 2012 to līdz Jan 15 2012 :

Time.2012.Q1 2012.Jan 2012.Jan 01 2012:Time.2012.Q1 2012.Jan 2012.Jan 15 2012

Diezgan bieži jūs nevēlēsieties norādīt kādus īpašu kopu, bet vēlēsieties atlasīt visus dimensijas hierarhiju līmeņu rādītājus. Jūs varat to izdarīt  izmantojot Members metodi, piemēram, lai iegūtu visus laika ( Time) dimensijas mēnešus : Time.Month.Members

Kad būs nepieciešams pārbaudīt, tieši kuri rādītāji tiks atgriezti, kā rezultāts kādai jūsu definētais kopai, lai pārbaudītu formulu pareizību tad varat izmantot SetToStr  funkciju , lai izveidotu tekstu ar iekļauto rādītāju pilnajiem nosaukumiem.

Piemēram, lai definētu aprēķināmo lielumu  Measures.test all months ar formulas SetToStr(Time.Month.Members) palīdzību iegūsiet sarakstu ar visiem mēnešiem.

Ir vēl vairākas noderīgas funkcijas, kuras varat izmantot strādājot ar kopām, lūk taži piemēri ar tām un to pielietojumu :  

Customers.USA.CA.Children  - atgriezīs vienu līmeni zemākā līmeņa ( .Children ) rādītājus izmantojot dimensiju hierarhiju (šajā gadījumā tās būs visas Kalifornijas pilsētas)

Descendants(Customers.USA, Customers.City)  - atgriezīs kopu ar eksistējošajiem rādītājiem noteiktajā ( .City) hierarhijas līmenī  ( šajā gadījumā visas USA pilsētas )

Customers.USA.CA.San Francisco.Siblings  - argriezīs tos "radniecīgos" rādītājus, kuriem ir tāda pati  jeb  līmeni augstāka rādītāja vērtība, kā norādītajam ( .Sanfrancisco) . Šajā gadījumā funkcija atgriezīs visas pilsētas Kalifornijā.  

Lieši tāpat arī funkcija  Customers.USA.CA.San Francisco.Parent.Children

Ir vēl papildus metodes, kuras jūs varat izmantot lai iegūtu tikai pirmās vai pēdējās šo kopu rādītāju vērtības  - .FirstChild, .LastChild, .FirstSibling, .LastSibling.

Laika periodu kopas

Ir pieejamas vairākas noderīgas kopu atlases metodes, ko var izmantot Laika periodu kopām ( Time sets) .

  • PeriodsToDate(Time.Year, Time.CurrentMember)  - atgriezīs visus periodus sākot no .Current member ( Tekošā rādītāja) atbilstošajā līmenī ( šajā gadījumā no tekoša gada sākuma ) lidz izvēlētajam rādītājam.
  • YTD(Time.CurrentMember) ir īsāka versija šai pašai funkcijai (saīsinājums no Year-to-date)
  • QTD(Time.CurrentMember) atbilstoši  tā pati funkcija ceturkšņiem  -  quarter-to-date rādītāju kopai
  • MTD(Time.CurrentMember)  funkcija mēnešiem  - month-to-date rādītāju kopai
  • WTD(Time.CurrentMember) atgriezīs nedēļu  - week-to-date rādītāju kopu. Ja jūsu datu kubā ir tikai Laika ( Time ) dimensija, tad jūs pat varat izmantot vēl īsaku funkcijas versiju YTD(), QTD(), MTD() un WTD() tā kā , kā noklusēto vērtību tās tāpat saņems tekošo rādītāju (  Current member ) no laika dimensijas  Time.CurrentMember.
  • Bet dažreiz ir nepieciešams padot citus rādītājus , kā neatkarīgos mainīgos šīm funkcijām.  Piemēram , ja jums nepieciešams salīdzināt tekošo  year-to-date kopu ar  year-to-date kopu vienu gadu atpakaļ, jūs varat izmantot sekojošu definīciju, lai iegūtu  year-to-date kopu atbilstošajam Laika ( Time) dimensijas rādītājam vienu gadu atpakaļ :
    YTD(ParallelPeriod(Time.Year, 1, Time.CurrentMember))

Ja vēlaties funkcijas definīciju saīsināt, tad var izlaist current member argumentu arī šajā funkcijā YTD(ParallelPeriod(Time.Year, 1)). Lielākā daļai ar laika dimensiju saistītajās funkcijās izmanto  Time.CurrentMember kā noklusēto funkcijas rādītāja vērtību ( argumentu) . 

Ja vēlaties iegūt skaitu ar pēdējiem periodien no Laika ( Time ) dimensijas, tad varat izmantot LastPeriods funkciju. Piemēram LastPeriods(3, Time.CurrentMember), kas atgriezīs kopu ar tekošo ( current ) laika (Time) rādītāju un divus iepriekšējo rādītājus vērtību. Ja Jūs vēlaties iegūt Tekošo (Current) un nākamos divus Laika periodus rādītāju vērtības , tad jš varat izmantot negatīvu vērtību -3 nevis 3.

Kopu operācijas

Ir vairākas operācijas,kuras var veikt ar kopām :

  • {set1, set2, ..., setn}  - atgriež divu vai vairāku kopu apvienojumu
  • Except(set1, set2)  -  " Izņemot " atgriež to rādītājus, kas ir iekļauti set1, bet atsijā tos, kas ir iekļauti arī set2. 
  • Head(set, number)  - "Galva"  atgriež kopu ar pirmajiem n (atkarībā no tā, kāda ir noteikta " number " vērtība - 1, 2 .vai .n)  rādītājus no oriģinālās kopas ( ja number nav noteikts tad kopa sākot ar tās pirmo rādītāju tiks atgriezta rezultātā ).
  • Tail(set, number)  - atgriež pēdējos n ( atkarīgs kāds ir "number " )  rādītājus šajā kopā
  • set.Item(position)  -  atgriež vienu rādītāju no kopas ar īpašu norādītu pozīciju ( position)  (sākot no nulles). Tādēl, ja jūs vēlaties iegūt kopas pirmo rādītāju, jūs varat izmantot izteiksmi Head(set).Item(0).
     Diezgan bieži jums būs nepieciešamība filtrēt  kopas rādītājus izmantojot kādu nosacījumu. Jūs to varat izdarīt izmantojot  Filter(set, condition), kur set - kopa, condition - nosacījums. Piemēram šadā veidā jūs varat atlasīt visas pilsētas, kru pārdošanas rezultāti ir lielāki par 1000 :
    Filter(Customers.City.Members, Measures.Store Sales > 1000) 
  • Nosacījumā parasti  [Customers].CurrentMember izteiksme atsaucas uz patreizējo, tekošo rādītāju kopu, priekš kura nosacījums tiek veidots.
  • Piemēram šī te izteiksme atgriezīs visas pilsētas, kuru nosaukums sākas ar " San" (izmantojot  MATCHES ( atbilst) operātoru ar regular expression - atbilstību kādai konkrētai simbolu virknei )  :
    Filter(Customers.City.Members, Customers.CurrentMember.Name MATCHES 'San .*') 
  • Vēl kāda tipiska funkcija, kas tiek izmantota darbībām ar kopām ir  IsEmpty. Sekojoša izteiksme atgriezīs visas pilsētas kā rezultātu, kurā nav tukši pārdošanas apjomi :
    Filter(Customers.City.Members, NOT IsEmpty(Measures.Store Sales))

Iepriekš apskatītā vienkārša teksta tipa funkcija SetToStr ir noderīga lai pārbaudītu sastādīto izteiksmju pareizību. Bet ja jūs vēlaties formatēt kopas rezultātu nestandarta veidā, tad varat izmantot  Generate(set, string_expression, separator_string). Piemēram sekojoša izteiksme atgriezīs rezultātā pilsētu nosaukumus atdalītus ar komatiem, kur nav informācijas par pārdošanas apjomiem ( IsEmty(measures.Store Sales)

Generate( Filter(Customers.City.Members, IsEmpty(Measures.Store Sales)), Customers.CurrentMember.Name, ', ')

Aprēķini

Tagad, kad jūs mākat atlasīt dažādas dimensiju rādītāju kopas, jūs varat izrēķināt arī dažādas darbības starp tām:

  • Sum(set, numeric_expression)  - kalkulē summu noteiktajam rādītājam (numeric_expression), katram norādītās kopas (set) rādītājam un atgriež to rezultātu summu.  Piemēram, Sum(LastPeriods(3). Measures.Store Sales) izkalkulēs kopējo pārdošanas apjomu pēdējiem 3 laika ( Time ) periodiem ( LastPeriods (3) ) , sākot ar tekošo laika ( Tiems) dimesijas  rādītāju.
  • Count(set)  -  atgrieš kopas rādītāju skaitu, kas iekļauti kopā ,piemēram, Count(set, ExcludeEmpty ) saskaitīs to kopas rādītāju skaitu, kuriem atbilstošās vērtības nav tuksās (ExcludeEmpty - neiekļaut tukšos )
  • Avg(set, numeric_expression)  - kalkulē vidējo aritmētisko norādītajam rādītājam ( numeric expression) noteiktajā kopā.
  • Max(set, numeric_expression)  - atgriež maksimālo vērtību norādītajam rādītājam ( numeric expression) noteiktajā kopā .
  • Min(set, numeric_expression) ir tāda pati funkcija, tikai atriež minimālo vērtību kopas ietvaros.

Nosacījumi

Jūs varat rakstīt dažadas formulas ismantojot nosacījumus un izvadīt dažadus rezultātus balstoties atblstību nosacījumiem  izmantojot IIF ( Ja ) un CASE ( Gadījumā ja) funkcijas. Priekš vienkāršiem IF/THEN /ELSE tipa nosacījumiem izmantojiet

IIF(nosacījums, ja_nosacījums_pateiss, ja_nosacījums_nepatiess)

Taču ņemiet vērā ka "IIF" satur divus "I" burtus, nevis kāierats IF !  Piemēram  IIF(Measures.Profit > 0, 'Profit', 'Loss') t.i ja Measures.Profit būs lielāks par nulli tad rezultāts būs " Profit", ja vērtība neatbildīs nosacījumam un būs mazāka par 0, ta rezultāts būs " Loss" .

Ja ir vairāki nosacījumi tad ir vieglāk izmantot CASE funkciju , piemēram šadā gadījumā :
CASE
WHEN Measures.Profit > 1000        – Kad peļņa ir lielāka par 1000
THEN 'Big profit'                               -- tad peļņa ir " Liela Peļņa "
WHEN Measures.Profit > 0              – kad peļņa ir lielāka par 0
THEN 'Small profit'                           --tad rezultāts "Maza peļņa"
ELSE 'Loss'                                      – citādi rezultāts  " Zaudējumi" ( darbosies visām vērtībām kas ir mazākas vai vienādas ar 0 )
END

Ja visi nosacījumi ir salīdzinājums vienam un tam pašam nosacījumam, kas attiecas uz dažāda tipa vērtībām, tad var izmantot cita tipa CASE funkcijas fomu .

CASE Time.CurrentMember.Level.Name                     – Ja Laika dimensijas tekošā rādītāja līmenī Nosaukums
WHEN 'Month'                                                              – atbilst " Mēnesis"
THEN Sum(LastPeriods(3), Measures.Store Sales)    –  tad summēt pēdējos trīs periodu ( 3 mēnešus) pārdošanas rezultātus
WHEN 'Day'                                                                  – Ja atbilst  " Diena "
THEN Sum(LastPeriods(90), Measures.Store Sales)  – Tad summēt pēdējās 90 dienu ( kas arī ir 3 mēneši) pārdošanas rezultātus
END

Lietojot IIF un CASE nosacījumus var izmantot standarta salīdzinājuma zīmes (=, <, <=, <>, >, >=) kā arī AND, OR and NOT  ( UN, VAI un NAV) operatorus, kā arī vēl dažus papildus specifiskos operatorus :

  • IS  - atgriezīs rezultātā to, vai divi objekti ir vienādi, piemēram, Customers.CurrentMember IS Customers.DefaultMember ( kas būs patiesi, ja Customers tekošais rādītājs ( Current member)  ir noklusētais  All Customers ( Visi Klienti)  rādītājs.
  • IN un  NOT IN  operatori atgriezīs rezultātā vai rādītājs ir iekļauts kādā noteiktā rādītāju kopā, Piemēram Customers.CurrentMember IN Customers.USA.CA.Children , šajā gadījumā vai Klienti tekošais rādītājs ( Current member )  ir no Kanādas klientiem.

Dimensiju rādītāju īpašības

Dimensiju rādītājiem katram ir vairākas noklusētās īpašības/ iezīmes ( kā piemēram Nosaukums (Name) kā arī tiem varbūt piešķirtas citas nestandarta īpašibas/ iezīmes )  Standard-BI sākuma imports no (piemēram no Basecamp, Highrise vai JIRA) importē arī papildus dimensijas raksturojošos laukus no datu sākotnējās sistēmas. 

MDX ir standarta funkcija Properties (Iezīmes ) MDX valodā, lai piekļūtu rādītāju iezīmēm, bet Standard-BI piedāvā uzlabotu funkciju getProperty ( kura atgriezīs tukšu rezultātu izņēmuma gadījumā, kad iezīme (Property) neeskistē tekošajam dimensijas līmenim.

Piemēram, Highrise imports pievieno iezīmi Izveidošanas datums (Created date) visiem importētajiem ierakstiem ( Deals). Sekojoša funkcija atgriež Izveidošanas datumu (Created date) iezīmes vērtību Ierakstu (Deals) dimensijas tekošjam rādītājam : Current memberDeals.CurrentMember.getProperty('Created date')

Datu tipu konvertācija

Dažreiz Jums var būt nepieciešams konvertēt teksta tipa lauku ( String) uz veselo skaitļu tipa ( Integer), decimāldaļas (Decimal) vai datuma (Date ) tipa lauku (piemēram, lai pārvērstu Highrise nestandarta lauka teksta vērtību uz atbilstoša veida vērtību, lai to varētu izmantot tālākajos aprēkinos). Ir pieejamas vairākas funkcijas, kas veic šādu datu tipi konvertāciju: 

  • CInt(value)  -  atgriež vērtību,kas ir konvertēta uz veseliem skaitļiem ( Integer )
  • CDbl(value)  -  atgriež returns vērtību, kas pārvēsta  " double floating number" tipā ( vajadzētu izmantot , ja rezultāts nepieciešams decimāldaļās ( decimal) )
  • Standard-BI  ir definētas papildus funkcija  DateParse(value) (DateParse( vērtība) ), kura mēģinās konvertēt lauka vērtību par datuma tipa lauku izmantojot dažada tipa datumu fromātus  (piemēram  gan 2012-01-31, gan Jan 31 2012 tiks abi konvertēti uz pareizo datumu) .  Eksitē arī MDX standarta funkcija CDate bet tā atbalsta mazāku skaitu datuma formātus.

Laika starpības kalkulācijas

flex.bi definē papildus funkcijas DateDiffDays(from_date, to_date) , kas atgriež vērtību : dienu starpība stap norādītajiem datumiem, kur from_date perioda sākuma datums, bet to_date - perioda beigu datums. To var izmantot koā ar funkciju Now() (kura atgriež patreizējo laiku), lai iegūtu starpību dienās starp norādīto laiku un patreizējo laiku, piemēram

DateDiffDays(DateParse(Time.CurrentMember.Name), Now())

 Papildus tam Standard-BI piedāvā arī funkciju DateAddDays (date, number_of_days), kura atgriezīs jaunu datumu pagātnē ( ja number_of_days ir negatīvs), vai nākotnē (ja number_of_days ir pozitīvs. Piemēram, šī funkcija atgriezīs datumu, kas ir piecas dienas no patreizējā Laika (Time) dimensijas rādītāja

date.DateAddDays(DateParse(Time.CurrentMember.Name), 5)

 Dažreiz to izmanto, lai iegūtu Laika (Time) dimensijas rādītāju, kurš atbild par faktisko laiku. To var izdarīt ar īpašu Standard-BI  dimensiju hierarhiju līmeņa atrībūtu CurrentDateMember. Piemēram, šādi var definēt mēnesi, kas atbilst  kunkrētajam datumam un nedēlu , kas atbilst konkrēt ajam datumam:

 Time.Month.CurrentDateMember

vai
Time.Weekly.Week.CurrentDateMember

 Ja neeksistē Laika dimensijas līmenis, kas atbilst patreizējam datumam (piem. ja dati par tekošo nedēļu vai mēnesi vēl nav ieimportēti ) tad CurrentDateMember atgriezīs pēdējo periodu pirms patreizējā datuma, kurš eksistē Laika (Time) dimensijas atbilstošajā līmenī. 

Noklusētās vērtības

Var būt gadījumi, kad nepieciešams, lai kalkulācija atgriež kādu noklusēto vērtību, ja kāds rādītājs vai funkcija atgriež tukšumu,  jeb rezultāta nav. Šādos gadījumos jūs varat izmantot CoalesceEmpty(expression, default_value) funkciju, kur "expression" ir aprēkināmā vērtība kura var būt tušums, piemēram  - CoalesceEmpty(Measures.Store Sales, 0)

Komentāri

Ir iespējams rakstīt komentārus kalkulāciju formulās. Izmantojiet komentārus, lai aprakstītu, paskaidrotu kādas ne uzreiz izprotamas kalkulāciju formulas vai testējot, rakstot jaunas formulas  -  kad kāda formula nestrādā, kā paredzēts, varat "izkomentēt"  dālu no formulas ārā un tad pārbaudīt tās pareizību, bet pēc tam atkal iekļaut formulā, ja nepieciešams.  

Code Block
-- vienas rindiņas komentārs
expression -- komentārs līdz rindiņas beigām
/* vairākas rindiņas garš komentārs 
*/

Saraksts ar visām MDX funkcijām

Šī apmācība deva ieskatu visvairāk lietotākajā MDX funkcijās un piemēros, kā tās lietot. Ja jūs neatradāt to, ko meklējāt, tad apskatiet sarakstu ar visām MDX funkcijām.

Ja ir vēl kādi neskaidri jautājumi vai problēmas rakstot kalkulāciju formulas tad sazinieties flex.bi atbalsta komandu.

Video Apmācību par MDX pamatkonceptiem atradīsiet šeit.