Analysis Services 微软Olap服务MDX函数应用举例

本文通过多个实例展示了MDX(Multidimensional Expressions)函数的应用,包括成员百分比分析、重要顾客分布、排序、历史累计值计算、四则运算、逻辑判断等功能,并介绍了如何利用这些函数进行复杂的业务分析。
摘要由CSDN通过智能技术生成

本文目的对丰富的函数集可以提供的强大功能有一个直观的认识
看看Microsoft是怎样应用函数,也许可以参考借鉴;
应用举例

  • 成员百分比分析
    函数:CurrentMember、Parent等;
    分析各城市的销售所占全部城市的总销售额百分比。
    WITHMEMBER Measures.[Unit Sales Percent] AS '((Store.CURRENTMEMBER,Measures.[Unit Sales]) / (Store.CURRENTMEMBER.PARENT, Measures.[UnitSales])) ', 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等;
    分析各个省份中重要顾客的数量及他们的总购买量,"重要顾客"的定义是一个顾客的购买金额或者购买数目达到或超过一定的数值。
    WITHMEMBER [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]
  • 四则运算
    函数:四则运算函数;
    在成员上及指标上均可以进行四则运算,动态派生出新的成员及指标。

    WITHMEMBER MEASURES.ProfitPercent AS '([Measures].[StoreSales]-[Measures].[Store Cost])/([Measures].[StoreCost])',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.[StoreCost]) / 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 LastPeriod]', FORMAT_STRING = 'Percent'
    select {[Measures].[Store Sales], [Measures].[Store Sales Last Period]} on columns,
    { [Product].members} on rows
    from Sales
    where ([Time].[1998])

    另一个例子,使用ParellelPeriod函数。

    WITHMEMBER [Measures].[YTD Unit Sales] AS 'COALESCEEMPTY(SUM(YTD(),[Measures].[Unit Sales]), 0)' MEMBER [Measures].[Previous YTD UnitSales] 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])
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值