PowerBI DAX - Identifying first instance based on multiple criteria

久未见 提交于 2019-12-20 07:41:41

问题


Using DAX to identify first instance of a record

I'm faced with trying to identify the first instance in a database where someone (identified by the ID column) has purchased a product for the first time. It's possible for said person to purchase the product multiple times on different days, or purchase different products on the same day. I drummed up an excel formula that gets me there, but am having trouble translating into DAX.

=COUNTIFS(ID,ID,PurchaseDate,"<="&PurchaseDate,Product,Product)

Which results in the correct values in the "First Instance?" Column.

Ideally I won't have to hardcode values, as I would like to use the "Product" column as a parameter in the future. If there are other suggests aside from translating this in DAX, that would also be appreciated! (IE using filters, or other tools in PowerBI)

Thanks in advance!


回答1:


This is very similar to an answer I gave to another question (which you can find here).

In that question, the request was to see a running count of rows for the given row's criteria (product, year, etc.). We can modify that slightly to get it to work in your problem.

This is the formula I provided in the answer I linked above. The basic concept is to use the EARLIER functions to get the value from the row and pass it into the filter statement.

Running Count = 
    COUNTROWS(
        FILTER(
            'Data',
            [ProductName] = EARLIER([ProductName]) &&
            [Customer] = EARLIER([Customer]) &&
            [Seller] = EARLIER([Seller]) &&
            [Year] <= EARLIER([Year])
        )
    )

What I would suggest for your problem is to create this as a TRUE/FALSE flag by simply checking if the running count is 1. This formula will evaluate to a Boolean flag.

First Instance = 
    COUNTROWS(
        FILTER(
            'Data',
            [ID] = EARLIER([ID]) &&
            [Product] = EARLIER([Product]) &&
            [Purchase Date] <= EARLIER([Purchase Date])
        )
    ) = 1


来源:https://stackoverflow.com/questions/55286495/powerbi-dax-identifying-first-instance-based-on-multiple-criteria

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!