使用 m 代码/DAX 在 excel 中创建直方图
Creating a histogram in excel using m code / DAX
我正在使用这个很棒的 tutorial。
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),
您还需要确保此行的数据类型正确:
"添加自定义" = Table.AddColumn(#"添加索引", "桶",
each Number.ToText([Index]*10) & " to " & Number.ToText(([Index]+1)*10)),
最好使用UI设置数据类型为字符串。我没有测试这个解决方案,但这基本上是要遵循的模式。
感谢您使用 Power BI。
Lukasz P.
微软 Power BI 团队
如果您想了解 Power BI 开发人员故事更新,您可以注册 (http://solutions.powerbi.com/appsuggestion.html) or follow our blog (http://blogs.msdn.com/b/powerbidev/)
我正在使用这个很棒的 tutorial。
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),
您还需要确保此行的数据类型正确:
"添加自定义" = Table.AddColumn(#"添加索引", "桶",
each Number.ToText([Index]*10) & " to " & Number.ToText(([Index]+1)*10)),
最好使用UI设置数据类型为字符串。我没有测试这个解决方案,但这基本上是要遵循的模式。
感谢您使用 Power BI。
Lukasz P.
微软 Power BI 团队
如果您想了解 Power BI 开发人员故事更新,您可以注册 (http://solutions.powerbi.com/appsuggestion.html) or follow our blog (http://blogs.msdn.com/b/powerbidev/)