MDX syntax for DAX measures calculated with AVERAGE()

你离开我真会死。 提交于 2019-12-22 17:23:05

问题


I have the following expression in Excel that works fine.

=CUBESET("ThisWorkbookDataModel",
"TopCount(
[ProductBV].[Product Name].Children,10, 
sum(
(
[Calendar].[Week Ending].[All].[1/6/2013]:[Calendar].[Week Ending].[All].["&TEXT($E$2,"m/d/yyyy")&"],
[ProductBV].[Moderation Status (ALL)].[All].["&$E$3&"]
),
[Measures].[Product Review Count]
)
)",
"Top 10 to date")

The Product Review Count measure using the following DAX formula.

Product Review Count:=COUNTROWS(ProductBV)

However, when I change the DAX measure to one that utilizes an AVERAGE function (Product Avg Review), the CUBESET function does not work correctly. It still has the correct number of items in the CUBESET, but when I use CUBERANKEDMEMBER(1-10), it does not show me the top 10 Products by Average Rating.

Product Avg Review:=AVERAGE(ProductBV[Rating])

'Not working correctly' expression below:

 =CUBESET("ThisWorkbookDataModel",
"TopCount(
[ProductBV].[Product Name].Children,10, 
sum(
(
[Calendar].[Week Ending].[All].[1/6/2013]:[Calendar].[Week Ending].[All].["&TEXT($E$2,"m/d/yyyy")&"],
[ProductBV].[Moderation Status (ALL)].[All].["&$E$3&"]
),
[Measures].[Product Avg Review]
)
)",
"Top 10 to date")

All in all, my data is very simple. I have one data table connected to a calendar table that contains my product name, rating, date and review status.

The within my dashboard, my date ($E$2) and the review status ($E$3) can be changed by the user via a dropdown.

+-----------------+-------------------+------------+----------------+
| Submission Date | Moderation Status | Product ID | Overall Rating |
+-----------------+-------------------+------------+----------------+
| 10/23/2016      | APPROVED          | Product 1  |              5 |
| 10/23/2016      | APPROVED          | Product 2  |              5 |
| 10/23/2016      | APPROVED          | Product 3  |              5 |
| 10/23/2016      | REJECTED          | Product 3  |              3 |
| 10/23/2016      | REJECTED          | Product 4  |              3 |
+-----------------+-------------------+------------+----------------+

Can anyone tell me why this is happening and how to fix it?


回答1:


When I saw your question I wondered why you were using Excel formulas and controls to create a dashboard if you had you model in PowerPivot. PowerPivot lets you analyze and visualize data in a more robust and reliable way.

Your initial approach is very complex and it is hard to mantain taking in account that:

  • You have to edit your dashboard whenever a new product be added to your source.
  • Excel functions are prone to errors since they are expecting a specific value as parameters and user could modify it causing errors.
  • PowerPivot calculations are performed faster than Excel functions, specially for large datasets.
  • You cannot offer much interaction with the data to your user.

This is the Dashboard I'd build using Pivot Tables conected to your PowerPivot model.

DOWNLOAD THE EXCEL FILE HERE

To build this dashboard I used four DAX measures:

This Week :=
CALCULATE (
        [Product Avg Review],
        FILTER (
            ALL ( Calendar ),
            Calendar[Week Ending] <= MAX ( Calendar[Week Ending] )
        )
    )

Last Week =
    CALCULATE (
        [Product Avg Review],
        FILTER (
            ALL ( Calendar ),
            Calendar[Week Ending]
                <= MAX ( Calendar[Week Ending] ) - 7
        )
    )

Positive Movement :=
    (
        CALCULATE (
            [Product Avg Review],
            FILTER (
                ALL ( Calendar ),
                Calendar[Week Ending]
                    <= MAX ( Calendar[Week Ending] ) - 7
            )
        )
            - CALCULATE (
                [Product Avg Review],
                FILTER (
                    ALL ( Calendar ),
                    Calendar[Week Ending] <= MAX ( Calendar[Week Ending] )
                )
            )
    )
        * -1

Negative Movement :=
    CALCULATE (
        [Product Avg Review],
        FILTER (
            ALL ( Calendar ),
            Calendar[Week Ending]
                <= MAX ( Calendar[Week Ending] ) - 7
        )
    )
        - CALCULATE (
            [Product Avg Review],
            FILTER (
                ALL ( Calendar ),
                Calendar[Week Ending] <= MAX ( Calendar[Week Ending] )
            )
        )

With measures working you can create a two Pivot Tables, one for showing positive movers and other one to show negative movers.

Note my Excel UI is in spanish, I hope you don't get stuck with this. Just follow the instructions and search in Google, there are plenty resources about Pivot Tables.

In Rows pane add Product Name, in Values add Last Week, This Week and Positive Movement or Negative Movement (according to the Pivot Table you are building) measures.

Once you have the table built you have have to get the TOP X Product Names.

In each Pivot Table click the Row tag icon filter (The below settings are for Positive Movers Pivot Table):

And use these settings:

Change Positive Movement for Negative Movement when you are filtering to get the Top 5 Product Names in the Negative Movement table.

While you can get the Top 5 product names purely in DAX, I think it is better to use the Pivot Table filter option, leaving it dynamic to any number of top products (what could be a new requeriment in the future) without the need to modify the underlying DAX.

Then you just need to add Conditional Formats and you are done.

Let me know if this helps.




回答2:


This produces a set CUBESET.

A set has a count so I can appreciate that COUNTROWS will function correctly.

If you feed a set of members into AVERAGE which measure does it average over? This seems to be non-determinant ?

Can you feed the set returned by ProductBV[Rating] into a subsequent cube function that specifies the measure you'd like to have the average of?



来源:https://stackoverflow.com/questions/40294199/mdx-syntax-for-dax-measures-calculated-with-average

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!