Power BI - 使用 Dax 基于分组进行过滤

时间:2021-01-20 09:22:25

标签: sql powerbi dax powerbi-desktop

我是 DAX 的新手。

假设我有一张看起来像这样的桌子:

Table A:

status     delivered    sold
late       10           50
late       20           300
early      5            500

假设我正在使用这个 SQL 查询:

with cte_1 as (

select 
status, count(*) as [row_count]
from [table a]
group by [status]
having count(*) > 1

)

select *
from [table a] as p1
inner join [cte_1] as p2
on p1.[status] = p2.[status]

什么是 dax 等价物?

1 个答案:

答案 0 :(得分:1)

SQL 查询返回表 A 中状态至少在表中出现两次的行,并将状态相同的行数相加。在Power BI中我们可以写一个计算表,将相同状态的行数相加,然后过滤掉计数小于2的行

Result =
FILTER(
    ADDCOLUMNS(
        'Table A',
        "row_count",
            CALCULATE(
                COUNTROWS( 'Table A' ),
                ALLEXCEPT( 'Table A', 'Table A'[Status] )
            )
    ),
    [row_count] > 1
)