简体   繁体   English

使用DAX的RANKX问题

[英]RANKX issues using DAX

I was working on the conversion of a sql request into a dax one : 我正在将sql请求转换为dax一个:

    >;WITH cte
    >AS
    >(
    >       SELECT uc.idU_Email
    >              ,uc.id_Type
    >              ,uc.dRecueil
    >              ,valeur
    >              ,ROW_NUMBER() OVER(PARTITION BY idU_Email,id_Type ORDER BY dRecueil DESC, valeur ASC) AS rang
    >       FROM   vUE uc 
    >       WHERE uc.Id_Type=1  AND uc.dRecueil<=(DATEADD(month, -1, GETDATE())) 
    >)                   
    >SELECT COUNT(idU_Email)
    >    FROM   cte
    >    WHERE  rang = 1 
    >    and valeur = 1

If i understood well, the equivalent of the rownumber function in sql in the RANKX Function in DAX. 如果我很好理解,则相当于DAX的RANKX函数中sql中的rownumber函数。 So basically, to achieve the conversion i created a new calculated colum with the following expression : 因此,基本上,为了实现转换,我使用以下表达式创建了一个新的计算列:

Ranking on partition 分区排名

To resume it, it makes a ranking on every parition of idU_Email order by dRecueil and then by valeur. 要恢复它,它将对dRecueil然后由valeur对idU_Email订单的每个分区进行排名。

Thus, the only thing left to do was to add a condition on the date (like in my SQL request), but i guess i don't really know how (or where?), and an error occurred when i tried, which i haven't achieved to solved yet : 因此,剩下要做的就是在日期上添加条件(例如在我的SQL请求中),但是我想我真的不知道如何(或在哪里?),并且在尝试时发生错误,我尚未解决:

Ranking on partition with filter on date 按日期过滤分区排名

I hope someone would find a way to solve that issue (or even to propose me a better way to have the equivalent of my SQL request). 我希望有人能找到解决该问题的方法(甚至向我提出一种更好的方法来实现与我的SQL请求等效的方法)。

Thanks by advance ! 预先感谢! Smiley Happy 笑脸快乐

[UPDATE] [更新]

I've achieved to partition as I wanted to do and to apply a date filter like that : 我已经实现了按需分区的功能,并且可以像这样应用日期过滤器:

RANK_OnDate = IF(vUE[dRecueil] <= DATE(YEAR(TODAY()); MONTH(TODAY())-1; DAY(TODAY()))
            ;RANKX(FILTER(vUE; vUE[idU_Email] = EARLIER(vUE[idU_Email]) && vUE[id_Type] = EARLIER(vUE[id_Type]))
                ;RANKX(ALL(vUE); vUE[dRecueil]; ;ASC) 
                +
                DIVIDE(
                    RANKX(ALL(vUE); vUE[valeur]; ; DESC; Skip)
                    ;COUNTROWS(ALL(vUE)) + 1
                )
            )
        )

But, actually, i would like it to filter dynamically... Thus i don't know if it would be better to use that directly in a measure (according that i want to count distinctly the lower/higher rank for each idU_email). 但是,实际上,我希望它可以动态过滤...因此,我不知道直接在度量中使用它是否会更好(根据我想分别计算每个idU_email的较低/较高等级)。

Additionally my filter just apply blank when the date isn't ok with the filter but the ranking stay the same... 此外,当过滤器的日期不正确但排名保持不变时,我的过滤器只会应用空白。

I tried to do directly the distinct count in a measure but can't save the different issues encountered... Have you got some ideas ? 我试图直接进行度量,但是无法保存遇到的不同问题……您有一些想法吗?

(and thanks for your answers) :) (并感谢您的回答):)

I think you're pretty close, try this 我觉得你很亲密,试试看

Rank = IF(vUE[dRecueil] <= DATE(YEAR(TODAY()), MONTH(TODAY())-1, DAY(TODAY())),
           RANKX(FILTER(vUE, vUE[idU_Email] = EARLIER(vUE[idU_Email])),
               RANKX(ALL(vUE), vUE[dRecueil], ,ASC) +
               DIVIDE(
                   RANKX(ALL(vUE), vUE[valeur], , DESC, Skip),
                   COUNTROWS(ALL(vUE)) + 1)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM