Row number partition by to POWER BI DAX query

孤街醉人 提交于 2021-01-29 09:33:51

问题


Can someone help me to convert the sql string to Dax?

row_number() p over (partition by date, customer, type order by day)enter image description here

The row number is my desired output.


回答1:


Assuming that your data looks like this table:

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      |
+------------+----------+---------+--------+

I have created a calculated column called Rank, using the RANKX and FILTER function.

The first part of the calculation is to create variables outside the scope of the FILTER function. The second part uses RANKX that takes an expression value - in this case Gender - to order the values.

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)

The output is

I contrasted the output to the SQL equivalent.

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



来源:https://stackoverflow.com/questions/63102302/row-number-partition-by-to-power-bi-dax-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!