行号分区到POWER BI DAX查询

时间:2020-07-26 15:42:41

标签: powerbi dax rownum

有人可以帮我将sql字符串转换为Dax吗?

row_number()结束(按日期,客户,按日期键入订单) enter image description here

行号是我想要的输出。

1 个答案:

答案 0 :(得分:0)

假设您的数据如下表所示:

Sample
+------------+----------+---------+--------+
|    Date    | Customer | Product | Gender |
+------------+----------+---------+--------+
| 01/01/2018 |     1234 | P2      | F      |
| 01/01/2018 |     1234 | P2      | M      |
| 03/01/2018 |     1235 | P1      | F      |
| 03/01/2018 |     1235 | P2      | F      |
+------------+----------+---------+--------+

我已经使用RANKX和FILTER函数创建了一个名为Rank的计算列。

计算的第一部分是创建FILTER函数范围之外的变量。第二部分使用RANKX,该RANKX使用一个表达式值(在本例中为Gender)对值进行排序。

Rank = 
VAR _currentdate = 'Sample'[Date]
VAR _customer = 'Sample'[Customer]
var _product = 'Sample'[Product]

return
RANKX(FILTER('Sample',
[Date]=_currentdate &&
[Customer] = _customer &&
[Product] = _product),[Gender],,ASC)

输出为

enter image description here

我将输出与SQL等效进行了对比。

select 
*, 
row_number() over(partition by Date,Customer,Product order by Gender) 
from (
select '2018-01-01' as Date,1234 as CUSTOMER,'P2' AS PRODUCT, 'M' Gender union
select '2018-01-01' as Date,1234,'P2','F' UNION
select '2018-01-03' as Date,1235,'P1','F' UNION
  select '2018-01-03' as Date,1235,'P2','F' 
)t1

enter image description here