简体   繁体   中英

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. 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. 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:

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.

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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