简体   繁体   English

Power BI/DAX:按添加的列值筛选 SUMMARIZE 或 GROUPBY

[英]Power BI/DAX: Filter SUMMARIZE or GROUPBY by added column value

because of confidential nature of data, I'll try to describe what I'm struggling with using some random examples.由于数据的机密性,我将尝试使用一些随机示例来描述我正在努力解决的问题。 Let's say I have a fact table with invoices data in Power BI.假设我在 Power BI 中有一个包含发票数据的事实表。 I need to count number of distinct product ID's with sales over let's say €50k in last 12 months or to be more precise in 12 months prior selected date.我需要计算在过去 12 个月内销售额超过 5 万欧元的不同产品 ID 的数量,或者更准确地说是在选定日期之前的 12 个月内。 At the same time I need to be able to narrow down results to selected Country, Product group and Product category.同时,我需要能够将结果缩小到选定的国家、产品组和产品类别。

I've started with setting the dates range for DATESBETWEEN like this:我已经开始像这样设置 DATESBETWEEN 的日期范围:

productsCount = 
VAR lastDay = IF(MAX('Calendar table'[Date]) > NOW(); NOW(); MAX('Calendar table'[Date]))
VAR firstDay = EDATE(lastDay; -12)
RETURN

But then I got lost:但后来我迷路了:

CALCULATE(
    COUNTROWS('Sales');
    SUMMARIZE(
        'Sales';
        'Sales'[ProductID];
        "prodSales"; SUM('Sales'[EUR])
    );
    DATESBETWEEN('Sales'[Date]; firstDay; lastDay);
    ALLEXCEPT(
        'Sales';
        'Sales'[Product group];
        'Sales'[Product category];
        'Sales'[Country]
    );
    [prodSales] > 50000
)

The thing is that I need to be able to filter summarized data by sum of sales before I'll count rows.问题是,在计算行数之前,我需要能够按销售额总和过滤汇总数据。

I haven't tested this but I think something like this might work where you filter after summarizing:我没有对此进行测试,但我认为这样的事情可能适用于您在总结后进行过滤:

productsCount =
VAR lastDay =
    IF (
        MAX ( 'Calendar table'[Date] ) > NOW ();
        NOW ();
        MAX ( 'Calendar table'[Date] )
    )
VAR firstDay = EDATE ( lastDay; -12 )
RETURN
    COUNTROWS (
        FILTER (
            CALCULATETABLE (
                SUMMARIZE ( 'Sales';
                   'Sales'[ProductID];
                   "prodSales"; SUM ( 'Sales'[EUR] )
                );
                DATESBETWEEN ( 'Sales'[Date]; firstDay; lastDay );
                ALLEXCEPT (
                    'Sales';
                    'Sales'[Product group];
                    'Sales'[Product category];
                    'Sales'[Country]
                )
            );
            [prodSales] > 50000
        )
    )

You can create a summarized table in power query editor and then create a measure in dax to filter the result over 50k.您可以在 power 查询编辑器中创建一个汇总表,然后在 dax 中创建一个度量来过滤超过 50k 的结果。

In summarise, we can do filter after the summarize table总之,我们可以在汇总表之后进行过滤

My input table is我的输入表是

在此处输入图像描述

I'm writing query我正在写查询

App Downlaod Count = 

FILTER(
    SUMMARIZE(events_intraday,
    events_intraday[event_name],
    events_intraday[event_date],
    events_intraday[platform],
    events_intraday[stream_id],
    "Count",CALCULATE(
        COUNT(events_intraday[event_name]),
            FILTER(events_intraday,
                    (events_intraday[platform] = "ANDROID" || 
                    events_intraday[platform] = "IOS")
                )
            )
        ),
        events_intraday[event_name] = "first_open" && 
        (events_intraday[stream_id] = "1415470954" || 
        events_intraday[stream_id] = "1420775080")
    )

Output is Output 是

在此处输入图像描述

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

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