我想在 DAX 中计算 Excel 函数 PERCENTRANK.INC 的等效项,但按类别计算。我承认我什至不知道如何计算类别。任何提示将不胜感激。
以下是示例数据的 M 代码:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcisqzSwpVtJRSiwoyEkF0oZKsTpIwkmJeUAIZJigipfn56QlpRYVVQLZpqhSyRlQcWOweFhqempJYlJOKlgusagovwTIMMKUK8gvSSzJhzsBRS4/LzM/D0ibo1qFw9HILogFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Category = _t, Product = _t, Amount = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Amount", Int64.Type}})
in
#"Changed Type"
以下措施将产生预期的结果。由于DAX中没有PERCENTRANK函数,您可以根据RANKX和COUNTROWS的结果手动计算。
Percent Rank Within Category =
IF (
-- This calculation only makes sense if there is only one product
-- in the current filter context. If there are more than one products
-- or no product in the filters, BLANK should be returned.
HASONEVALUE ( MyTable[product] ),
-- Get all products which belong to the same parent category with
-- the product currently being filtered
VAR tbl = CALCULATETABLE (
-- all products, in the modified filter context of...
VALUES ( MyTable[product] ),
-- no filter on product
REMOVEFILTERS ( MyTable[product] ),
-- and under the same parent category
VALUES ( MyTable[Category] )
)
RETURN
CALCULATE (
-- PERCENTRANK = (<rank of product> - 1)
-- / (<total N of products> - 1)
DIVIDE (
-- Sales rank of each product in ascending order
RANKX (
tbl,
CALCULATE ( SUM ( MyTable[Amount] ) ), ,
ASC
) - 1,
-- Total number of products
COUNTROWS ( tbl ) - 1,
-- When there is only one product, it should return 1
1
)
)
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)