使用m代码/ DAX在Excel中创build一个直方图

我正在使用这个令人敬畏的教程 。

let //Get data from Customers table Source = Excel.CurrentWorkbook(){[Name="Customers"]}[Content], //Get a list of all the values in the Age column Ages = Table.Column(Source,"Age"), //Find the maximum age MaxAge = List.Max(Ages), //The number of buckets is the max age divided by ten, then rounded up to the nearest integer NumberOfBuckets = Number.RoundUp(MaxAge/10), //Hash function to determine which bucket each customer goes into BucketHashFunction = (age) => Number.RoundDown(age/10), //Use Table.Partition() to split the table into multiple buckets CreateBuckets = Table.Partition(Source, "Age", NumberOfBuckets, BucketHashFunction), //Turn the resulting list into a table #"Table from List" = Table.FromList(CreateBuckets, Splitter.SplitByNothing() , null, null, ExtraValues.Error), //Add a zero-based index column #"Added Index" = Table.AddIndexColumn(#"Table from List", "Index", 0, 1), //Calculate the name of each bucket #"Added Custom" = Table.AddColumn(#"Added Index", "Bucket", each Number.ToText([Index]*10) & " to " & Number.ToText(([Index]+1)*10)), //Find the number of rows in each bucket - ie the count of customers #"Added Custom1" = Table.AddColumn(#"Added Custom", "Count", each Table.RowCount([Column1])), //Remove unnecessary columns #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Column1", "Index"}) in #"Removed Columns" 

最终的结果是:

在这里输入图像说明

我想知道如何将50岁以上的所有年龄段的人都称为“50+”?

你需要改变这一行:BucketHashFunction =(age)=> Number.RoundDown(age / 10),

如下:BucketHashFunction =(age)=> if(age> 85)then“85+”else Number.RoundDown(age / 10),

您还需要确保这一行的数据types正确:

“添加自定义”= Table.AddColumn(#“添加索引”,“桶”,

  each Number.ToText([Index]*10) & " to " & Number.ToText(([Index]+1)*10)), 

最好使用UI将数据types设置为string。 我没有testing这个解决scheme,但是这基本上是遵循的模式。

感谢您使用Power BI。

Lukasz P.

微软的Power BI Team

如果您想了解Power BI开发人员的故事更新,可以注册( http://solutions.powerbi.com/appsuggestion.html )或关注我们的博客( http://blogs.msdn.com/ b / powerbidev / )