简体   繁体   English

Power BI中的交叉表数据

[英]Crosstab data in Power BI

I have data in the PowerBI which I need to crosstab in order to generate the base data for a specific visual. 我在PowerBI中有数据,我需要对其进行交叉表分析,以便生成特定视觉效果的基础数据。

Example data: 示例数据:

tblExample = DATATABLE("Customer ID", INTEGER, "Gender", STRING, "Age Range", STRING, "Order Date", DATETIME, "WS Next Day", STRING, "Order Next Day", STRING, "WS Next Week", STRING, "Order Next Week", STRING, "WS Next Month", STRING, "Order Next Month", STRING,
    {
        {1, "Female", "30 - 39", "2017-02-09", "Yes", "No", "Yes", "No", "No", "No" },
        {2, "Female", "30 - 39", "2017-02-11", "Yes", "Yes", "Yes", "No", "No", "No" },
        {3, "Female", "50 - 59", "2017-02-12", "Yes", "No", "Yes", "No", "No", "No" },
        {4, "Male", "20 - 29", "2017-02-12", "Yes", "No", "Yes", "No", "No", "No" },
        {5, "Male", "40 - 49", "2017-02-19", "No", "No", "Yes", "No", "No", "No" }
    }
)

This data shows customers who have placed an order and then a set of six yes / no flags showing whether or not the customer has visited the web site again the following day / week / month, and whether this visit resulted in another order. 此数据显示已下订单的客户,然后显示六个是/否标志集,分别显示该客户是否在第二天/一周/一个月再次访问了该网站,以及此访问是否导致了另一个订单。

What I need to do is turn this information into a table with one row for each category of "Next Day", "Next Week" and "Next Month", and for each row a value showing the count of customers who (a) visited and (b) purchased. 我需要做的是将这些信息转换为表格,其中每一行分别包含“下一天”,“下周”和“下个月”的类别,并且每行都有一个值,该值显示了(a)访问过的客户的数量和(b)已购买。

I thought this would be pretty straight forward to do in DAX - and to be honest I'm not sure that it isn't and I haven't just missed something really obvious - but at the moment I can't see a tidy way to achieve this. 我认为这在DAX中是很简单的-老实说,我不确定不是,我也没有错过任何真正明显的东西-但目前我看不到一个整洁的方法为达到这个。

I have created measures for each of the six values I need as follows: 我为我需要的六个值分别创建了度量,如下所示:

NextDay_Visits = COUNTROWS(FILTER(tblExample, [WS Next Day] = "Yes"))
NextDay_Orders = COUNTROWS(FILTER(tblExample, [Order Next Day] = "Yes"))

And so on, and when I look at these measures in a card they give me the correct values, but to jam these into a table which is the correct shape I do the following (which is almost certainly the wrong way to do this in PowerBI !): 依此类推,当我在卡片中查看这些度量时,它们会为我提供正确的值,但要将其塞入形状正确的表格中,我会执行以下操作(这肯定是在PowerBI中执行此操作的错误方法) !):

1 Create a new table 1创建一个新表

tblJunk1 = DATATABLE("Row ID", INTEGER, "Category", STRING, { {1, "Next Day"} })

2 Add the appropriate measures to the new table as new columns 2将适当的措施作为新列添加到新表中

tblJunk2 = ADDCOLUMNS(tblJunk1, "Visits", [NextDay_Visits], "Purchases", [NextDay_Purchases])

3 Do the same two steps again using tblJunk3 / tblJunk4 for the "Next Week" figures 3使用“ tblJunk3” /“ tblJunk4”对“下周”数字再次执行相同的两个步骤

4 Do the same two steps again using tblJunk5 / tblJunk6 for the "Next Month" figures. 4使用tblJunk5 / tblJunk6再次对“下个月”数字执行相同的两个步骤。

5 Create a final table to use as the basis for my column chart by union-ing tblJunk2 / tblJunk4 / tblJunk6 5通过合并tblJunk2 / tblJunk4 / tblJunk6,创建一个最终表作为我的柱形图的基础

tblChartBase = UNION(tblJunk2, tblJunk4, tblJunk6)

Then it's good news / bad news, because although this seems to me to be a horrible hack solution, I do get the table I need with the correct figures and the correct shape, and I can base a column chart on it which gives me the analysis I am looking for. 那是个好消息/坏消息,因为尽管在我看来这是一个可怕的黑客解决方案,但我的确得到了需要的桌子以及正确的数字和正确的形状,并且可以在其上建立柱形图,从而为我提供了我正在寻找的分析。 (Good News!) (好消息!)

However, once I add slicers to the page based on "Gender" and "Age Group", the measures in this new table do not respond to them and the figures do not change meaning that this is not the way to achieve what I want. 但是,一旦我将基于“性别”和“年龄组”的切片器添加到页面上,此新表中的度量就不会对其做出响应,并且数字也不会改变,这意味着这不是实现我想要的方式。 (Bad News) (坏消息)

I am not sure whether I am kind of hedging around the correct methodology but need to tweak my DAX for the measures so they do respond to the slicers on the page, or whether this is totally the wrong approach from the ground up? 我不确定我是不是在避开正确的方法,但是需要调整DAX的措施以使它们对页面上的切片器做出响应,还是从头开始这完全是错误的方法?

Any suggestions / pointers gratefully received. 任何建议/指示均感激不尽。

I would address this requirement in the Query Editor. 我将在查询编辑器中解决此要求。 It has Unpivot functionality that should meet your needs, eg 它具有应满足您需求的Unpivot功能,例如

https://support.office.com/en-us/article/unpivot-columns-power-query-0f7bad4b-9ea1-49c1-9d95-f588221c7098 https://support.office.com/en-us/article/unpivot-columns-power-query-0f7bad4b-9ea1-49c1-9d95-f588221c7098

The Query Editor has added a few more options since that article was written, like Unpivot Other Columns, Unpivot Only Selected Columns . 自撰写该文章以来,查询编辑器添加了更多选项,例如“ 取消枢轴其他列”,“取消枢轴仅选定的列”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM