简体   繁体   English

每个月的 Rankx 函数 DAX Power BI

[英]Rankx function for every month DAX Power BI

So I have a table in Power BI with Snapshot dates, Company Name and a Rating.所以我在 Power BI 中有一个表格,其中包含快照日期、公司名称和评级。 I want to Rank the Ratings for each particular month (no month has repeated companies).我想对每个特定月份的评级进行排名(没有一个月有重复的公司)。

So as a simple example, for the table below:所以作为一个简单的例子,对于下表:

Snapshot_date快照日期 Company Name公司名 Rating评分
31/03/2021 31/03/2021 A一种 1 1
31/03/2021 31/03/2021 B 8 8
31/03/2021 31/03/2021 C C 6 6
31/03/2021 31/03/2021 D D 5 5
30/04/2021 30/04/2021 A一种 4 4
30/04/2021 30/04/2021 B 7 7
30/04/2021 30/04/2021 C C 9 9
30/04/2021 30/04/2021 D D 3 3

I want to end up with:我想结束:

Snapshot_date快照日期 Company Name公司名 Rating评分 Rank
31/03/2021 31/03/2021 A一种 1 1 1 1
31/03/2021 31/03/2021 B 8 8 4 4
31/03/2021 31/03/2021 C C 6 6 3 3
31/03/2021 31/03/2021 D D 5 5 2 2
30/04/2021 30/04/2021 A一种 4 4 2 2
30/04/2021 30/04/2021 B 7 7 3 3
30/04/2021 30/04/2021 C C 9 9 4 4
30/04/2021 30/04/2021 D D 3 3 1 1

It seems the measure should be something like:似乎该措施应该是这样的:

Rank = Rankx(filter(
                    all(table),
                    table[Snapshot_date].[month] = Earlier(table[Snapshot_date].month)
                    && table[Snapshot_date].[year] = Earlier(table[Snapshot_date].year)
                     ),
                     Calculate(Sum(table[Rating])),,1)

But here I get an error where it states that parameter in the Earlier function is not the correct type, and I am referring to an earlier row context which doesn't exist.但是在这里我得到一个错误,它指出 Early 函数中的参数不是正确的类型,我指的是不存在的较早的行上下文。

I have also tried:我也试过:

Rank = Rankx(allexcept(table,
                    table[Snapshot_date].[month], table[Snapshot_date].[year]
                     ),
                     Calculate(Sum(table[Rating])),,1)

However this simply does the rank for the whole table rather than for each month.然而,这只是对整个表而不是每个月进行排名。

In the simple example with the table above with only two months, what has worked is:在只有两个月的上表的简单示例中,有效的是:

Rank = Rankx(filter(
                    all(table),
                    table[Snapshot_date].[month] = 1
                    && table[Snapshot_date].[year] = 2021)
                     ),
                     Calculate(Sum(table[Rating])),,1)

This weirdly works for both months if the month and year is one of the two dates.如果月份和年份是两个日期之一,则这对两个月份都有效。 However if the month and year I use is not one of the two dates the ranks all become 1.但是,如果我使用的月份和年份不是两个日期之一,则排名都变为 1。

Also I want this to work on a large table with many dates and want it to be dynamic.此外,我希望它可以在具有多个日期的大表上工作,并希望它是动态的。 Any help would be greatly appreciated!任何帮助将不胜感激!

You need to remove filter context from your calculation (row filter).您需要从计算中删除过滤器上下文(行过滤器)。 ALLEXCEPT is really useful, put your group here. ALLEXCEPT 真的很有用,把你的小组放在这里。

Use this measure:使用此措施:

Rank = 

RANKX( 
            ALLEXCEPT( Sheet1,Sheet1[Snapshot_date] ),
             CALCULATE(SUM(Sheet1[Rating]))
  )

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

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