DAX - Get current row number

纵饮孤独 提交于 2021-02-11 12:49:50

问题


The question seems to be easy but I found it really hard to be done in DAX. I'd like to get the current row number on my current context data. Same as ROW_NUMBER() in T-SQL.

Any hints?

Thanks in advance.


回答1:


There is no such function. Closest you can get is to calculate rank based on sort-order, like:

DEFINE MEASURE SomeTbl[ROW_NO] = SUMX(SomeTbl, RANKX(ALL(SomeTbl), SomeTbl[SortCol]))
EVALUATE ADDCOLUMNS(SomeTbl, "ROW_NO", SomeTbl[ROW_NO])

Or if you can't use RANKX

EVALUATE ADDCOLUMNS (
  SomeTbl, "ROW_NO", COUNTROWS(FILTER(SomeTbl, 
    EARLIER(SomeTbl[SortCol])<=SomeTbl[SortCol]))+0
)

Note: for same values (of SomeTbl[SortCol]) the ROW_NO will be the same.

If you're using DirectQuery mode you can also in model add extra column and define it likeSELECT *, ... ROW_NUMBER() as Foo - and use Foo column in DAX instead.



来源:https://stackoverflow.com/questions/44242492/dax-get-current-row-number

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