MDX 語法說明

摘要:MDX 語法說明

<h3>本文目的</h3>

對豐富的函數集可以提供的強大功能有一個直觀的認識
看看Microsoft是怎樣應用函數,也許可以參考借鑑;

<h3>應用舉例 </h3>

  • 成員百分比分析
    函數:CurrentMember、Parent等;
    分析各城市的銷售所佔全部城市的總銷售額百分比。
     WITH MEMBER Measures.[Unit Sales Percent] AS '((Store.CURRENTMEMBER, Measures.[Unit Sales]) / (Store.CURRENTMEMBER.PARENT, Measures.[Unit Sales])) ', FORMAT_STRING = 'Percent'
     
    SELECT {Measures.[Unit Sales], Measures.[Unit Sales Percent]} ON COLUMNS,
     
     ORDER(DESCENDANTS(Store.[USA].[CA], Store.[Store City], SELF),[Measures].[Unit Sales], ASC) ON ROWS
     
    FROM Sales
     
  • 重要顧客分佈分析
    函數:Count、Sum、Filter、Descendants等;
    分析各個省份中重要顧客的數量及他們的總購買量,"重要顧客"的定義是一個顧客的購買金額或者購買數目達到或超過一定的數值。
     
    WITH MEMBER [Measures].[Qualified Count] AS                『 COUNT(FILTER(DESCENDANTS(Customers.CURRENTMEMBER, [Customers].[Name]), ([Measures].[Store Sales]) > 10000 OR ([Measures].[Unit Sales]) > 10))'               
     
     
    MEMBER [Measures].[Qualified Sales] AS 'SUM(FILTER(DESCENDANTS(Customers.CURRENTMEMBER, [Customers].[Name]), ([Measures].[Store Sales]) > 10000 OR ([Measures].[Unit Sales]) > 10), ([Measures].[Store Sales]))'
     
     
    SELECT {[Measures].[Qualified Count], [Measures].[Qualified Sales]} ON COLUMNS,
     
    DESCENDANTS([Customers].[All Customers], [State Province], SELF_AND_BEFORE) ON ROWS
     
    FROM Sales
     
     
  • 排序
    函數:Order
    對各個產品類別按照Store Sales指標降序排列,排序分為維內排序/整體排序。
    select {[Measures].[Unit Sales], [Measures].[Store Sales]} on columns,
     
    Order([Product].[Product Department].members, [Measures].[Store Sales], DESC) on rows
     
    from Sales
     
  • 歷史相關的累計值
    函數:YTD、Sum、Descendants
    求銷售額的本年累計值YTD(),類似還可以求解歷史累計YTD()、本月累計MTD()、本週累計WTD()等, 以及更通用的函數PeriodToDate()。
    with member [Measures].[Accumulated Sales] as 'Sum(YTD(),[Measures].[Store Sales])'
     
    select                   {[Measures].[Store Sales],[Measures].[Accumulated Sales]} on columns, {Descendants([Time].[1997],[Time].[Month])} on rows
     
    from [Warehouse and Sales]
     
  • 四則運算
    函數:四則運算函數;
    在成員上及指標上均可以進行四則運算,動態派生出新的成員及指標。
     
    WITH MEMBER MEASURES.ProfitPercent AS '([Measures].[Store Sales]-[Measures].[Store Cost])/([Measures].[Store Cost])',FORMAT_STRING = '#.00%'
     
    MEMBER [Time].[First Half 97] AS  '[Time].[1997].[Q1] + [Time].[1997].[Q2]'
     
    MEMBER [Time].[Second Half 97] AS '[Time].[1997].[Q3] + [Time].[1997].[Q4]'
     
     
    SELECT {[Time].[First Half 97], [Time].[Second Half 97], [Time].[1997].CHILDREN} ON COLUMNS,
     
    {[Store].[Store Country].[USA].CHILDREN} ON ROWS
     
    FROM [Sales]
     
    WHERE (MEASURES.ProfitPercent)
     
     
  • 邏輯判斷
    函數:IIf
    邏輯判斷可以根據不同的條件產生不同的結果。下例判斷各商店是否是啤酒及白酒的大賣家。
     
    WITH MEMBER [Product].[BigSeller] AS 'IIf([Product].[Drink].[Alcoholic Beverages].[Beer and Wine] > 100, "Yes","No")'
     
     
    SELECT {[Product].[BigSeller],[Product].children} ON COLUMNS,
     
    {[Store].[All Stores].[USA].[CA].children} ON ROWS
     
    FROM Sales
     
     
  • 成員屬性
    函數:Properties、Dimension Properties
    成員屬性是與成員綁定的,其對應關係導致很難選擇合適的使用方式。以下是使用成員屬性的例子,它對應每個商店成員列出了商店類型屬性,相應的,商店經理、商店規模、商店地址等屬性也可以被列出。該用法稍加靈活應用就可以解決過去遇到的企業名稱——〉企業代碼對應展示問題。
     
    WITH MEMBER [Measures].[StoreType] AS '[Store].CurrentMember.Properties("Store Type")',
     
    MEMBER [Measures].[ProfitPct] AS '(Measures.[Store Sales] - Measures.[Store Cost]) / Measures.[Store Sales]', FORMAT_STRING = '##.00%'
     
     
    SELECT { Descendants([Store].[USA], [Store].[Store Name])} ON COLUMNS,
     
    {[Measures].[Store Sales], [Measures].[Store Cost], [Measures].[StoreType], [Measures].[ProfitPct] } ON ROWS"
     
    FROM Sales
     
     
    另外一種用法:
     
    SELECT {[Measures].[Units Shipped], [Measures].[Units Ordered]} ON COLUMNS,
     
    [Store].[Store Name].MEMBERS DIMENSION PROPERTIES [Store].[Store Name].[Store Type] ON ROWS
     
    FROM Warehouse
     
     
  • 多步計算實現複雜邏輯
     
    函數:其實可以是任意涵數合乎邏輯的組合
     
     
    求出從來沒有買過乳製品的顧客,求解過程是先求出每位顧客在過去購買的乳製品的數量累計,然後找出累計值為0的顧客。同樣,過去遇到的求稅額大於平均稅額的海關的問題可以類似求出。
     
    with member [Measures].[Dairy ever] as 'sum([Time].members, ([Measures].[Unit Sales],[Product].[Food].[Dairy]))'
     
    set [Customers who never bought dairy] as 'filter([Customers].members, [Measures].[Dairy ever] = 0)'
     
     
    select {[Measures].[Unit Sales], [Measures].[Dairy ever]}  on columns,
     
    [Customers who never bought dairy] on rows
     
    from Sales
     
     
     
  • 同期、前期
     
    函數:PrevMember、ParellelPeriod
     
     
    求解各產品銷售額的去年同期值,年增長率。
     
    with member [Measures].[Store Sales Last Period] as '([Measures].[Store Sales], Time.PrevMember)', format='#,###.00'
     
    member [Measures].[Yearly Increase Rate] as 『

                ([Measures].[Store Sales] - [Measures].[Store Sales Last Period])/ [Measures].[Store Sales Last Period]', FORMAT_STRING = 'Percent'
 
 
                select {[Measures].[Store Sales], [Measures].[Store Sales Last Period]} on columns,
 
                { [Product].members} on rows
 
                from Sales
 
                where ([Time].[1998])
 
 
另一個例子,使用ParellelPeriod函數。
 
WITH MEMBER [Measures].[YTD Unit Sales] AS 'COALESCEEMPTY

  • (SUM(YTD(), [Measures].[Unit Sales]), 0)'
  •  

                MEMBER [Measures].[Previous YTD Unit Sales] AS  '

             (Measures.[YTD Unit Sales], PARALLELPERIOD([Time].[Year]))'
 
              MEMBER [Measures].[YTD Growth] AS '[Measures].[YTD Unit Sales] - ([Measures].[Previous YTD Unit Sales])'
 
                 SELECT {[Time].[1998]} ON COLUMNS,
 
                {[Measures].[YTD Unit Sales], [Measures].[Previous YTD Unit Sales], [Measures].[YTD Growth]} ON ROWS
 
                FROM Sales;

 
 

  • Top N分析
     
    函數:TopCount
     
     
    求解1998年總購買量處於前5名的顧客;
     
    select {[Measures].[Store Sales]} on columns,
     
    {TopCount([Customers].[Customer Name].members,5, [Measures].[Store Sales])} on rows
     
    from Sales
     
    where ([time].[1998])
     
     
     
  • 成員過慮
     
    函數:Filter、Except
     
     
    求解1998年所有顧客中購買總額得到1萬元以上的顧客,列出滿足條件的顧客的名字、年購買數量、年購買金額。
     
    Select {[measures].[Store Sales],[measures].[unit sales]} on columns,
     
    FILTER(Customers.[Name].Members,[Measures].[Store Sales] > 10000) on rows
     
    From sales
     
    Whare ([time].[1998])
     
     
    另外一種成員過慮(以所有的媒體類型中剔除No Media類型),確切的說應該是集合運算。
     
    select {[Measures].[Unit Sales]} on columns,
     
    except([Promotion Media].[Media Type].members,{[Promotion Media].[Media Type].[No Media]}) on rows
     
    from Sales
     
     
     
  • 時間區段
     
    函數:sum、":"運算符
     
     
    求美國的商店在指定時間段內的銷售額。
     
    WITH  MEMBER [Time].[1997].[Six Month] AS 'SUM([Time].[1]:[Time].[6])'
     
    MEMBER [Time].[1997].[Nine Month] AS 'SUM([Time].[1]:[Time].[9])'
     
    SELECT {[Time].[1997].[Six Month],[Time].[1997].[Nine Month]} ON COLUMNS,
     
    {[measures].[store salse]} ON ROWS
     
    FROM Sales
     
    Where ([Store].[USA])





 

 

 

 

 

請大家永躍參與Facebook MSBI 粉思團:http://www.facebook.com/#!/group.php?gid=303757165010