简体   繁体   English

创建桶的堆积柱形图

[英]Stacked Column chart that creates buckets

I currently have a bar graph in power-bi .我目前在power-bi有一个条形图。 The graph has distinct id's on the x axis and the amount of mail they received on the y axis .该图在x axis有不同的id's ,在y axis他们收到的邮件数量。 In the Table I am using, I have SalesYear , id , and a Val column which has the value 1 in each row.在我使用的表中,我有SalesYearid和一个Val列,每行的值都是1 The id shows up multiple times in the table, sometimes more than twice in the same year. id在表中出现多次,有时在同一年出现两次以上。

The problem is I want the graph reversed .问题是我希望graph reversed I would like to bucket people based on how much mail they received.我想根据人们收到的邮件数量来分类。 Then use a slicer to see how much they receive per year.然后使用slicer查看他们每年收到多少。 I have been struggling to find a solution on my own, would anyone have any ideas on how to approach this.我一直在努力寻找自己的解决方案,有人对如何解决这个问题有任何想法吗? Table l looks like this:表 l 如下所示:

    id    |     salesYear    | Val
    10    |         2012     |  1 
    11    |         2012     |  1 
    11    |         2013     |  1 
    10    |         2012     |  1 
    10    |         2013     |  1 
    12    |         2012     |  1 
    12    |         2012     |  1 

So in the visualization I want to show that on the x-axis that people who received 1 piece of mail = 0, 2 pieces of mail = 2, 3 pieces of mail = 1. My question is how can i achieve this will a Stacked Column chart.因此,在可视化中,我想在 x 轴上显示收到 1 封邮件 = 0、2 封邮件 = 2、3 封邮件 = 1 的人。我的问题是如何实现这一点柱状图。 Any suggestions would be greatly appreciated!任何建议将不胜感激!

15k |  Y axis would be amount of people who recieved 1 piece, 2 piece, etc..
14k |   _ 
13k |  | | 
12k | _| |
11k || | |
10k ||_|_|_________________________
      1  2  3  4  5  6  7  8  9    <-AmountOfMailRecieved

1) Starting with your sample data in Power BI. 1)从 Power BI 中的示例数据开始。

样本数据

2) Create a new calculated table that is a distinct list of years from your source table. 2)创建一个新的计算表,它是与源表不同的年份列表。

Years = DISTINCT(
    SELECTCOLUMNS(
        Mail,
        "SalesYear", Mail[salesYear]
    )
)

年表

3) Create another calculated table that crossjoins the year table with a series of integers. 3)创建另一个计算表,将年份表与一系列整数交叉连接。 Power BI might indicate the [Value] has an error (like it does for me in the picture below), but it works properly. Power BI 可能表示[Value]有错误(就像下图中对我所做的那样),但它可以正常工作。

MailCounts = SELECTCOLUMNS(
    CROSSJOIN(Years, GENERATESERIES(1, 20)),
    "SalesYear", Years[SalesYear],
    "MailReceived", [Value]
)

计数表

4) Add a calculated column that counts the number of IDs in the source table that match the year and mail count. 4)添加计算列,计算源表中与年份和邮件计数匹配的 ID 数量。 For example, the first row is counting how many distinct IDs show up exactly once in the source table for the year 2012;例如,第一行计算 2012 年有多少不同的 ID 在源表中只出现一次; it's 1 because only ID 11 shows up in 2012 once.它是 1,因为只有 ID 11 在 2012 年出现过一次。

PersonCount = CALCULATE(
    DISTINCTCOUNT(Mail[id]),
    FILTER(Mail,
        Mail[salesYear] = EARLIER(MailCounts[SalesYear]) &&
        EARLIER(MailCounts[MailReceived]) = CALCULATE(
            COUNTROWS(Mail),
            FILTER(Mail, 
                Mail[salesYear] = EARLIER(Mail[salesYear]) && 
                Mail[id] = EARLIER(Mail[id])
            )
        )
    )
)

人数

5) Create relationships between your source table and the year table, and then between the year table and the count table. 5)在源表和年表之间创建关系,然后在年表和计数表之间创建关系。 This will allow the creation of a slicer based on the year of your source table to filter the results from the count table.这将允许根据源表的年份创建切片器以过滤计数表中的结果。

关系

6) Optionally, you can hide the year field in the source and count tables. 6)或者,您可以隐藏源表和计数表中的年份字段。 After doing that if you desire, create a chart as configured in the picture below如果你愿意,这样做之后,创建一个如下图配置的图表

图表

7) Create a slicer from the year table as shown in the picture below. 7)从年表中创建一个切片器,如下图所示。

切片机

And that's it.就是这样。 The chart should match with your expected outcome and can be filtered by year.该图表应与您的预期结果相匹配,并可按年份过滤。

您将在度量或列中使用 DISTINCTCOUNT。

VariableName = DISTINCTCOUNT([AmountofMailReceived])

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

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