Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Create custom fields that extracts values from a field

Hi,   I have a data like the one below and i want to create two custom field (Sales Yesterday and Sales last Week) that extract values from the Sold field for any row whose TxnDate is equal to the ...
  • v-alq-msft's avatar
    6 years ago

    Hi, Anonymous 

     

    Based on your description, I created data to reproduce your scenario. I used 7/17/2020 as test date on my side. You need to modify it as Today(). The pbix file is attached in the end.

     

    You may creatd measures as below.

    Last week Sales = 
    var tab = 
    SUMMARIZE(
        'Table',
        'Table'[Prroduct],
        "Result",
        var _testdate = DATE(2020,7,17)
        var _product = [Prroduct]
        return
        CALCULATE(
            SUM('Table'[Sold]),
            FILTER(
                ALL('Table'),
                'Table'[Prroduct]=_product&&
                YEAR('Table'[TxnDate])=YEAR(_testdate)&&
                WEEKNUM('Table'[TxnDate])=WEEKNUM(_testdate)-1
            )
        )
    )
    var result =
    SUMX(
        tab,
        [Result]
    )
    return
    IF(
        ISBLANK(result),
        0,
        result
    )
    
    YesterDay Sales = 
    var tab = 
    SUMMARIZE(
        'Table',
        'Table'[Prroduct],
        "Result",
        var _testdate = DATE(2020,7,17)
        var _product = [Prroduct]
        return
        CALCULATE(
            SUM('Table'[Sold]),
            FILTER(
                ALL('Table'),
                'Table'[Prroduct]=_product&&
                'Table'[TxnDate]=_testdate-1
            )
        )
    )
    var result = 
    SUMX(
        tab,
        [Result]
    )
    return
    IF(
        ISBLANK(result),
        0,
        result
    )

     

    Result:

     

    Best Regards

    Allan

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.